views:

55

answers:

1

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!

A: 

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();
}
Caleb Huitt - cjhuitt