好的,自从提出这个问题以来,我继承了我同事的项目并自己找到了答案。
//use restart for an already running timer, to restart it
Timer.restart();
如果你的定时器已经停止,需要重新启动,你需要先重新设置周期间隔,下面是 Poco 的例子,加上我自己的几行代码。这将编译并重新启动计时器。
#include "Poco/Timer.h"
#include "Poco/Thread.h"
#include "Poco/Stopwatch.h"
#include <iostream>
using Poco::Timer;
using Poco::TimerCallback;
using Poco::Thread;
using Poco::Stopwatch;
class TimerExample
{
public:
TimerExample()
{
_sw.start();
}
void onTimer(Timer& timer)
{
std::cout << "Callback called after " << _sw.elapsed()/1000 << " milliseconds." << std::endl;
}
private:
Stopwatch _sw;
};
int main(int argc, char** argv)
{
TimerExample example;
TimerCallback<TimerExample> callback(example, &TimerExample::onTimer);
Timer timer(250, 500);
timer.start(callback);
Thread::sleep(5000);
timer.stop();
std::cout << "Trying to restart timer now" << std::endl;
timer.setStartInterval(250);
timer.setPeriodicInterval(500);
timer.start(callback);
Thread::sleep(5000);
timer.stop();
return 0;
}