Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8357665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:33:15+00:00 2026-06-09T10:33:15+00:00

I want to execute once a specific action delayed n seconds after the last

  • 0

I want to execute once a specific action delayed n seconds after the last asynchronous event occurred. So if successive events appear in less that n seconds, the specific action is delayed (deadline_timer is restarted).

I adapted the timer class from boost deadline_timer issue and for simplicity the events are generated synchronously. Running the code, I’m expecting something like:

1 second(s)
2 second(s)
3 second(s)
4 second(s)
5 second(s)
action       <--- it should appear after 10 seconds after the last event

but I get

1 second(s)
2 second(s)
action
3 second(s)
action
4 second(s)
action
5 second(s)
action
action

Why does this happen? How to solve this?

#include <boost/asio.hpp>
#include <boost/thread.hpp>
#include <iostream>

class DelayedAction
{
public:
    DelayedAction():        
        work( service),
        thread( boost::bind( &DelayedAction::run, this)),
        timer( service)
    {}

    ~DelayedAction()
    {
        thread.join();
    }

    void startAfter( const size_t delay)
    {
        timer.cancel();
        timer.expires_from_now( boost::posix_time::seconds( delay));
        timer.async_wait( boost::bind( &DelayedAction::action, this));
    }

private:
    void run()
    {
        service.run();
    }

    void action() 
    {
        std::cout << "action" << std::endl;
    }

    boost::asio::io_service         service;
    boost::asio::io_service::work   work;
    boost::thread                   thread;
    boost::asio::deadline_timer     timer;
};

int main()
{
    DelayedAction d;
    for( int i = 1; i < 6; ++i)
    {
        Sleep( 1000);
        std::cout << i << " second(s)\n";
        d.startAfter( 10);
    }
}

PS Writing this, I’m thinking the true issue is how boost::deadline_timer can be restarted once it was started.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T10:33:16+00:00Added an answer on June 9, 2026 at 10:33 am

    When you call expires_from_now() it resets the timer, and the handler is immediately called with the error code boost::asio::error::operation_aborted.

    If you handle the error code case in your handler, then you can make this work as expected.

    void startAfter( const size_t delay)
    {
        // no need to explicitly cancel
        // timer.cancel();
        timer.expires_from_now( boost::posix_time::seconds( delay));
        timer.async_wait( boost::bind( &DelayedAction::action, this, boost::asio::placeholders::error));
    }
    
    // ...
    
    void action(const boost::system::error_code& e) 
    {
        if(e != boost::asio::error::operation_aborted)
            std::cout << "action" << std::endl;
    }
    

    This is discussed in the documentation:
    http://www.boost.org/doc/libs/1_47_0/doc/html/boost_asio/reference/deadline_timer.html

    See specifically the section titled: Changing an active deadline_timer’s expiry time

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to have an event handler that executes only once and then unbinds
I want to execute some code only once on start of a web service.
I've one rake task which i want to execute once every day: in production
IN SHORT: I want to execute a new command 3 sec after the former
I'm trying to execute a specific function once some processes finished executing. My specific
I basically want a dispatcher timer object to only execute once. So I have
I have asp.net generated html code. I want execute jQuery click function when user
I want to execute an application (executable ending with an .exe extension ) that
I want to execute a program which takes in 1 text file(say like Text.exe),
I want to execute a code helloword.cpp which takes in some argument from console

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.