qt/LedOK/synctimer.cpp

35 lines
879 B
C++
Raw Permalink Normal View History

2023-04-18 14:14:46 +08:00
#include "synctimer.h"
#include <QDateTime>
#include <QDebug>
2023-04-21 11:06:47 +08:00
#ifdef Q_OS_WINDOWS
2023-04-18 14:14:46 +08:00
#include <windows.h>
2023-04-21 11:06:47 +08:00
#endif
2023-04-18 14:14:46 +08:00
SyncTimer::SyncTimer(int inter) {
this->inter = inter;
connect(this, &QThread::finished, this, &QThread::deleteLater);
}
void SyncTimer::run(){
qint64 curTime;
sleep = inter;
if(sleep <= 0) return;
2023-04-21 11:06:47 +08:00
#ifdef Q_OS_WINDOWS
2023-04-18 14:14:46 +08:00
timeBeginPeriod(1);
2023-04-21 11:06:47 +08:00
#endif
2023-04-18 14:14:46 +08:00
msleep(sleep);
qint64 exeTime = QDateTime::currentMSecsSinceEpoch();
while(inter > 0) {
inter = 0;
emit timeout();
curTime = inter;
if(curTime<=0) break;
exeTime += curTime;
curTime = QDateTime::currentMSecsSinceEpoch();
sleep = exeTime - curTime;
if(exeTime > curTime) msleep(sleep);
else exeTime = curTime;
}
2023-04-21 11:06:47 +08:00
#ifdef Q_OS_WINDOWS
2023-04-18 14:14:46 +08:00
timeEndPeriod(1);
2023-04-21 11:06:47 +08:00
#endif
2023-04-18 14:14:46 +08:00
}