C++ (Qt)void MyThread::run(){ QTimer timer; connect( &timer, SIGNAL( timeout() ), SLOT( timerTick() ) ); timer.start( 1000 ); exec();} void MyThread::timerTick(){ qDebug() << "tick";}
class EventLoopThread implements Runnable { @Iverride public void run() { QEventLoop loop = new QEventLoop(); loop.exec(); // Do whatever loop.exit(); } }
CThread::run{t1.start(10);connect(&t1,SIGNAL(timeout()),this,SLOT(func1()));t2.start(1000);connect(&t2,SIGNAL(timeout()),this,SLOT(func2()));}
CThread::run{QEventLoop loop = new QEventLoop();loop.exec();t1.start(10);connect(&t1,SIGNAL(timeout()),this,SLOT(func1()));t2.start(1000);connect(&t2,SIGNAL(timeout()),this,SLOT(func2()));loop.exit();}
C#include <QtCore/QCoreApplication>#include "thread.h" int main(int argc, char *argv[]){ QCoreApplication a(argc, argv); Thread thread; thread.start(); return a.exec();}
C#ifndef THREAD_H#define THREAD_H #include <QThread>#include <QTimer>#include <QEventLoop> class Thread : public QThread{ Q_OBJECTpublic: explicit Thread(QObject *parent = 0); void run(); signals: public slots: bool f1(); bool f2(); private: QTimer t1,t2; }; #endif // THREAD_H
C#include "thread.h" Thread::Thread(QObject *parent) : QThread(parent){} void Thread::run(){ QEventLoop loop(this); t1.start(10); connect(&t1,SIGNAL(timeout()),this,SLOT(f1())); t2.start(1000); connect(&t2,SIGNAL(timeout()),this,SLOT(f2())); loop.exec();} bool Thread::f1(){ int x = 123; return true;} bool Thread::f2(){ int x = 123; return false;}