天津城投集团联系电话:Windows API一日一练(45)CreateEvent和SetEvent函数-(2)...

来源:百度文库 编辑:偶看新闻 时间:2024/05/05 03:22:33
#030 
#031  //创建线程
#032  HANDLE CreateThread(void)
#033  {
#034         //创建退出事件。
#035         m_hEventExit = ::CreateEvent(NULL,TRUE,FALSE,NULL);
#036         if (!m_hEventExit)
#037          {
#038               //创建事件失败。
#039               return NULL;
#040         }
#041 
#042         //创建线程。
#043          m_hThread = ::CreateThread( 
#044               NULL,                    //安全属性使用缺省。
#045               0,                         //线程的堆栈大小。
#046               ThreadProc,                 //线程运行函数地址。
#047               this,                      //传给线程函数的参数。
#048               0,                         //创建标志。
#049               &m_dwThreadID);        //成功创建后的线程标识码。
#050 
#051         return m_hThread;
#052  }
#053 
#054  //等待线程结束。
#055  void WaitFor(DWORD dwMilliseconds = INFINITE)
#056  {
#057         //发送退出线程信号。
#058        ::SetEvent(m_hEventExit);
#059 
#060         //等待线程结束。
#061         ::WaitForSingleObject(m_hThread,dwMilliseconds);
#062  }
#063 
#064 protected:
#065  //
#066  //线程运行函数。
#067  //蔡军生 2007/09/21
#068  //
#069  static DWORD WINAPI ThreadProc(LPVOID lpParameter)
#070  {
#071         //转换传送入来的参数。
#072         CThread* pThread = reinterpret_cast(lpParameter);
#073         if (pThread)
#074         {
#075               //线程返回码。
#076               //调用类的线程处理函数。
#077               return pThread->Run();
#078         }
#079         
#080         //
#081         return -1;        
#082  }
#083 
#084  //线程运行函数。
#085  //在这里可以使用类里的成员,也可以让派生类实现更强大的功能。
#086  //蔡军生 2007/09/25
#087  virtual DWORD Run(void)
#088  {
#089         //输出到调试窗口。
#090         ::OutputDebugString(_T("Run()线程函数运行\r\n"));      
#091 
#092         //线程循环。
#093         for (;;)
#094         {
#095              DWORD dwRet = WaitForSingleObject(m_hEventExit,0);
#096               if (dwRet == WAIT_TIMEOUT)
#097               {
#098                    //可以继续运行。                 
#099                    TCHAR chTemp[128];
#100                    wsprintf(chTemp,_T("ThreadID=%d\r\n"),m_dwThreadID);
#101                    ::OutputDebugString(chTemp);
#102 
#103                     //目前没有做什么事情,就让线程释放一下CPU。
#104                    Sleep(10);
#105               }
#106               else if (dwRet == WAIT_OBJECT_0)
#107               {
#108                    //退出线程。
#109                    ::OutputDebugString(_T("Run() 退出线程\r\n"));
#110                    break;
#111               }
#112               else if (dwRet == WAIT_ABANDONED)
#113               {
#114                    //出错。
#115                    ::OutputDebugString(_T("Run() 线程出错\r\n"));
#116                    return -1;
#117               }
#118         }
#119 
#120         return 0;
#121  }
#122 
#123 protected:
#124  HANDLE m_hThread;         //线程句柄。
#125  DWORD m_dwThreadID;          //线程ID。
#126 
#127  HANDLE m_hEventExit;    //线程退出事件。
#128 };
#129 
 
上面在第35行创建线程退出事件,第95行检查事件是否可退出线程运行,第58行设置退出线程的事件。