龙仪的家

导航

<2008年12月>
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

随笔分类

文章分类

收藏夹

随笔档案

文章档案

统计

我的常用网址

代码库之三 -事件

没有废话直接看代码

///////////////////////////////////////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;
 }
}

posted on 2006-04-28 13:00 longest 阅读(503) 评论(0)  编辑 收藏

评论

标题  
姓名  
主页
验证码 *
内容   
  登录  使用高级评论  Top
[使用Ctrl+Enter键可以直接提交]