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.