问题描述
我有一个c ++代码,我在按下按钮时创建一个线程。当我推两次我有多线程并且我试验一个线程如何等待WaitForSingleObject()应用于一个Event对象。
如果我将Event对象定义为静态,构造函数中的CreateEvent()方法和OpenEvent在实现线程的函数的开头,一切似乎都没问题。
但是如果我在其他地方定义Event对象,非静态,似乎OpenEvent在函数的开头虽然OpenEvent返回非null,但实现该线程的线程不能正常工作并且线程不会等待另一个线程。
HANDLE hevent0;
hevent0 = CreateEvent(NULL,TRUE,TRUE,_T(hevenim));
...
HANDLE hevent;
hevent = OpenEvent(EVENT_ALL_ACCESS,TRUE,_T(hevenim));
rwait = WaitForSingleObject(hevent,9999);
我的问题是为什么OpenEvent在第二种情况下无法正常工作?
I have a c++ code where I create a thread when I push a button. When I push twice I have multithreading and I experiment how a thread waits for another by WaitForSingleObject() which apply on an Event object.
If I define the Event object as static, CreateEvent() in the constructor method, and OpenEvent at the begining of the function that implements the thread, all seems to be OK.
But if I define the Event object elsewhere, nonstatic, seems that the OpenEvent at the begining of the function that implements the thread doesn’t work properly and the thread doesn’t wait one for another,although the OpenEvent returns non null.
HANDLE hevent0;
hevent0=CreateEvent(NULL,TRUE,TRUE,_T("hevenim"));
…
HANDLE hevent;
hevent=OpenEvent(EVENT_ALL_ACCESS,TRUE, _T("hevenim"));
rwait=WaitForSingleObject(hevent,9999);
My question is why the OpenEvent doesn’t work properly in the second case?
推荐答案
这篇关于OpenEvent和多线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!