久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

QThread 發(fā)出finished() 信號(hào)但isRunning() 返回true 并且

QThread emits finished() signal but isRunning() returns true and isFinished() returns false(QThread 發(fā)出finished() 信號(hào)但isRunning() 返回true 并且isFinished() 返回false)
本文介紹了QThread 發(fā)出finished() 信號(hào)但isRunning() 返回true 并且isFinished() 返回false的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(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:

  1. 你調(diào)用QThread::start().
  2. 此時(shí),isRunning() 應(yīng)該開(kāi)始返回 true.
  3. 線程內(nèi)部開(kāi)始.它們發(fā)出 started() 信號(hào).
  4. 線程內(nèi)部調(diào)用 run().
  5. 除非你在子類中重寫(xiě)它,run() 調(diào)用 exec().
  6. exec() 進(jìn)入一個(gè)事件循環(huán)并保持在那里直到 quit()exit() 被調(diào)用.
  7. exec()run() 返回內(nèi)部.
  8. 此時(shí),isFinished() 應(yīng)該開(kāi)始返回 true 和 isRunning() false.
  9. 內(nèi)部發(fā)出 finished() 信號(hào).
  10. 內(nèi)部人員做了一些最后的清理工作.
  11. 線程真正終止.
  1. You call QThread::start().
  2. At this point, isRunning() should start returning true.
  3. The thread internals start. They emit the started() signal.
  4. The thread internals call run().
  5. Unless you override this in a subclass, run() calls exec().
  6. exec() enters an event loop and stays there until quit() or exit() is called.
  7. exec() and run() return to the internals.
  8. At this point, isFinished() should start returning true and isRunning() false.
  9. The internals emit the finished() signal.
  10. The internals do some final cleanups.
  11. 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)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數(shù)據(jù)?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫(xiě) for() 循環(huán): for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環(huán)?)
Reusing thread in loop c++(在循環(huán) C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環(huán)形?)
主站蜘蛛池模板: 1级黄色大片 | 国产乱国产乱300精品 | 国产视频一二三 | 五月婷婷激情综合 | 亚洲免费视频一区 | 成人国产精品一区二区 | 国产激情网站 | 成人国产精品一区二区 | 欧美精品亚洲精品 | 91国内精品 | 国产三级久久 | 毛片网站在线观看 | 久久国产一区二区三区 | 中文字幕一区在线观看 | 秘密爱大尺度做爰呻吟 | 视频一区中文字幕 | 夜色在线影院 | 亚洲免费视频一区 | 特黄一级视频 | 国产一区欧美 | 日韩不卡av| 毛片在线免费 | 97国产视频| 国产福利一区二区三区 | 欧美在线亚洲 | www.嫩草| 中国黄色1级片 | 国产精品一品二区三区的使用体验 | 91av导航 | 欧美综合网| 欧美一级片在线观看 | 日韩精品二区 | 国产在线播放av | 免费黄色一级片 | 免费av不卡 | 女人av在线| 国产小视频在线播放 | 黄色成人在线 | 国产精品黄 | 超碰成人福利 | 欧美一区二区三区视频 |