由于无效的句柄,WaitForSingleObject返回等待失败 [英] WaitForSingleObject returns wait failed due to invalid handle

查看:403
本文介绍了由于无效的句柄,WaitForSingleObject返回等待失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用CEvent使我的线程等待,直到按照

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_HANDLEWAIT_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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆