Poco::Timer 定时器demo

拜拜、爱过 提交于 2019-11-26 18:32:22

timer.cc

#include <cstdlib>
#include <iostream>

#include <Poco/Timer.h>
#include <Poco/Thread.h>

using Poco::Timer;
using Poco::TimerCallback;
using Poco::Thread;

class TimerExample
{
   public:
      void onTimer(Timer& timer)
      {
         std::cout << "onTimer called." << std::endl;
      }
};


// ===  FUNCTION  ======================================================================
//         Name:  main
//  Description:  main function
// =====================================================================================
   int
main ( int argc, char *argv[] )
{
   long totalTime        = 2000,
        startInterval    = 0,
        periodicInterval = 0;

   if (argc == 3) {
      startInterval    = atol(argv[1]);
      periodicInterval = atol(argv[2]);
   }

   TimerExample te;
   Timer timer(startInterval, periodicInterval);
   timer.start(TimerCallback<TimerExample>(te, &TimerExample::onTimer));

   Thread::sleep(totalTime);
   return EXIT_SUCCESS;
}     // ----------  end of function main  ----------

编译:

[root@slayer poco]# g++ timer.cc -lPocoFoundation

结果:

[root@slayer poco]# ./a.out
onTimer called.
[root@slayer poco]# ./a.out 1000 600
onTimer called.
onTimer called.
[root@slayer poco]# ./a.out 1500 100
onTimer called.
onTimer called.
onTimer called.
onTimer called.
onTimer called.
onTimer called.

 

转载于:https://www.cnblogs.com/Leo-Forest/archive/2013/02/18/2915282.html

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!