自定义定时器的一种方法

这几天在用MFC做有关定时器的东西,发现MFC自带的定时器方式SetTimer方式不适合用于频繁重新计时的方式,过多地Stop和SetTimer程序会死掉,因此,自已用线程的方式做了一个定时器类:

#pragma once
#include "afxwin.h"

#define ONESHOT 0
#define PERIOD 1
typedef int (*tp_ontimer)(void *param);
class CMyTimer
{
public:
	CMyTimer(void);
	~CMyTimer(void);
protected:
	DWORD m_start_tick;
	UINT m_timeout;
public:
	static UINT CounterThread(void * param);
protected:
	bool m_run_flag;
public:
	void init(int mode,int timeout, tp_ontimer handle,void *param);
protected:
	tp_ontimer m_onhandle;
public:
	void init(void);
	void SetTimeouet(int timeout);
	void SetOnhandle(tp_ontimer handle,void *param);
protected:
	CWinThread *m_thread;
public:
	int Start(void);
	void Stop(void);
	void Restart(void);
protected:
	void *m_param;
	int m_mode;
};


#include "StdAfx.h"
#include "TboxTimer.h"


CTboxTimer::CTboxTimer(void)
	: m_start_tick(0)
	, m_timeout(0)
	, m_run_flag(false)
	,m_onhandle(NULL)
	,m_thread(NULL)
	, m_param(NULL)
	, m_mode(0)
{
}


CTboxTimer::~CTboxTimer(void)
{
}


UINT CTboxTimer::CounterThread(void * param)
{
	
	CTboxTimer *timer =(CTboxTimer *)param;

	while(timer->m_run_flag){
		if(GetTickCount() -timer->m_start_tick >=timer->m_timeout){
			//do something here
			if(timer->m_onhandle !=NULL){
				if(0 !=(*timer->m_onhandle)(timer->m_param)){
					timer->Stop();
					break;
				}
				if(timer->m_mode ==ONESHOT){
					timer->Stop();
					break;
				}

				timer->m_start_tick =GetTickCount();//更新记时起点
			}
		}
		Sleep(20);
	}
	return 0;
}


void CTboxTimer::init(int mode,int timeout, tp_ontimer handle=NULL,void *param=NULL)
{
	m_mode =mode;
	m_timeout =timeout;
	m_onhandle =handle;
	m_param =param;
}


void CTboxTimer::init(void)
{
	m_timeout =0;
	m_onhandle =NULL;
}


void CTboxTimer::SetTimeouet(int timeout)
{
	m_timeout =timeout;
}


void CTboxTimer::SetOnhandle(tp_ontimer handle,void *param)
{
	m_onhandle =handle;
	m_param =param;
}


int CTboxTimer::Start(void)
{
	if(m_run_flag){
		return 0;
	}
	m_run_flag =true;
	m_start_tick =GetTickCount();
	m_thread =AfxBeginThread(CounterThread,this);
	if(m_thread ==NULL){
		m_run_flag =false;
		return -1;
	}
	return 0;
}


void CTboxTimer::Stop(void)
{
	m_run_flag =false;
}


void CTboxTimer::Restart(void)
{
	m_start_tick =GetTickCount();
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值