Poco::Timer 定时器demo

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.

 

posted @ 2013-02-18 10:37  Leo Forest  阅读(1894)  评论(0编辑  收藏  举报