最近在写多线程的应用,事件来,事件去的,非常麻烦。
每个Event,都要初始化、释放、Set/Reset;一点点修改,就要改很
多处;搞得我头都大了。
今天写了个包装class,共享之:
#ifndef _MyEventHelp_h_DATE_2008_04_28_TIME_20_57_05_
#define _MyEventHelp_h_DATE_2008_04_28_TIME_20_57_05_
#include <windows.h>
class CMyEventHelp
{
public:
CMyEventHelp(
LPSECURITY_ATTRIBUTES lpEventAttributes = NULL,
BOOL bManualReset = FALSE,
BOOL bInitialState = FALSE,
LPCSTR lpName = NULL
)
: m_hEvent(NULL)
{
init(lpEventAttributes, bManualReset, bInitialState, lpName);
}
~CMyEventHelp()
{
clear();
}
public:
HANDLE getHandle()const {return m_hEvent;}
bool set() {return (FALSE != ::SetEvent(m_hEvent));}
bool reset() {return (FALSE != ::ResetEvent(m_hEvent));}
HANDLE init(
LPSECURITY_ATTRIBUTES lpEventAttributes = NULL,
BOOL bManualReset = FALSE,
BOOL bInitialState = FALSE,
LPCSTR lpName = NULL
)
{
clear();
m_hEvent = ::CreateEvent(lpEventAttributes, bManualReset, bInitialState, lpName);
return m_hEvent;
}
void clear()
{
if(NULL != m_hEvent)
{
::CloseHandle(m_hEvent);
m_hEvent = NULL;
}
};
DWORD wait(DWORD dwTimeOut) const
{
return ::WaitForSingleObject(m_hEvent, dwTimeOut);
}
protected:
HANDLE m_hEvent;
};
#endif // _MyEventHelp_h_DATE_2008_04_28_TIME_20_57_05_