| Home | Libraries | People | FAQ | More

# Changing an active deadline_timer's expiry time

Changing the expiry time of a timer while there are pending asynchronous waits causes those wait operations to be cancelled. To ensure that the action associated with the timer is performed only once, use something like this: used:

     void on_some_event()
     {
       if (my_timer.expires_from_now(seconds(5)) > 0)
       {
         // We managed to cancel the timer. Start new asynchronous wait.
         my_timer.async_wait(on_timeout);
       }
       else
       {
         // Too late, timer has already expired!
       }
     }

     void on_timeout(const boost::asio::error& e)
     {
       if (e != boost::asio::error::operation_aborted)
       {
         // Timer was not cancelled, take necessary action.
       }
     }

- The boost::asio::basic_deadline_timer::expires_from_now() function cancels any pending asynchronous waits, and returns the number of asynchronous waits that were cancelled. If it returns 0 then you were too late and the wait handler has already been executed, or will soon be executed. If it returns 1 then the wait handler was successfully cancelled.

- If a wait handler is cancelled, the boost::asio::error passed to it contains the value boost::asio::error::operation_aborted.

See also:
  boost::asio::basic_deadline_timer

| Copyright © 2003 - 2006 Christopher M. Kohlhoff