atomicgs12
asked on
How to manage threads
I have an application that has 20+ threads in it. When I do a WM_CLOSE on the main .exe it closes the dialog but it will not close down, I still see two threads running, one that is my main .exe and another that I can only see the thread id from. I am assuming this open thread is preventing my main .exe from closing completly. So basically I have two questions. 1. is there some way to force my app closed, dynamically and 2. are there some good techniques/examples of how to manage threads, e.g. keep track of the threads, who opens, who is closing, how many are running and etc. I am working on both window 32 and windows ce using vs2005
Thanks
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Forcing to finish all process can be called with ExitProcess(DWORD exitCode);
but
prepare your thread for managed finishing, not for forced termination. For example,
in your thread loop used WaitForMultipleObjects(...
Thread which finishes other thread:
SetEvent(stopEvent);
DWORD waitRes = WaitForSingleObject(thread
if(waitRes == 0)
In thread which calls SetEvent(), place for example WaitForSingleObject(thread
and if return value is 0, your thread finishes correctly. If expires 3 seconds (by parameter 3000),
return value will be WAIT_TIMEOUT and after it call TerminateThread(threadHand
Else you have option to call TerminateThread(threadHand