QThread发出finished()信号,但isRunning()返回true和isFinished()返回false [英] QThread emits finished() signal but isRunning() returns true and isFinished() returns false

查看:5774
本文介绍了QThread发出finished()信号,但isRunning()返回true和isFinished()返回false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的qthread实现的代码。我试图从卫星获得GPS数据。即使程序退出 gpsSearch() slot函数,QThread也不会产生finished()信号。每当单击按钮时,将调用函数 locateMe()。第一次当线程未启动并且点击按钮时,它为 isRunning()函数输出true值,并为打印false值isFinished / code>函数。我不得不调用QTherad的 quit()函数来手动停止线程。之后,它进入 gnssProvider 类中的连接的 threadQuit()函数。但是,即使之后,如果我点击按钮它打印真正的价值 isRunning 和假的 isFinished() code> locateMe()函数。

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 run()调用 exec()
  5. code>。
  6. exec()进入事件循环并保持到 $ exit()

  7. run()返回到内部。
  8. 应该开始返回true和 isRunning() false。
  9. 内部发出
  10. 线程终止为真实的。
  11. li>
  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() slot。

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆