等待线程退出 MsgWaitForMultipleObjects和WaitForSingleObject
2016-11-02 22:56
1101 查看
为什么要用MsgWaitForMultipleObjects?
主要是多线程、防止MFC消息阻塞、监控某些消息...
在MFC多线程中如果要等待工作线程的退出(工作线程可能有控件的使用或者Postmessage)
//草稿
while(TRUE)
{
DWORD dwObject = MsgWaitForMultipleObjects();//QS_ALLEVENTS|QS_ALLINPUT
if(dwObject == WAIT_OBJECT_0)
{
break
}
else
{
MSG msg;
if (PeekMessage(msg,NULL,0,0,
PM_REMOVE|PM_QS_INPUT|PM_QS_PAINT|PM_QS_POSTMESSAGE|PM_QS_SENDMESSAGE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}
为什么不用GetMessage()?
因为GetMessage是取到消息才返回。GetMessage从消息队列中取不到消息,则线程就会被操作系统挂起,等到OS重新调度该线程时
PeekMessagee只是查询消息队列,没有消息就立即返回,从返回值判断是否取到了消息。
自己的想的
//草稿
while(TRUE)
{
DWORD dwObject = WaitForSingleObject(HANDLE hHandle,0);
if (dwObject == WAIT_OBJECT_0)
{
break;
}
MSG msg;
if (PeekMessage(msg,NULL,0,0,
PM_REMOVE|PM_QS_INPUT|PM_QS_PAINT|PM_QS_POSTMESSAGE|PM_QS_SENDMESSAGE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
不建议用Sleep, 因为Sleep虽然不占用CPU但是Sleep占着资源不放。
主要是多线程、防止MFC消息阻塞、监控某些消息...
在MFC多线程中如果要等待工作线程的退出(工作线程可能有控件的使用或者Postmessage)
//草稿
while(TRUE)
{
DWORD dwObject = MsgWaitForMultipleObjects();//QS_ALLEVENTS|QS_ALLINPUT
if(dwObject == WAIT_OBJECT_0)
{
break
}
else
{
MSG msg;
if (PeekMessage(msg,NULL,0,0,
PM_REMOVE|PM_QS_INPUT|PM_QS_PAINT|PM_QS_POSTMESSAGE|PM_QS_SENDMESSAGE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}
为什么不用GetMessage()?
因为GetMessage是取到消息才返回。GetMessage从消息队列中取不到消息,则线程就会被操作系统挂起,等到OS重新调度该线程时
PeekMessagee只是查询消息队列,没有消息就立即返回,从返回值判断是否取到了消息。
自己的想的
//草稿
while(TRUE)
{
DWORD dwObject = WaitForSingleObject(HANDLE hHandle,0);
if (dwObject == WAIT_OBJECT_0)
{
break;
}
MSG msg;
if (PeekMessage(msg,NULL,0,0,
PM_REMOVE|PM_QS_INPUT|PM_QS_PAINT|PM_QS_POSTMESSAGE|PM_QS_SENDMESSAGE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
不建议用Sleep, 因为Sleep虽然不占用CPU但是Sleep占着资源不放。
相关文章推荐
- 主线程使用WaitForSingleObject和MsgWaitForMultipleObjects等待线程返回值
- WaitForSingleObject或MsgWaitForMultipleObjects函数无返回,程序直接退出问题
- 在主线程中慎用WaitForSingleObject (WaitForMultipleObjects) --代替方法:MsgWaitForMultipleObjects
- WaitForSingleObject或MsgWaitForMultipleObjects函数无返回,程序直接退出问题
- 在界面线程不能使用Sleep和WaitForSingleObject之类的函数, 使用 MsgWaitForMultipleObjects
- 在主线程中慎用WaitForSingleObject (WaitForMultipleObjects) --代替方法:MsgWaitForMultipleObjects
- 在界面线程不能使用Sleep和WaitForSingleObject之类的函数, 使用 MsgWaitForMultipleObjects
- 在主线程中慎用WaitForSingleObject (WaitForMultipleObjects)
- WaitForSingleObject和MsgWaitForMultipleObjects
- 在主线程中慎用WaitForSingleObject (WaitForMultipleObjects)
- 在主线程中慎用WaitForSingleObject (WaitForMultipleObjects)
- event与WaitForSingleObject、MsgWaitForMultipleObjects等
- 在用户线程/主线程中推荐MsgWaitForMultipleObjects代替WaitForSingleObject和WaitForMultipleObjects()函数
- 在主线程中慎用WaitForSingleObject (WaitForMultipleObjects)
- 在主线程中慎用WaitForSingleObject (WaitForMultipleObjects)
- FW:在主线程中慎用WaitForSingleObject (WaitForMultipleObjects)
- 在主线程中慎用WaitForSingleObject (WaitForMultipleObjects)
- 在主线程中慎用WaitForSingleObject (WaitForMultipleObjects)
- 在主线程中慎用WaitForSingleObject (WaitForMultipleObjects) [转载]
- 在主线程中慎用WaitForSingleObject (WaitForMultipleObjects)