0
votes

I use a QtConcurrence to run a function in a separated thread but I want to stop, pause or kill this thread but I can't. I read this:

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.

Can I do this in any other way?

My function is:

void MainWindow::on_imprimirButton_clicked()
{
        if(filename.length() == 0){
            ui->consolaBrowser->append("Error. Debe seleccionar un fichero.");
        }else if(!filename.contains(".txt")){
            ui->consolaBrowser->append("Fichero erroneo. Debe seleccionar un archivo de tipo G-CODE.");
        }else{

            imprimiendo=1;

            *future= QtConcurrent::run(Imprimir,filename.toUtf8().data());

            imprimiendo=0;
        }
}
1
you should never need to "kill" a thread. You just need to set a flag somewhere that causes the long running process to stop.paulm

1 Answers

1
votes

I think the QtConcurrence solution is not very nice. It is often suggested, but has no advantage over a good implementation with threading library (e.g. QThread). The sample below shows one possibility to stop your thread. If you set the variable m_bBreak true somewhere in your main program, then the thread stops. In a similar way you can get the current thread progress too.

int foo(bool* bStopper) {
  if(*bStopper)
    return 0;

  // Do code here

  return 1;
}

void QThread::run() {
  m_iErrors = foo(&m_bBreak);
  // Handle errors
}