由于无效的句柄,WaitForSingleObject返回等待失败 [英] WaitForSingleObject returns wait failed due to invalid handle
问题描述
I am trying to use a CEvent
to make my thread wait until the message queue is ready as per MSDN's advice so that my PostThreadMessage
function will work correctly.
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时,我收到原因为ERROR_INVALID_HANDLE
的WAIT_FAILED
.为什么会这样?
Unfortunately, it seems that the WaitForSingleObject
isn't doing its job. Sometimes Breakpoint 1 is hit first, sometimes Breakpoint 2. When Breakpoint 2 is hit first, I receive WAIT_FAILED
with the cause ERROR_INVALID_HANDLE
. Why is this happening?
推荐答案
这可能是因为您传递了CEvent
对象而不是其句柄.
That might be because you're passing the CEvent
object instead of its handle.
尝试一下:
WaitForSingleObject(controllerThreadReady.m_hObject, INFINITE);
这篇关于由于无效的句柄,WaitForSingleObject返回等待失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!