-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerateDataThread.cpp
More file actions
81 lines (57 loc) · 1.36 KB
/
GenerateDataThread.cpp
File metadata and controls
81 lines (57 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#ifndef GENERATEDATATHREAD_H
#define GENERATEDATATHREAD_H
#include "GenerateDataThread.h"
#include <QDateTime>
#include <QString>
#include <QDebug>
#include <Windows.h>
#include "DataEntry.h"
#ifdef DEBUG
#include <QDebug>
#endif
GenerateDataThread::GenerateDataThread(QObject *parent)
: QThread(parent)
{}
GenerateDataThread::~GenerateDataThread()
{
}
void GenerateDataThread::run()
{
LARGE_INTEGER frequency, start, current;
QueryPerformanceFrequency(&frequency); // 获取频率
QueryPerformanceCounter(&start); // 获取开始时间
while (!m_bStop)
{
static int id = 1;
// 获取当前时间
QueryPerformanceCounter(¤t);
// 计算已过的时间(毫秒)
double elapsedTime = static_cast<double>(current.QuadPart - start.QuadPart) * 1000.0 / frequency.QuadPart;
// 检查是否过了1毫秒
if (elapsedTime >= 1)
{
DataEntry dataentry;
dataentry.setId(id);
qint64 msSinceEpoch = QDateTime::currentMSecsSinceEpoch();
dataentry.setTime(msSinceEpoch);
dataentry.setValue(rand() % 100);
emit newDataEntry(dataentry);
id++;
start = current; // 更新开始时间
#ifdef DEBUG
qDebug() << "id:" << dataentry.id << "time:" << dataentry.time << "value:" << dataentry.value;
#endif
}
}
return;
}
void GenerateDataThread::stop()
{
m_bStop = true;
}
void GenerateDataThread::start()
{
QThread::start();
m_bStop = false;
}
#endif // !GENERATEDATATHREAD_H