在主线程中慎用WaitForSingleObject (WaitForMultipleObjects)
2008-02-29 15:08
871 查看
使用多线程时工作线程中调用的API可能会使用到消息循环来完成任务,如果在主线程中使用WaitForSingleObject或WaitForMultipleObjects,,那么程序会crash。
解决办法:
1、
MSG msg;
while (TRUE)
{
if(WAIT_OBJECT_0 == MsgWaitForMultipleObjects(1, &m_pThread->m_hThread,
FALSE, INFINITE, QS_ALLEVENTS))
{
break;
}
else
{
while (PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}
2、
BOOL WaitWithMessageLoop(HANDLE hEvent, int nTimeout)
{
DWORD dwRet;
while (1)
{
// wait for event or message, if it's a message, process it and return to waiting state
dwRet = MsgWaitForMultipleObjects(1, &hEvent, FALSE, nTimeout, QS_ALLINPUT);
if (dwRet == WAIT_OBJECT_0)
{
//WaitWithMessageLoop() event triggered.
return TRUE;
}
else
if (dwRet == WAIT_OBJECT_0 + 1)
{
// process window messages
AfxGetApp()->PumpMessage();
}
else
if (dwRet == WAIT_TIMEOUT)
{
// timed out !
return FALSE;
}
else
{
// WAIT_ABANDONED_0 ...
return TRUE;
}
}
}
实例:
Windows Media Encoder中IWMEncoder类PrepareToEncode(VARIANT_FALSE)时会使用到主线程的消息循环,因此在主线程中使用WaitForSingleObject等待工作线程的结束,程序会卡在PrepareToEncode(VARIANT_FALSE)这一步死掉。
解决办法:
1、
MSG msg;
while (TRUE)
{
if(WAIT_OBJECT_0 == MsgWaitForMultipleObjects(1, &m_pThread->m_hThread,
FALSE, INFINITE, QS_ALLEVENTS))
{
break;
}
else
{
while (PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}
2、
BOOL WaitWithMessageLoop(HANDLE hEvent, int nTimeout)
{
DWORD dwRet;
while (1)
{
// wait for event or message, if it's a message, process it and return to waiting state
dwRet = MsgWaitForMultipleObjects(1, &hEvent, FALSE, nTimeout, QS_ALLINPUT);
if (dwRet == WAIT_OBJECT_0)
{
//WaitWithMessageLoop() event triggered.
return TRUE;
}
else
if (dwRet == WAIT_OBJECT_0 + 1)
{
// process window messages
AfxGetApp()->PumpMessage();
}
else
if (dwRet == WAIT_TIMEOUT)
{
// timed out !
return FALSE;
}
else
{
// WAIT_ABANDONED_0 ...
return TRUE;
}
}
}
实例:
Windows Media Encoder中IWMEncoder类PrepareToEncode(VARIANT_FALSE)时会使用到主线程的消息循环,因此在主线程中使用WaitForSingleObject等待工作线程的结束,程序会卡在PrepareToEncode(VARIANT_FALSE)这一步死掉。
相关文章推荐
- 在主线程中慎用WaitForSingleObject (WaitForMultipleObjects)
- 在主线程中慎用WaitForSingleObject (WaitForMultipleObjects)
- 在主线程中慎用WaitForSingleObject (WaitForMultipleObjects)
- 在主线程中慎用WaitForSingleObject (WaitForMultipleObjects)
- 在主线程中慎用WaitForSingleObject (WaitForMultipleObjects)
- 在主线程中慎用WaitForSingleObject (WaitForMultipleObjects)
- 【转】在主线程中慎用WaitForSingleObject或WaitForMultipleObjects
- 在主线程中慎用WaitForSingleObject (WaitForMultipleObjects)
- 在主线程中慎用WaitForSingleObject (WaitForMultipleObjects)
- 在主线程中慎用WaitForSingleObject (WaitForMultipleObjects)
- 在主线程中慎用WaitForSingleObject (WaitForMultipleObjects)
- FW:在主线程中慎用WaitForSingleObject (WaitForMultipleObjects)
- 在主线程中慎用WaitForSingleObject (WaitForMultipleObjects) --代替方法:MsgWaitForMultipleObjects
- 在主线程中慎用WaitForSingleObject (WaitForMultipleObjects)
- 在主线程中慎用WaitForSingleObject (WaitForMultipleObjects) --代替方法:MsgWaitForMultipleObjects
- 在主线程中慎用WaitForSingleObject (WaitForMultipleObjects)
- 在主线程中慎用WaitForSingleObject (WaitForMultipleObjects) [转载]
- 在主线程中慎用WaitForSingleObject (WaitForMultipleObjects)
- 在主线程中慎用WaitForSingleObject (WaitForMultipleObjects)
- 主线程使用WaitForSingleObject和MsgWaitForMultipleObjects等待线程返回值