Win7 - worker thread freeze on call to WaitForSingleObject
-
Hi All. I call the following when I need to save a report in my MFC base program: m_pEndThread = AfxBeginThread(Save2CWGCoilRep, &m_CoilRep[iCRepPos], THREAD_PRIORITY_NORMAL,0,0); UINT Save2CWGCoilRep(LPVOID pParam) { I use CStdioFile to them save data to file } Then before I save another report, I will call if(m_pEndCoilThread != NULL) ::WaitForSingleObject(m_pEndCoilThread->m_hThread, INFINITE); to see if the thread is finished before calling again to save another report. And if not just wait. It seems in Win7 now, this call will not exit so that the program will just freeze. It does not freeze all the time, seems random. In WinXP, the same program seems not to have this problem. I am not sure what I am missing. Any help or suggestion will be appreciated. Thanks! Stan the man
-
Hi All. I call the following when I need to save a report in my MFC base program: m_pEndThread = AfxBeginThread(Save2CWGCoilRep, &m_CoilRep[iCRepPos], THREAD_PRIORITY_NORMAL,0,0); UINT Save2CWGCoilRep(LPVOID pParam) { I use CStdioFile to them save data to file } Then before I save another report, I will call if(m_pEndCoilThread != NULL) ::WaitForSingleObject(m_pEndCoilThread->m_hThread, INFINITE); to see if the thread is finished before calling again to save another report. And if not just wait. It seems in Win7 now, this call will not exit so that the program will just freeze. It does not freeze all the time, seems random. In WinXP, the same program seems not to have this problem. I am not sure what I am missing. Any help or suggestion will be appreciated. Thanks! Stan the man
You may ran into a race condition. A common way to check if a thread has been terminated (ensure that
STILL_ACTIVE
is not returned by your thread):if (m_pEndCoilThread != NULL &&
m_pEndCoilThread->m_hThread != INVALID_HANDLE_VALUE)
{
DWORD dwExitCode = 0;
::GetExitCodeThread(m_pEndCoilThread->m_hThread, &dwExitCode);
if (dwExitCode == STILL_ACTIVE)
::WaitForSingleObject(m_pEndCoilThread->m_hThread, INFINITE);
m_pEndCoilThread = NULL;
} -
Hi All. I call the following when I need to save a report in my MFC base program: m_pEndThread = AfxBeginThread(Save2CWGCoilRep, &m_CoilRep[iCRepPos], THREAD_PRIORITY_NORMAL,0,0); UINT Save2CWGCoilRep(LPVOID pParam) { I use CStdioFile to them save data to file } Then before I save another report, I will call if(m_pEndCoilThread != NULL) ::WaitForSingleObject(m_pEndCoilThread->m_hThread, INFINITE); to see if the thread is finished before calling again to save another report. And if not just wait. It seems in Win7 now, this call will not exit so that the program will just freeze. It does not freeze all the time, seems random. In WinXP, the same program seems not to have this problem. I am not sure what I am missing. Any help or suggestion will be appreciated. Thanks! Stan the man
From the code example I take it that you create the Save2CWGCoilRep thread from the UI thread, i.e. from a menu or button handler function. My guess then is that you have a deadlock: 1. The background thread sends messages to the UI thread. Maybe it tries to display a message box, but it could be anything really. 2. The UI thread is suspended in WaitForSingleObject and can't process the message. It's risky to suspend the UI thread as you don't know when or why messages are posted to it. I would instead protect the critical parts of Save2CWGCoilRep with a critical section and take away the WaitForSingleObject call in the UI thread.
-
You may ran into a race condition. A common way to check if a thread has been terminated (ensure that
STILL_ACTIVE
is not returned by your thread):if (m_pEndCoilThread != NULL &&
m_pEndCoilThread->m_hThread != INVALID_HANDLE_VALUE)
{
DWORD dwExitCode = 0;
::GetExitCodeThread(m_pEndCoilThread->m_hThread, &dwExitCode);
if (dwExitCode == STILL_ACTIVE)
::WaitForSingleObject(m_pEndCoilThread->m_hThread, INFINITE);
m_pEndCoilThread = NULL;
}Hi Jochen. Thanks. I will try it out.... Stan
-
Hi All. I call the following when I need to save a report in my MFC base program: m_pEndThread = AfxBeginThread(Save2CWGCoilRep, &m_CoilRep[iCRepPos], THREAD_PRIORITY_NORMAL,0,0); UINT Save2CWGCoilRep(LPVOID pParam) { I use CStdioFile to them save data to file } Then before I save another report, I will call if(m_pEndCoilThread != NULL) ::WaitForSingleObject(m_pEndCoilThread->m_hThread, INFINITE); to see if the thread is finished before calling again to save another report. And if not just wait. It seems in Win7 now, this call will not exit so that the program will just freeze. It does not freeze all the time, seems random. In WinXP, the same program seems not to have this problem. I am not sure what I am missing. Any help or suggestion will be appreciated. Thanks! Stan the man