Thanks for the help! I will try the example in the link you sent.
Thanks for the help! I will try the example in the link you sent.
I have implemented my thread according to: http://mayaposch.wordpress.com/2011/...l-explanation/ and it works fine.
If I would like to stop my thread from the main thread (when the user presses Cancel) how shall I do that?
It depends on what your thread is doing, but something like:
requestStop() can be called to schedule an orderly shut down of the thread.Qt Code:
Q_OBJECT public: Worker(): stopRequested(false) { } ~Worker() { } public slots: void process() { while (1) { sleep(1); // fake some uninteruptable work unit if (stopRequested) break; } emit finished(); } public: void requestStop() { stopRequested = true; } signals: void finished(); private: QMutex mutex; bool stopRequested; };To copy to clipboard, switch view to plain text mode
OK Thanks.
I have decided that i whould like to wait until may thread has finished instead of exiting the thread when the user presses Cancel. How can i implement that?
Qt Code:
//Slot connected to finished signal of the worker class void a::threadFinished() { m_finished = true; } void a::cancelPressed() { //Here I would like to wait until the thread is finished. bool stopWaiting = false; while(!stopWaiting) { m_mutex.lock(); if (m_finished) stopWaiting = true; m_mutex.unlock(); //How shall I implement the delay (if stopWaiting is false) before i continue another loop in the whileloop without locking the entire program? } //Do som stuff after the thread has finished. }To copy to clipboard, switch view to plain text mode
Both the worker and the QThread issue a finished() signal when they are done. Connect to that signal and put your post-processing in the slot. Then you don't have to block the Qt event loop and GUI update.
But I whould like to exit the entire program when the user presses Cancel. I would like to make sure the threads are finished before I exit the program.
SepticInsect (12th September 2012)
Thanks everyone for the help! The problem is solved!
Bookmarks