加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
timer.h 2.25 KB
一键复制 编辑 原始数据 按行查看 历史
/*
Based on "Simple C++ Timer Wrapper"
http://www.codeproject.com/Articles/146617/Simple-C-Timer-Wrapper
by ken.loveday
v1.0 2013 ArmyOfPirates
*/
static void CALLBACK TimerProc(void*, BOOLEAN);
static void CALLBACK TimerProcOnce(void* param, BOOLEAN timerCalled);
///////////////////////////////////////////////////////////////////////////////
//
// class CTimer
//
class CTimer
{
public:
CTimer()
{
m_hTimer = NULL;
m_mutexCount = 0;
}
virtual ~CTimer()
{
Stop();
}
bool Start(unsigned int interval, // interval in ms
bool immediately = false,// true to call first event immediately
bool once = false) // true to call timed event only once
{
if( m_hTimer )
{
Stop();
}
SetCount(0);
BOOL success = CreateTimerQueueTimer( &m_hTimer,
NULL,
once ? TimerProcOnce : TimerProc,
this,
immediately ? 0 : interval,
once ? 0 : interval,
WT_EXECUTEINTIMERTHREAD);
return( success != 0 );
}
void Stop()
{
DeleteTimerQueueTimer( NULL, m_hTimer, NULL );
m_hTimer = NULL ;
}
void (*OnTimedEvent)();
void SetCount(int value)
{
InterlockedExchange( &m_mutexCount, value );
}
int GetCount()
{
return InterlockedExchangeAdd( &m_mutexCount, 0 );
}
bool Enabled()
{
return m_hTimer != NULL;
}
private:
HANDLE m_hTimer;
long m_mutexCount;
};
///////////////////////////////////////////////////////////////////////////////
//
// TimerProc
//
static void CALLBACK TimerProc(void* param, BOOLEAN timerCalled)
{
CTimer* timer = static_cast<CTimer*>(param);
timer->SetCount( timer->GetCount()+1 );
timer->OnTimedEvent();
};
static void CALLBACK TimerProcOnce(void* param, BOOLEAN timerCalled)
{
CTimer* timer = static_cast<CTimer*>(param);
timer->SetCount( timer->GetCount()+1 );
timer->OnTimedEvent();
if( timer->Enabled() )
timer->Stop();
};
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化