tfs
2010-06-26 17:19:42 UTC
In my MFC extended dll, during DLL_PROCESS_DETACH I add a clean-up function
to exit all worker-threads (created by AfxBeginThread(ThreadProc)). But if I
use WaitForSingleObject() to wait for the BG-thread to exit,
WaitForSingleObject() never returns:
e.g.
CWinThread* pBGThread = < previously created by AfxBeginThread(ThreadProc,
...); >
HANDLE hThx = pBGThread->m_hThread;
// signal the ThreadProc() to exit
::WaitForSingleObject(hThx, INFINITE); // never returns, even though I've
debug-traced that ThreadProc() had already exited
However, if I use ::GetExitCodeThread() instead of ::WaitForSingleObject():
DWORD dwExitCode = 0;
while (::GetExitCodeThread(hThx, &dwExitCode)) {
if (dwExitCode != STILL_ACTIVE) break;
::Sleep(10);
dwExitCode = 0;
}
at least it doesn't hang forever, but that is because ::GetExitCodeThread()
returns FALSE, and I do not have the required thread exit-code retrieved (I
deliberately put return 100 in ThreadProc(), but dwExitCode is 0).
Are the above problems simply because when the dll is in DLL_PROCESS_DETACH
state?
to exit all worker-threads (created by AfxBeginThread(ThreadProc)). But if I
use WaitForSingleObject() to wait for the BG-thread to exit,
WaitForSingleObject() never returns:
e.g.
CWinThread* pBGThread = < previously created by AfxBeginThread(ThreadProc,
...); >
HANDLE hThx = pBGThread->m_hThread;
// signal the ThreadProc() to exit
::WaitForSingleObject(hThx, INFINITE); // never returns, even though I've
debug-traced that ThreadProc() had already exited
However, if I use ::GetExitCodeThread() instead of ::WaitForSingleObject():
DWORD dwExitCode = 0;
while (::GetExitCodeThread(hThx, &dwExitCode)) {
if (dwExitCode != STILL_ACTIVE) break;
::Sleep(10);
dwExitCode = 0;
}
at least it doesn't hang forever, but that is because ::GetExitCodeThread()
returns FALSE, and I do not have the required thread exit-code retrieved (I
deliberately put return 100 in ThreadProc(), but dwExitCode is 0).
Are the above problems simply because when the dll is in DLL_PROCESS_DETACH
state?