Boost logo

Boost Users :

Subject: [Boost-users] Deadline_timer problem
From: Mario Chacon (the.masch_at_[hidden])
Date: 2009-07-11 22:04:04


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_ptr<boost::thread> m_timerThread;
    TimerCallback* m_callbac;
    unsigned long m_timeOutMilliSeconds;
};

Salu2..
masch...



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net