【问题标题】:Poco Timer ExamplePoco 定时器示例
【发布时间】:2013-10-02 15:41:20
【问题描述】:

一位同事问了以下问题,在互联网上四处寻找并没有找到好的答案后,这似乎是一个很好的问题:

我在我的嵌入式代码中使用 POCO 计时器(在 Linux 上运行)。计时器是 Foundation 组件的一部分。计时器具有三个基本功能:

Timer.start();
Timer.stop();
Timer.restart();

我试图停止然后重新启动我的计时器,但我无法让它工作... ...我查看了所有 POCO 示例和示例,但没有任何关于 timer.restart( )。

有没有人对此有任何见解,或者有一个停止和重新启动计时器的工作代码示例?即使回调函数没有运行,定时器也会启动和停止,但重启似乎不起作用。

【问题讨论】:

  • 系统允许多少线程?

标签: c++ linux timer poco


【解决方案1】:

好的,自从提出这个问题以来,我继承了同事的项目并自己找到了答案。

//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;
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-29
    • 1970-01-01
    相关资源
    最近更新 更多