GUI主线程waitforsingleobject的问题?

rotus 2005-04-13 08:00:17
在主线程中用waitforsingleobject,等待子线程结束,为何子线程得不到响应,程序阻塞了。
怎么解决啊。子线程中只是完成一些计算啊
...全文
800 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
nelsonc 2005-04-16
  • 打赏
  • 举报
回复
在主线程用WaitForSingleObject会停止消息循环,应该用MsgWaitForMultipleObject();
参考以下代码。主要思想是在保持消息循环的同时等待事件发生。
int MessageLoop (
HANDLE* lphObjects, // handles that need to be waited on
int cObjects // number of handles to wait on
)
{
// The message loop lasts until we get a WM_QUIT message,
// upon which we shall return from the function.
while (TRUE)
{
// block-local variable
DWORD result ;
MSG msg ;

// Read all of the messages in this next loop,
// removing each message as we read it.
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
// If it's a quit message, we're out of here.
if (msg.message == WM_QUIT)
return 1;
// Otherwise, dispatch the message.
DispatchMessage(&msg);
} // End of PeekMessage while loop.

// Wait for any message sent or posted to this queue
// or for one of the passed handles be set to signaled.
result = MsgWaitForMultipleObjects(cObjects, lphObjects,
FALSE, INFINITE, QS_ALLINPUT);

// The result tells us the type of event we have.
if (result == (WAIT_OBJECT_0 + cObjects))
{
// New messages have arrived.
// Continue to the top of the always while loop to
// dispatch them and resume waiting.
continue;
}
else
{
// One of the handles became signaled.
DoStuff (result - WAIT_OBJECT_0) ;
} // End of else clause.
} // End of the always while loop.
} // End of function.
rotus 2005-04-15
  • 打赏
  • 举报
回复
MsgWaitForSingleObject也试过,没成功,哈哈。
lianglp 2005-04-14
  • 打赏
  • 举报
回复
MsgWaitForSingleObject()试一下。
fanqing 2005-04-14
  • 打赏
  • 举报
回复
同意oyljerry.
主线程会处于等待状态等待WaitForSingleObject的对象激发.
xuzheng318 2005-04-13
  • 打赏
  • 举报
回复
This function returns when the specified object is in the signaled state or when the time-out interval elapses.

DWORD WaitForSingleObject(
HANDLE hHandle,
DWORD dwMilliseconds );
rotus 2005-04-13
  • 打赏
  • 举报
回复
我查过一些资料,是这么说啊。子线程结束+发送消息+主线程响应,试下先。。。。
oyljerry 2005-04-13
  • 打赏
  • 举报
回复
主线程中用WaitForSingleObject会阻塞整个进程的,界面会失去响应
发送消息好了

15,467

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧