• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 252
  • Last Modified:

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
0
atomicgs12
Asked:
atomicgs12
  • 2
1 Solution
 
peter41Commented:
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.
0
 
peter41Commented:
Sorry, not finished previous post, read this one:
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) {
// thread finishes correctly
} else
if(waitRes == WAIT_TIMEOUT) {
TerminateThread(threadHandle, 1);// thread ust be terminated due timeout
}
CloseHandle(threadHandle);

but keep in mind, that depending on organizing of your thread code TerminateThread() may bring problems (crash) if your thread code is not written right.
2.
Detecting if thread is running you can do with above
waitRes = WaitForSingleObject(threadHandle, 0);// if returns 0, your thread is finished.
You can also to list all threads belonging to some process, or all thread in system, but it is not needed for your purpose.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now