Hossein
12th October 2015, 08:14
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 :
void frmCustomNetwork::on_btnTrain_clicked()
{
try
{
if( P.size() == 0 || T.size() == 0)
{
QMessageBox::warning(this,"No Sample for training is set","You need to specify some samples with their respective desired outputs first");
return;
}
elapsedSeconds=0;
timer->start();
connect(&watcher,SIGNAL(finished()),this,SLOT(on_TimerTick( )));
future = QtConcurrent::run(this,&frmCustomNetwork::Rec,ui->chkPlot->isChecked());
watcher.setFuture(future);
}
catch(std::exception ex)
{
QMessageBox::critical(this,"Exception occured in on_btnTrain_clicked()",ex.what());
}
catch(...)
{
QMessageBox::critical(this,"Exception occured on_btnTrain_clicked()","Uknown Exception occured");
}
}
and this is the timerTick slot:
void frmCustomNetwork::on_TimerTick()
{
elapsedSeconds++;
if(watcher.isFinished())
{
timer->stop();
elapsedSeconds = 0;
return;
}
day = elapsedSeconds / (24*3600);
hour = (elapsedSeconds % (24*3600)) / 3600 ;
min = ((elapsedSeconds % (24*3600)) % 3600) / 60;
second = ((elapsedSeconds % (24*3600)) % 3600) % 60;
ui->lblElapsedTime->setText(QString::number(day)+" : "+QString::number(hour)+" : "+QString::number(min)+" : "+QString::number(second));
}
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
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 :
void frmCustomNetwork::on_btnTrain_clicked()
{
try
{
if( P.size() == 0 || T.size() == 0)
{
QMessageBox::warning(this,"No Sample for training is set","You need to specify some samples with their respective desired outputs first");
return;
}
elapsedSeconds=0;
timer->start();
connect(&watcher,SIGNAL(finished()),this,SLOT(on_TimerTick( )));
future = QtConcurrent::run(this,&frmCustomNetwork::Rec,ui->chkPlot->isChecked());
watcher.setFuture(future);
}
catch(std::exception ex)
{
QMessageBox::critical(this,"Exception occured in on_btnTrain_clicked()",ex.what());
}
catch(...)
{
QMessageBox::critical(this,"Exception occured on_btnTrain_clicked()","Uknown Exception occured");
}
}
and this is the timerTick slot:
void frmCustomNetwork::on_TimerTick()
{
elapsedSeconds++;
if(watcher.isFinished())
{
timer->stop();
elapsedSeconds = 0;
return;
}
day = elapsedSeconds / (24*3600);
hour = (elapsedSeconds % (24*3600)) / 3600 ;
min = ((elapsedSeconds % (24*3600)) % 3600) / 60;
second = ((elapsedSeconds % (24*3600)) % 3600) % 60;
ui->lblElapsedTime->setText(QString::number(day)+" : "+QString::number(hour)+" : "+QString::number(min)+" : "+QString::number(second));
}
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