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

QThread 發出finished() 信號但isRunning() 返回true 并且

QThread emits finished() signal but isRunning() returns true and isFinished() returns false(QThread 發出finished() 信號但isRunning() 返回true 并且isFinished() 返回false)
本文介紹了QThread 發出finished() 信號但isRunning() 返回true 并且isFinished() 返回false的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

下面是我的 qthread 實現代碼.我正在嘗試從衛星獲取 GPS 數據.即使程序退出 gpsSearch() 槽函數,QThread 也不會產生finished() 信號.每當單擊按鈕時都會調用函數 locateMe().第一次當線程未啟動并單擊按鈕時,它會為 isRunning() 函數打印真值,并為 isFinished() 函數打印假值.我不得不調用 QTherad 的 quit() 函數來手動停止線程.之后,它轉到 gnssProvider 類中連接的 threadQuit() 函數.但即使在那之后,如果我點擊按鈕,它會在 locateMe() 函數中為 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. 你調用QThread::start().
  2. 此時,isRunning() 應該開始返回 true.
  3. 線程內部開始.它們發出 started() 信號.
  4. 線程內部調用 run().
  5. 除非你在子類中重寫它,run() 調用 exec().
  6. exec() 進入一個事件循環并保持在那里直到 quit()exit() 被調用.
  7. exec()run() 返回內部.
  8. 此時,isFinished() 應該開始返回 true 和 isRunning() false.
  9. 內部發出 finished() 信號.
  10. 內部人員做了一些最后的清理工作.
  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.

所以你需要在你的位置獲取器完成后調用 quit() - 但是 this->quit() 沒有調用 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.

你的代碼看起來有點像這篇文章之后的模式:

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/

注意她如何給她的工人一個 finished() 信號(與 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.

這篇關于QThread 發出finished() 信號但isRunning() 返回true 并且isFinished() 返回false的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數據?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環: for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環?)
Reusing thread in loop c++(在循環 C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環形?)
主站蜘蛛池模板: 精品视频在线一区 | 亚洲精品电影在线观看 | 久久亚洲国产精品日日av夜夜 | 欧美日韩久久 | 精品久久久网站 | 免费看a | 午夜精品在线观看 | 91视频国产精品 | 久久久久久久一区 | m豆传媒在线链接观看 | 亚洲精品综合 | 日韩av在线一区 | 在线免费观看欧美 | 久久a久久 | 成人在线中文字幕 | 久久久久久网站 | 日韩av免费在线电影 | 亚洲一区二区三区在线播放 | 久久国产精品免费一区二区三区 | 丁香久久 | 成人国产午夜在线观看 | 久久一二| 欧美另类视频 | 日韩在线观看一区二区三区 | 91麻豆产精品久久久久久夏晴子 | 欧美日韩专区 | 亚洲一区二区三区免费 | 欧美精品首页 | 午夜视频网站 | 中文字幕成人网 | 婷婷丁香在线视频 | 精品国产乱码久久久久久图片 | 亚洲五码久久 | 黄色一级大片视频 | 国产一二三视频在线观看 | 成人h动漫亚洲一区二区 | 日韩欧美国产综合 | 日本久久久影视 | 久久精品成人 | 91av视频在线观看 | 精品国产一区二区久久 |