View Full Version : how to terminate a thread when it is running

19th October 2009, 05:33
i use the terminate function in the MainWindow's destruction function. i have add the setTerminationEnabled(true); in the thread's construction function. at the mainwindow's destruction function i invoke it like this: thread->terminate(); but when i close the applicaiton the gui is closed and the application process is still running. .. the thread is blocking waiting client's data. i search the froum and try the solution that i found. my debug log show that it is blocking at the terminate() fucntion. my question is: how to terminate the thread when it is running? any help is appreciate!

19th October 2009, 11:50
In general it is better if you can show the code you are talking about.
Also, it is not a good practice to terminate a thread from outside the thread the way you are doing it.
It is better to set some flag in the thread class, and allow the thread to terminate it self.
You can then connect the finished() or terminated() signals to slots out side the thread to know your thread has terminated, and then continue to close your application.

Also note, the you calling terminate() does not guarantee that the thread indeed is terminated when your window destructor continues, and possibly even finished by its self, before the thread is terminated.
Have a look at doc of QThread.