gpt4 book ai didi

multithreading - 停止由 QtConcurrent::run 启动的线程?

转载 作者:行者123 更新时间:2023-12-03 12:43:51 26 4
gpt4 key购买 nike

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

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

在 startLiveCapturing-Method 内部,一个无限循环正在运行,它捕获图像并显示它们。因此,如果用户想要停止该过程,他只需按下一个按钮即可停止该操作。
但似乎我不能通过调用这样的取消方法来停止这个线程?
this->cameraThreadRepresentation.cancel();

我做错了什么以及如何停止该线程或操作。

最佳答案

来自 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.



您可以做的是按下按钮在主窗口中设置一个 bool 标志,并像这样构建无限循环:
_aborted = false;

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

// do your actual work here
}

关于multithreading - 停止由 QtConcurrent::run 启动的线程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18845579/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com