我正在尝试使用 CEvent
让我的线程等待,直到消息队列按照 MSDN's advice 准备就绪。这样我的 PostThreadMessage
函数就能正常工作。
BOOL MFC_Thread::InitInstance(){
BOOL worked=CWinThread::InitInstance();
MSG msg;
BOOL result=PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
fingerveinControllerThreadReady->SetEvent();//Breakpoint 1
return TRUE;
}
void init(){
controllerThreadReady=new CEvent(FALSE, FALSE);
CWinThread* thread=AfxBeginThread(RUNTIME_CLASS(MFC_Thread));
controllerThread=thread->m_nThreadID;
WaitForSingleObject(controllerThreadReady, INFINITE);
DoSomething();//Breakpoint 2
}
不幸的是,WaitForSingleObject
似乎没有完成其工作。有时断点 1 首先被命中,有时断点 2。当断点 2 首先被命中时,我收到 WAIT_FAILED
,原因为 ERROR_INVALID_HANDLE
。为什么会发生这种情况?
最佳答案
这可能是因为您传递的是 CEvent
对象而不是其句柄。
试试这个:
WaitForSingleObject(controllerThreadReady.m_hObject, INFINITE);
关于mfc - 由于句柄无效,WaitForSingleObject 返回等待失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4988874/