PDA

View Full Version : QtConcurrent pause



MarKac
19th April 2013, 14:42
Hi,

I'm having problems with QtConcurrent, how to pause execution of QtConcurrent function (thread) ?
I've tried QFuture setPaused(true) and pause() but that does not work. Let say I have code like this:



void MainWindow::on_pushButton_clicked() // run button
{
future = QtConcurrent::run(this, &MainWindow::my_func);
watcher.setFuture(future);
}

void MainWindow::on_pushButton_2_clicked() //pause button
{
future.pause();
return;
}

void MainWindow::my_func()
{
qDebug()<< "time-consuming code start";
for (int i = 0; i < 8000; i++)
{
for (int j = 1; j< 1000; j++)
{
sum = sum * i / j;
QApplication::processEvents();

}
}
qDebug()<< "time-consuming code end";
}


When I press pause button my_func() still continues, thread is not paused. what am I doing wrong, how to pause execution of my_func ?
Thanks in advance.

Santosh Reddy
19th April 2013, 14:50
You cannot stop the thread.

Qt Doc Say's

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.

anda_skoa
23rd April 2013, 14:49
And better not use a member function in a UI class, you could be tempted to do UI stuff in it and that would not be OK with a secondary thread.
Also remove the QApplication::processEvent(), the worker thread should not do that either.

Cheers,
_