問(wèn)題描述
下面是我的 qthread 實(shí)現(xiàn)代碼.我正在嘗試從衛(wèi)星獲取 GPS 數(shù)據(jù).即使程序退出 gpsSearch()
槽函數(shù),QThread 也不會(huì)產(chǎn)生finished() 信號(hào).每當(dāng)單擊按鈕時(shí)都會(huì)調(diào)用函數(shù) locateMe()
.第一次當(dāng)線程未啟動(dòng)并單擊按鈕時(shí),它會(huì)為 isRunning()
函數(shù)打印真值,并為 isFinished()
函數(shù)打印假值.我不得不調(diào)用 QTherad 的 quit()
函數(shù)來(lái)手動(dòng)停止線程.之后,它轉(zhuǎn)到 gnssProvider
類中連接的 threadQuit()
函數(shù).但即使在那之后,如果我點(diǎn)擊按鈕,它會(huì)在 locateMe()
函數(shù)中為 isRunning
打印真值,為 isFinished()
打印假值.
Below is the code for my qthread implementation. I am trying to get gps data from satellite. QThread doesn't produce the finished() signal even when the programs exits gpsSearch()
slot function. The function locateMe()
is called whenever a button is clicked. The first time when the thread isnt started and the button is clicked it prints true value for isRunning()
function and prints false value for isFinished()
function. I had to call the quit()
function of the QTherad to manually stop the thread. After that it goes to the connected threadQuit()
function in the gnssProvider
class. But even after that if I click the button it prints true value for isRunning
and false for isFinished()
in the locateMe()
function.
GPSInfo::GPSInfo()
{
hybridGPSFound = satelliteGPSFound = networkGPSFound = false;
qDebug()<<"Thread Creating";
gnssThread = new QThread;
gnssProvider = new LocationFetcher(this,GEOLOCATION_PROVIDER_GNSS,1);
gnssProvider->moveToThread(gnssThread);
connect(gnssThread, SIGNAL(started()), gnssProvider, SLOT(gpsSearch()));
connect(gnssThread, SIGNAL(finished()), gnssProvider, SLOT(threadQuit()));
}
void LocationFetcher::gpsSearch()
{
if (BPS_SUCCESS != geolocation_request_events(0))
{
fprintf(stderr, "Error requesting geolocation events: %s", strerror(errno));
return;
}
geolocation_set_provider(GPS_Search_Provider);
geolocation_set_period(GPS_Search_Period);
while (!stopThread)
{
bps_event_t *event = NULL;
bps_get_event(&event, -1);
if (event)
{
if (bps_event_get_domain(event) == geolocation_get_domain() && bps_event_get_code(event) == GEOLOCATION_INFO)
{
handle_geolocation_response(event);
break;
}
}
}
geolocation_stop_events(0);
this->quit();
}
void GPSInfo::LocateMe()
{
qDebug()<<"Thread Running: "<<gnssThread->isFinished();
qDebug()<<"Thread Running: "<<gnssThread->isRunning();
gnssThread->start();
hybridThread->start();
networkThread->start();
}
推薦答案
QThread 生命周期的工作方式是這樣的:
The way the QThread lifecycle works is like this:
- 你調(diào)用
QThread::start()
. - 此時(shí),
isRunning()
應(yīng)該開(kāi)始返回 true. - 線程內(nèi)部開(kāi)始.它們發(fā)出
started()
信號(hào). - 線程內(nèi)部調(diào)用
run()
. - 除非你在子類中重寫(xiě)它,
run()
調(diào)用exec()
. exec()
進(jìn)入一個(gè)事件循環(huán)并保持在那里直到quit()
或exit()
被調(diào)用.exec()
和run()
返回內(nèi)部.- 此時(shí),
isFinished()
應(yīng)該開(kāi)始返回 true 和isRunning()
false. - 內(nèi)部發(fā)出
finished()
信號(hào). - 內(nèi)部人員做了一些最后的清理工作.
- 線程真正終止.
- You call
QThread::start()
. - At this point,
isRunning()
should start returning true. - The thread internals start. They emit the
started()
signal. - The thread internals call
run()
. - Unless you override this in a subclass,
run()
callsexec()
. exec()
enters an event loop and stays there untilquit()
orexit()
is called.exec()
andrun()
return to the internals.- At this point,
isFinished()
should start returning true andisRunning()
false. - The internals emit the
finished()
signal. - The internals do some final cleanups.
- The thread terminates for real.
所以你需要在你的位置獲取器完成后調(diào)用 quit()
- 但是 this->quit()
沒(méi)有調(diào)用 quit()
在線程上!這可能就是它什么都不做的原因.
So you need to call quit()
after your location fetcher is done - but this->quit()
isn't calling quit()
on the thread! This is probably why it's not doing anything.
你的代碼看起來(lái)有點(diǎn)像這篇文章之后的模式:
Your code looks a bit like it was patterned after this article:
http://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/
注意她如何給她的工人一個(gè) finished()
信號(hào)(與 QThread::finished
不同)并將其連接到 QThread::quit()
槽.
Note how she gives her worker a finished()
signal (not the same as QThread::finished
) and connects it to the QThread::quit()
slot.
這篇關(guān)于QThread 發(fā)出finished() 信號(hào)但isRunning() 返回true 并且isFinished() 返回false的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!