12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- #include "stdafx.h"
- #include "SingleTimer.h"
- UINT WINAPI TimerWaitThread(LPVOID pM);
- HANDLE hWaitEvent = NULL;
- SingleTimer::SingleTimer()
- {
- m_isRunning = false;
- hWaitEvent = CreateEventA(NULL, TRUE, FALSE, 0); //手动设置,初始状态为无信号
-
- m_hWaitThread = NULL;
- m_waitMS = 3000;
- }
- SingleTimer:: ~SingleTimer()
- {
- if (hWaitEvent)
- {
- CloseHandle(hWaitEvent);
- hWaitEvent = NULL;
- }
- if (m_hWaitThread)
- {
- WaitForSingleObject(m_hWaitThread, INFINITE); //等待线程执行完
- CloseHandle(m_hWaitThread);
- m_hWaitThread = NULL;
- }
- }
- void SingleTimer::start()
- {
- if(isRunning())
- return;
- m_isRunning = TRUE;
- m_hWaitThread = (HANDLE)_beginthreadex(NULL, 0, TimerWaitThread, this, 0, NULL);
- }
- void SingleTimer::stop()
- {
- m_isRunning = FALSE;
- }
- bool SingleTimer::isRunning() const
- {
- return m_isRunning;
- }
- void SingleTimer::setWaitTime(int wTime)
- {
- m_waitMS = wTime;
- }
- int SingleTimer::waitTime() const
- {
- return m_waitMS;
- }
- //等待线程
- UINT WINAPI TimerWaitThread(LPVOID pM)
- {
- SingleTimer *timer = (SingleTimer*)pM;
- DWORD dwRet = WaitForSingleObject(hWaitEvent, timer->waitTime()); //挂起线程
-
- timer->stop();
-
- return 0;
- }
|