How to wait for a thread to complete?
-
In my main GUI thread, I start a new thread and need to wait until the thread is terminated normally. When waiting the thread, I do not want the main GUI thread loses the reponses, just like WaitForMultipleObjects in Windows SDK API. Is there a similar function that can implement this?
Thanks
-
Hi,
It depends on how you started the thread. What class did you use?
For QThread, listen for the QThread::finished() signal. The signal is emitted when the thread stops. Documentation at https://qt-project.org/doc/qt-5/qthread.html
-
You can use something like that:
@QEventLoop loop;
connect(thread, &QThread::finished, &loop, &QEventLoop::quit);
thread->start();
loop.exec();@ -
Hii.
and if you want to stop/finished your thread before quitting from event loop you can use
connect(thread,SIGNAL(finished()),thread,SLOT(deleteLater())); as well