我在WinApp中声明了一个事件句柄test_evt,并在构造函数中创建它
test_evt = ::CreateEvent(NULL,TRUE,FALSE,"evt");
if (test_evt == NULL)
{
AfxMessageBox("create event failed");
}然后自WinApp的Run里面等待test_evt被激发
int CMsgwaitApp::Run() 
{
// TODO: Add your specialized code here and/or call the base class
if (WAIT_OBJECT_0 == ::MsgWaitForMultipleObjectsEx(1,&test_evt,INFINITE,QS_ALLINPUT,MWMO_INPUTAVAILABLE 
))
{
AfxMessageBox("ok");
} return CWinApp::Run();
}
然后重写对话框的OK按钮处理函数
void CMsgwaitDlg::OnOK() 
{
// TODO: Add extra validation here
HANDLE  h = ::OpenEvent(EVENT_ALL_ACCESS,FALSE,"evt");
::SetEvent(h);
// CDialog::OnOK();
}
为什么按下OK,却没有消息框弹出?

解决方案 »

  1.   

    判断OpenEvent是否成功,如果失败,用GetLastError获取原因
      

  2.   

    不是这个原因,我后来又改写了Run函数
    int CMsgwaitApp::Run() 
    {
        // TODO: Add your specialized code here and/or call the base class
            AfxMessageBox("ok");    return CWinApp::Run();
    }
    发现根本不会跳出消息框
      

  3.   

    如果你这个是个对话框程序,不会进到Run里面,你看下InitInstance里面就是dlg的DoModal操作了,是对话框来接管了消息循环,轮不到你的app的run了。