use QThread::wait() to wait for threads to complete before exiting.
use QThread::wait() to wait for threads to complete before exiting.
We can't solve problems by using the same kind of thinking we used when we created them
i am using the following where i want to kill the thread :
MyThread.wait();
MyThread.exit();
What i have observed is that i still need to put a sleep() or a while loop between wait() and exit().
If i dont use the same then i still get the same error.
Is this the right way??
When playing with Qthread I often use the following code in the destructor :
Qt Code:
while ( isRunning() ) quit();To copy to clipboard, switch view to plain text mode
Current Qt projects : QCodeEdit, RotiDeCode
It might not apply to your job, but...
if you need to break off your job (stopping the thread in the middle of it's work) you can use a 'bool keepRunning' member variable in the threaded function.
run(){
keepRunning = true;
do{
//whatever - some short job
}while(someCondition&&keepRunning);
Then sub-class quit to look like
void AutomationThread::quit()
{
keepRunning = false;
QThread::quit();
}
Then you can stop the thread with
myThread->quit();
myThread->wait();//this will wait a maximum of one loop iteration.
Not always applicable, but I use that method all the time - my threads can run for hours (monitoring things, waiting)
by the way, a long sleep in my thread then looks like
for(int i=0; i<someLargeWait&&keepRunning; ++i){
msleep(100);
}
which can also be broken off quickly.
K
Sheetal (29th March 2007)
Bookmarks