PDA

View Full Version : Thread started from main does send signal back.



edxxgardo
20th July 2011, 14:02
Hello everybody, I need your help with Threads ...

in the main.cpp file I start a main window, do stuff, ecc ...and a QThread to do some processing in parallel. When I exit from the menu or close the main window, in closeEvent() I send a signal to the thread to set a variable to stop the thread. The signal is received in the thread and the run() method stop working (checked). But, the signal finished() I set to be sent back to the main (once inside closeEvent()) and then check that the thread stopped, is not received. Any idea why? thx in advance,

mcosta
20th July 2011, 14:20
Probably after the closeEvent returns the eventLoop stop running and the exec returns.

MarekR22
20th July 2011, 14:25
show me your code

edxxgardo
20th July 2011, 14:36
Thx for your replay. Actually, what I did was to set a variable in the thread and in the main window. When I reach the closeEvent(), I emit a signal to sent a bool value to the thread to say "stop", and it works, I check it. But, in the same way, the thread should send back a signal (finished()) saying "I stopped" . This signal should set in the main window (via slot) another variable saying so, and it works too. But, my mistake is that I put a while(!stoppedThread); in the closeEvent() and the signal is not received. Which means that the stoppedThread variable is not set in the main window with the signal received back from the thread. What I did was to disable the while loop, put a Message Box (with an ok button) just to try and exit directly. Now the signal from the thread is received, the variable set and the thread stopped.
Of course, there should be better ways to do it. I still have to see what to do instead of the message box before exiting. I'll have a look. If you can suggest something more efficient, it is welcome. Thx again,