Clean up QThread after calling quit()
I have a problem. If I call Abort(), run function will return without complexMath instance have enough time to do clean up.
What i want is, after calling Abort(), complexMath instance have enough time to shutdown itself, clearing all pending signal and slot(inside complexMath, it also have it own signal and slots) before it return.
void MyThread::Go(){
start();
}
void MyThread::Abort(){
emit stopNow();
quit();
}
void MyThread::run(){
ComplexMath * complexMath = new ComplexMath();
connect( complexMath, SIGNAL(OnCalculation(qint)), this, SLOTS(PartialOutput(qint)) );
connect( this, SIGNAL(stopNow()), complexMath, SLOTS(deleteLater());
exec();
}
void MyThread::PartialOutput(qint data){
qDebug() <开发者_开发知识库;< data;
}
Thanks!
I think you can get rid of the stopNow signal:
void MyThread::Abort(){
quit();
}
void MyThread::run(){
ComplexMath * complexMath = new ComplexMath();
connect( complexMath, SIGNAL(OnCalculation(qint)), this, SLOTS(PartialOutput(qint)) );
exec();
// Any code here will be run after the thread quits, and the event loop stops
deleteLater();
}
精彩评论