本文介绍了停止由QtConcurrent :: run启动的线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过其关联的QFuture对象停止线程?目前,我一直在开始像这样的视频捕获过程.

Is it possible to stop a Thread by its associated QFuture Object ?Currently i've been starting a video capturing process like this.

this->cameraThreadRepresentation = QtConcurrent::run(this,&MainWindow::startLiveCapturing);

在startLiveCapturing-Method内部,一个无限循环正在运行,该循环捕获图像并显示它们.因此,如果用户要停止该过程,则只需按一个按钮,该操作就会停止.但似乎我无法通过调用像这样的cancel方法来停止此线程?

Inside the startLiveCapturing-Method an infinite loop is running that captures images and displays them. So if the user wants to stop that process he should simply press a button and that operation stops.But it seems that i can not stop this thread by calling the cancel method like this ?

this->cameraThreadRepresentation.cancel();

我做错了什么,如何停止该线程或操作.

What i am doing wrong and how can i stop that thread or operation.

推荐答案

来自 QtConcurrent的文档:: run :

您可以做的是在主窗口中按下按钮并设置一个布尔标志,然后像下面那样构建无限循环:

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
}

这篇关于停止由QtConcurrent :: run启动的线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 18:51