Link to home
Start Free TrialLog in
Avatar of atomicgs12
atomicgs12Flag for United States of America

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
Avatar of peter41
peter41
Flag of Slovakia image

1. yes,
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(....) where first event is created on application startup. When application finishes, set this event by SetEvent(evHandle) and your loops finishes correctly.
Thread which finishes other thread:
SetEvent(stopEvent);
DWORD waitRes = WaitForSingleObject(threadHandle, 3000);
if(waitRes == 0)
In thread which calls SetEvent(), place for example WaitForSingleObject(threadHandle, 3000
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(threadHandle, exitCode);
Else you have option to call TerminateThread(threadHandle);, but keep in mind, that depending on organizing of your thread code it may bring problems (crash) if your thread function is not written right.
ASKER CERTIFIED SOLUTION
Avatar of peter41
peter41
Flag of Slovakia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial