没有废话直接看代码
///////////////////////////////////////H/////////////////////////////////////////////////
#ifndef LGLIB_EVENT_H
#define LGLIB_EVENT_H
namespace lglib
{
class __LGLIB__ lg_Event
{
private: // 私有数据
HANDLE m_hCond;
public: // 公用数据
enum
{
EVENT = 0, // 事件发生
TIME_OUT, // 超时
EVENT_ERR // 发生错误
};
public: // 构造
lg_Event();
virtual ~lg_Event();
public: // 方法
/*!
* 重置事件状态,在一个事件发生后必须重置才能继续使用。
*/
void reset( void );
/*!
* 事件通知,像等待事件的线程发送这个事件。
*/
void set( void );
/*!
* 等待事件通知
*/
int wait( void );
/*!
* 等待事件一段时间
*/
int wait( uint32 timeout );
};
}
#endif
///////////////////////////////////////CPP/////////////////////////////////////////////////////////
namespace lglib
{
//---------------------------------------------------------------------------
// 构造
lg_Event::lg_Event()
{
m_hCond = CreateEvent( NULL, TRUE, FALSE, NULL );
if ( m_hCond == NULL )
{
throw( this );
}
}
//---------------------------------------------------------------------------
// 析构
lg_Event::~lg_Event()
{
::CloseHandle( m_hCond );
}
//---------------------------------------------------------------------------
// 设置事件通知
void lg_Event::set( void )
{
::SetEvent( m_hCond );
}
//---------------------------------------------------------------------------
// 重置事件通知
void lg_Event::reset( void )
{
::ResetEvent( m_hCond );
}
//---------------------------------------------------------------------------
// 等待事件通知
int lg_Event::wait( void )
{
if ( ::WaitForSingleObject( m_hCond, INFINITE ) == WAIT_OBJECT_0 )
{
return EVENT;
}
else
{
return EVENT_ERR;
}
}
//---------------------------------------------------------------------------
// 等待事件通知
int lg_Event::wait( uint32 timeout )
{
int ret = WaitForSingleObject( m_hCond, timeout );
switch( ret )
{
case WAIT_OBJECT_0:
{
return EVENT;
}
case WAIT_TIMEOUT:
{
return TIME_OUT;
}
case WAIT_FAILED:
{
return EVENT_ERR;
}
}
return ret;
}
}