This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center
                            
                        
                    
                
                                6年前关闭。
            
                    
我已经搜索了论坛,但找不到答案。我是VC ++中编程表单的新手。我所拥有的非常简单。我想显示一个表单,然后等待该表单中的事件,如下所示:

int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
In_opt_ HINSTANCE hPrevInstance,
_In_ LPTSTR CmdLine,
_In_ int nCmdShow)

{

    bool bExit = FALSE;
    Main oForm;

    g_UIThread.g_hUIEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
    g_MainLineThread.g_MainLineEvent = CreateEvent(NULL, FALSE, FALSE, NULL);

    oForm.Show();

    while (!bExit) {
        WaitForSingleObject(&g_UIThread.g_hUIEvent, INFINITE);

          (etc)


问题在于该表单在WaitForSingleObject中时挂起。我看过像MsgWaitForMultipleObjects这样的替代方案,但没有解决方案。

有人可以帮忙吗?我会非常感激。

最佳答案

如您所知,WaitForSingleObject正在等待单个事件。因此,它不处理窗口消息。

while (!bExit) {
  if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) {
      // dispatch the message
  } else if (WaitForSingleObject(&g_UIThread.g_hUIEvent, 0) == WAIT_OBJECT_0) {
      // handle the event
  }
}


如果要对窗口消息使用WaitForMultipleObjects,则应使用QS_ALLINPUT来检查是否已通知事件。 (请注意,您应该在PeekMessage之前致电以下内容)

MsgWaitForMultipleObjects(0, NULL, FALSE, timeout, QS_ALLINPUT) == WAIT_OBJECT_0

关于c++ - VC++:_tWinMain中的WaitForSingleObject期间表格不响应,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17181771/

10-10 18:37