Hello: I have a problem using boost deadline timer that I cannot resolve. Why the resume method do not call again to the handler method??? Do I have something wrong? class TimerCallback { public: virtual void OnTimer()=0; }; class Timer : boost::noncopyable { public: Timer(unsigned long timeOutMilliSeconds, TimerCallback* callbac) : m_scheduler(), m_timer(m_scheduler), m_timerThread(), m_callbac(callbac), m_timeOutMilliSeconds(timeOutMilliSeconds) { }; virtual ~Timer() { m_scheduler.stop(); m_timerThread->join(); }; void some_operation() { //m_scheduler.post(boost::bind(m_callbac->OnTimer(), this)); m_callbac->OnTimer(); }; void handler(const boost::system::error_code& error) { if (!error) { // Timer expired. m_callbac->OnTimer(); m_timer.expires_from_now(boost::posix_time::milliseconds(m_timeOutMilliSeconds)); // Start an asynchronous wait. m_timer.async_wait(boost::bind(&Timer::handler, this, boost::asio::placeholders::error)); } } void start() { // Set an expiry time relative to now. m_timer.expires_from_now(boost::posix_time::milliseconds(m_timeOutMilliSeconds)); // Start an asynchronous wait. m_timer.async_wait(boost::bind(&Timer::handler, this, boost::asio::placeholders::error)); m_timerThread.reset(new boost::thread(boost::bind(&boost::asio::io_service::run, &m_scheduler))); } void pause() { m_timer.cancel(); }; void resume() { m_timer.expires_from_now(boost::posix_time::milliseconds(m_timeOutMilliSeconds)); m_timer.async_wait(boost::bind(&Timer::handler, this, boost::asio::placeholders::error)); }; private: boost::asio::io_service m_scheduler; boost::asio::deadline_timer m_timer; boost::scoped_ptrboost::thread m_timerThread; TimerCallback* m_callbac; unsigned long m_timeOutMilliSeconds; }; Salu2.. masch...
Mario Chacon wrote:
Hello: I have a problem using boost deadline timer that I cannot resolve. Why the resume method do not call again to the handler method??? Do I have something wrong?
It could be that the io_service runs out of work if you are calling start/cancel/resume in that order. I.e., io_service is probably not running anymore. Kind regards, Rutger
participants (2)
-
Mario Chacon
-
Rutger ter Borg