Hello everyone,
I am trying to make some parts of my app run in a different thread than th UI thread, meanwhile I am trying to use a timer to show the elapsed time on the form.
For this I used the QConcurrent::run method like this :
Qt Code:
  1. void frmCustomNetwork::on_btnTrain_clicked()
  2. {
  3. try
  4. {
  5. if( P.size() == 0 || T.size() == 0)
  6. {
  7. QMessageBox::warning(this,"No Sample for training is set","You need to specify some samples with their respective desired outputs first");
  8. return;
  9. }
  10.  
  11.  
  12. elapsedSeconds=0;
  13. timer->start();
  14. connect(&watcher,SIGNAL(finished()),this,SLOT(on_TimerTick()));
  15. future = QtConcurrent::run(this,&frmCustomNetwork::Rec,ui->chkPlot->isChecked());
  16. watcher.setFuture(future);
  17.  
  18. }
  19. catch(std::exception ex)
  20. {
  21. QMessageBox::critical(this,"Exception occured in on_btnTrain_clicked()",ex.what());
  22. }
  23. catch(...)
  24. {
  25. QMessageBox::critical(this,"Exception occured on_btnTrain_clicked()","Uknown Exception occured");
  26. }
  27. }
To copy to clipboard, switch view to plain text mode 

and this is the timerTick slot:
Qt Code:
  1. void frmCustomNetwork::on_TimerTick()
  2. {
  3. elapsedSeconds++;
  4. if(watcher.isFinished())
  5. {
  6. timer->stop();
  7. elapsedSeconds = 0;
  8. return;
  9. }
  10.  
  11. day = elapsedSeconds / (24*3600);
  12. hour = (elapsedSeconds % (24*3600)) / 3600 ;
  13. min = ((elapsedSeconds % (24*3600)) % 3600) / 60;
  14. second = ((elapsedSeconds % (24*3600)) % 3600) % 60;
  15.  
  16. ui->lblElapsedTime->setText(QString::number(day)+" : "+QString::number(hour)+" : "+QString::number(min)+" : "+QString::number(second));
  17. }
To copy to clipboard, switch view to plain text mode 

the problem that I am facing now is :
1.Is it the correct way of using QConcurent in first place?
2.How can I destroy the running thread ( the one spawned by QConcurrect::run()) when the window closes ( that is when the Rec() is not finished yet and is being executed in a separate thread, I suddently decide to close the app)

at the moment I am facing random crashes which I guess pretty much is because of the way I am creating and dealing with the new thread + timer.
I would be grateful If anyone could give me a hand in this
Thanks in advance