Click to See Complete Forum and Search --> : Worker thread question


surender
April 20th, 2004, 06:46 PM
Hi,
I am using plain C and Win32SDK[Visual Studio 6.0].In the GUI i want to give user two options start and Cancel. If they click on start then a seperate thread is created and lengthy operations are performed. When ever they hit on Cancel the thread needs to be terminated.By looking at whether user clicked cancel before executing each statement in my big thread function it is very difficult to know when to terminate the thread. Can you please give me any inputs since i don't have loop logic in my thread.

Thanks
surender

MikeAThon
April 20th, 2004, 08:48 PM
For this approach to work, it is essential that the thread check the "cancel" button periodically. If your thread has a loop, then periodic checking is easier, since the check can be made right in the loop somewhere.

But if the thread doesn't have a loop, it can still check periodically. It's just harder in this case, since you need to sprinkle checks manually thoughout the thread, and liberally enough to make sure that you are checking for the "cancel" button at reasonable intervals. For example, you might check right before making an Internet connection, right after, right before downloading a file, right after, right before parsing the file, etc.

-Mike

kirants
April 21st, 2004, 04:51 PM
As Mike says, there is no other way but to have checks in the worker thread at necessary intervals to check if it is ok to proceed or not.

You could use anything to check for cancel, either some variable or a kernel object..

Is the thread having a message pump, in the sense does it process window messages ? From the first look , it doesn't look like. But if it does, MsgWaitForMultipleObjects could be an option too.

Just my thoughts....