我想停下來時,信號被髮射所以這裏一個循環的線程是我的代碼如何停止循環線程
void MyThread::stopWatchingThread()
{
qDebug()<<"MyThread::stopWatchingThread()";
Keep_running=false;
qDebug()<<"MyThread::stopWatchingThread Keep_running"<<Keep_running;
...
}
void MyThread::run()
{
qDebug()<<"MyThread::run()";
qDebug()<<"MyThread::run Keep_running"<<Keep_running;
while(Keep_running)
{
...
}
qDebug()<<"MyThread::run Keep_running"<<Keep_running;
Keep_running=false;
qDebug()<<"MyThread::run Keep_running"<<Keep_running;
}
void Watcher::Init()
{
WatchingThread=new MyThread(this->L_RootToWatch);
connect(this,SIGNAL(stopmonotiring()),WatchingThread, SLOT(stopWatchingThread()));
...
}
void Watcher::StartWatching()
{
WatchingThread->start();
}
void Watcher::StopWatching()
{
emit stopmonotiring();
}
所以每一件事情去所有的權利,但我的問題是,Keep_running
從未在MyThread::run()
得到false
值永遠發出stopWatchingThread
等while
循環。 我錯過了什麼? 任何幫助將不勝感激。
你檢查過,如果你真的進入'MyThread :: stopWatchingThread()'? – sashoalm
是的,我的'Keep_running'是假的 – Oumaya
你確定Keep_Running是同一個變量嗎?從run()和MyThread :: stopWatchingThread()中打印它的地址(使用'qDebug()<<&Keep_Running;')並且比較 – sashoalm