Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop Thread started by QtConcurrent::run?

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);

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.

like image 669
Sebastian Boldt Avatar asked Sep 17 '13 09:09

Sebastian Boldt


People also ask

How do I turn off QtConcurrent?

this->cameraThreadRepresentation = QtConcurrent::run(this,&MainWindow::startLiveCapturing); 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.

What is QFuture?

The state of the computation represented by a QFuture can be queried using the isCanceled(), isStarted(), isFinished(), isRunning(), or isPaused() functions. QFuture is a lightweight reference counted class that can be passed by value. QFuture<void> is specialized to not contain any of the result fetching functions.


1 Answers

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
}
like image 56
Tim Meyer Avatar answered Sep 28 '22 07:09

Tim Meyer