Stop Thread started by QtConcurrent::run?

邮差的信 提交于 2019-12-05 11:43:54

From the documentation of QtConcurrent::run:

Note that the QFuture returned by QtConcurrent::run() does not support canceling, pausing, or progress reporting. The QFuture returned can only be used to query for the running/finished status and the return value of the function.

What you could do is have a button press set a boolean flag in your main window and build your infinite loop like this:

_aborted = false;

forever    // Qt syntax for "while( 1 )"
{
    if( _aborted ) return;

    // do your actual work here
}

Why don't you create a boolean flag that you can test inside your capturing loop and when it is set, it jumps out and the thread exits?

Something like:

MainWindow::onCancelClick() // a slot
{
    QMutexLocker locker(&cancelMutex);
    stopCapturing = true;
}

And then for your threaded function:

MainWindow::startLiveCapturing()
{
   forever
   {

       ...
       QMutexLocker locker(&cancelMutex);
       if (stopCapturing) break;
   }

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!