21,479
社区成员
发帖
与我相关
我的任务
分享
Worker *worker;
QTimer timer;
worker->moveToThread(Threaddata);
connect(&timer,SIGNAL(timeout()),worker,SLOT(repeatResult()));
timer.stop();
QObject::killTimer: timers cannot be stopped from another thread
QObject::startTimer: timers cannot be started from another thread
后来就使用了信号槽来控制计时器的开始和停止
connect(this,SIGNAL(trigTimer()),&guitimer,SLOT(start()));
connect(this,SIGNAL(stopTimer()),&guitimer,SLOT(stop()));
而且在repeatResult函数里面加了
mutex->lock();
......
mutex->unlock();
这样子基本能解决问题。你说的信号量,我看了《C++ GUI Qt4》里的介绍,好像是用来解决生产者和消费者问题的,不知道该怎么用来解决我的这个问题,少侠能否不吝赐教?
,
写着异步调用的代码,却想达到同步的效果.
要想代码同步调用,可以使用信号量,限制timer的timeout,这样timer不会以准确的时间触发,会等上一次触发完成之后才会触发,效果大概是
timer时间大于repeatResult的时间,会取timer的时间,timer时间小于repeatResult,时间会取repeatResult的时间.
还有就是timer也移动到Threaddata这个里面去.这样timer和repeatResult在一个线程里面,这样就是顺序执行,repeatResult执行的时候,timer就会卡住.
如果上述方法都不是你想要的,你就只能自己写一个队列,自己处理队列.其实到最后和上述方法的效果应该是一样的.