#include "ttimer.h" #include #include SyncTimer::SyncTimer(int inter) { this->inter = inter; connect(this, &QThread::finished, this, &QThread::deleteLater); } void SyncTimer::run(){ try { long curTime = inter; if(curTime <= 0) return; msleep(curTime); long exeTime = QDateTime::currentDateTime().toMSecsSinceEpoch(); while(inter > 0) { inter = 0; emit timeout(); curTime = inter; if(curTime<=0) return; exeTime += curTime; curTime = QDateTime::currentDateTime().toMSecsSinceEpoch(); if(exeTime > curTime) msleep(exeTime - curTime); else exeTime = curTime; } } catch (...) {} }