Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Win32 (http://www.go4expert.com/forums/win32/)
-   -   How to let a user cancel a thread at any time. (http://www.go4expert.com/forums/user-cancel-thread-time-t3070/)

ever_thus 20Feb2007 00:05

How to let a user cancel a thread at any time.
 
Here's a problem I encounter very often in GUI programming and I'm not even sure how to google it.

A task is executing. While it does so I display a dialog that tells the user what is going on. The dialog has a cancel button. Since I can't be in the message loop while executing the given task, the dialog executes in its own thread.

The problem is, how does the task know when the user has hit the cancel button. I don't want to have keep polling an event. What's more, sometimes there is a separate error handling function (when the main function is retrying the task several times) which the function executing the task shouldn't know about.

I'm sure there must be a simple elegant solution to such situations. If anyone can help I'd be grateful.

shabbir 20Feb2007 09:15

Re: How to let a user cancel a thread at any time.
 
The normal way of doing it or more accuartely the way I do it is.

I start a thread in the Message Loop and then return so that the Message Loop is not blocked and use the MsgWaitForSingleObject for the completion of the thread.

Now in the body of the thread there is a check for a variable which just stops the running thread and sets the event so that the wait is over and the job is cancelled.

ever_thus 21Feb2007 00:14

Re: How to let a user cancel a thread at any time.
 
Quote:

I start a thread in the Message Loop and then return
I'm not sure I understand. The message loop runs countless times and we don't want to start a thread in each iteration. Also what do you return from; CreateThread returns immediately.

Could you possibly post the psuedocode?

shabbir 21Feb2007 09:11

Re: How to let a user cancel a thread at any time.
 
Quote:

Originally Posted by ever_thus
I'm not sure I understand. The message loop runs countless times and we don't want to start a thread in each iteration. Also what do you return from; CreateThread returns immediately.

Could you possibly post the psuedocode?

What I meant by the message loop and return is that in the event handler of the message I create the thread and return and so that the other messages just dont get blocked.

ever_thus 26Feb2007 04:13

Re: How to let a user cancel a thread at any time.
 
Thanks for your reply. I didn't know about the MsgWaitForMultipleObjects API. What I've done is create a thread in the error handler, pass it an event, have the thread create a dialog and, in the main thread, poll the event regularly. In the dialog thread the thread uses MsgWaitForMUltipleObjects to process messages while polling the event. If the user cancels the dialog thread sets the event. If the operation succeeds the main thread sets the event which the dialog thread then ends the dialog and thread.

shabbir 26Feb2007 10:10

Re: How to let a user cancel a thread at any time.
 
Thats the perfect way of doing it. Or at least the best I know off.


All times are GMT +5.5. The time now is 02:43.