Quit application from within thread
hi there, i have a application that is quite similar to the threaded fortune server.
So what i have, is a QT application, with QMainWindow in it, that has a QTcpServer running, that starts threads on incomming connections.
What i want to do, is, quit the whole application from within one of the threads started by the server.
I tried different things:
- just call application->quit();
Says that a thread was killed while running, unclean exit
- call QApplication::postEvent(application, new QCloseEvent()) from the end of the threads run() method
- connect(thread, SIGNAL ( finished ) , application, SLOT ( quit ) );
Any ideas how i could exit the application cleanly from within the thread?
You need to add synchronization that waits for all threads to finished before main() exits. If you are sure (to some extend :-) ) that the threads will exit, you can call wait() on the threads that are still running before exiting main.
The event loop (QApplication::exec()) will exit after processing all the events, and not wait for other threads to exit.
Thanx, actually i removed the threads and it turned out that it was some not closed object that was causing the unclean exit ;) but i will return to threads and its good to know
You can call terminate() to all your threads before you exit your program, by reimplementing close event on your main window and insert there a call to terminate all threads.