项目场景:
多线程执行一些耗时操作,并且需要固定时间去轮询。文章来源地址https://www.toymoban.com/news/detail-663317.html
代码:
#include <QThread> #include <QTimer> #include <QtCore/QMutex> class pollingManager : public QObject { Q_OBJECT public: static pollingManager*getInstance() { if ( !instance ) { instance = new pollingManager(); } return instance; } void startTimerThread(); signals: void timeout( void ); slots: void timeoutHandle( void ); private: pollingManager() {} ~pollingManager() {} static pollingManager*instance; class pollingTimerThread : public QThread { public: void run() override; }; }; #endif // A7TIMERPOLLDEVICE_H .cpp #define POLLING_TIMER 600 pollingManager *pollingManager ::instance = nullptr; void pollingManager ::startTimerThread() { qDebug( "startA7TimerThread" ); pollingTimerThread*thread = new A7TimerThread(); thread->setObjectName( "pollingThread" ); thread->start(); } void pollingManager ::pollingTimerThread::run() { QTimer timer; timer.setInterval( POLLING_TIMER); connect( &timer, &QTimer::timeout, []() { timeoutHandle(); } ); timer.start(); exec(); }
文章来源:https://www.toymoban.com/news/detail-663317.html
到了这里,关于Qt多线程开启定时任务的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!