pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

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 ()?環形?)
主站蜘蛛池模板: 深圳宣传片制作-企业宣传视频制作-产品视频拍摄-产品动画制作-短视频拍摄制作公司 | 胶原检测试剂盒,弹性蛋白检测试剂盒,类克ELISA试剂盒,阿达木单抗ELISA试剂盒-北京群晓科苑生物技术有限公司 | 威海防火彩钢板,威海岩棉复合板,威海彩钢瓦-文登区九龙岩棉复合板厂 | 上海诺狮景观规划设计有限公司| 专注提供国外机电设备及配件-工业控制领域一站式服务商-深圳市华联欧国际贸易有限公司 | 南京试剂|化学试剂|分析试剂|实验试剂|cas号查询-专业60年试剂销售企业 | 塑料检查井_双扣聚氯乙烯增强管_双壁波纹管-河南中盈塑料制品有限公司 | 湖州织里童装_女童男童中大童装_款式多尺码全_织里儿童网【官网】-嘉兴嘉乐网络科技有限公司 | 便携式高压氧舱-微压氧舱-核生化洗消系统-公众洗消站-洗消帐篷-北京利盟救援 | 烘箱-工业烘箱-工业电炉-实验室干燥箱 - 苏州华洁烘箱制造有限公司 | 暴风影音| 北京翻译公司_同传翻译_字幕翻译_合同翻译_英语陪同翻译_影视翻译_翻译盖章-译铭信息 | 粘弹体防腐胶带,聚丙烯防腐胶带-全民塑胶| 技德应用| 吹田功率计-长创耐压测试仪-深圳市新朗普电子科技有限公司 | 仿真植物|仿真树|仿真花|假树|植物墙 - 广州天昆仿真植物有限公司 | 防爆型气象站_农业气象站_校园气象站_农业四情监测系统「山东万象环境科技有限公司」 | 北京易通慧公司从事北京网站优化,北京网络推广、网站建设一站式服务商-北京网站优化公司 | 河南档案架,档案密集架,手动密集架,河南密集架批发/报价 | 北京银联移动POS机办理_收银POS机_智能pos机_刷卡机_收银系统_个人POS机-谷骐科技【官网】 | 电磁辐射仪-电磁辐射检测仪-pm2.5检测仪-多功能射线检测仪-上海何亦仪器仪表有限公司 | 中矗模型-深圳中矗模型设计有限公司| 广东青藤环境科技有限公司-水质检测 | 四合院设计_四合院装修_四合院会所设计-四合院古建设计与建造中心1 | 凝胶成像系统(wb成像系统)百科-上海嘉鹏 | 无纺布包装机|径向缠绕包装机|缠绕膜打包机-上海晏陵智能设备有限公司 | 济南菜鸟驿站广告|青岛快递车车体|社区媒体-抖音|墙体广告-山东揽胜广告传媒有限公司 | 礼仪庆典公司,礼仪策划公司,庆典公司,演出公司,演艺公司,年会酒会,生日寿宴,动工仪式,开工仪式,奠基典礼,商务会议,竣工落成,乔迁揭牌,签约启动-东莞市开门红文化传媒有限公司 | 精密模具加工制造 - 富东懿 | 全国冰箱|空调|洗衣机|热水器|燃气灶维修服务平台-百修家电 | 珠海冷却塔降噪维修_冷却塔改造报价_凉水塔风机维修厂家- 广东康明节能空调有限公司 | 活性炭-果壳木质煤质柱状粉状蜂窝活性炭厂家价格多少钱 | 馋嘴餐饮网_餐饮加盟店火爆好项目_餐饮连锁品牌加盟指南创业平台 | 郑州墨香品牌设计公司|品牌全案VI设计公司 | 在线钠离子分析仪-硅酸根离子浓度测定仪-油液水分测定仪价格-北京时代新维测控设备有限公司 | 浙江工业冷却塔-菱电冷却塔厂家 - 浙江菱电冷却设备有限公司 | 刺绳_刀片刺网_刺丝滚笼_不锈钢刺绳生产厂家_安平县浩荣金属丝网制品有限公司-安平县浩荣金属丝网制品有限公司 | 手表腕表维修保养鉴定售后服务中心网点 - 名表维修保养 | 鄂泉泵业官网|(杭州、上海、全国畅销)大流量防汛排涝泵-LW立式排污泵 | 贴片电感_贴片功率电感_贴片绕线电感_深圳市百斯特电子有限公司 贴片电容代理-三星电容-村田电容-风华电容-国巨电容-深圳市昂洋科技有限公司 | 商标转让-商标注册-商标查询-软著专利服务平台 - 赣江万网 |