Poco库Timer定时器

Poco库是一个很强大的C++库,其中常使用到的定时器类为Timer,下面就编写一个简单的定时器程序,具体说明参见注释’//'部分:

#include <stdio.h>
#include <iostream>
#include <string>
#include "Poco\Timer.h"

using namespace std;
using Poco::Timer;         // 使用Timer基类的成员
using Poco::TimerCallback; // 使用TimerCallback基类的成员

class StatTimer
{
public:
	Timer timer;
	TimerCallback<StatTimer> callback;  // 套用模板,将StatTimer类关联起来
	bool end;

	StatTimer();
	bool start();
	void onTimer(Timer& t);
	void stop();
};

StatTimer::StatTimer() : timer(0, 2000), callback(*this, &StatTimer::onTimer), end(false)
{
}
// timer(0, 2000),第一个参数默认设置为0,2000代表时间间隔为2秒
// callback的第二个参数指定定时器需要做的具体事情

bool StatTimer::start()
{
	try
	{
		timer.start(callback);   // 启动定时器线程
		//started = true;
	}
	catch (...)
	{
		return false;
		end = true;
	}

	return true;
}

void StatTimer::onTimer(Timer&/*t*/)
{
	static size_t counter = 0;
	++counter;
	p
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值