Search code examples
c++wt

Stop Wt C++ function while it's running


I'm using Wt (witty) C++ framework and stuck with an issue how to stop a function from running. If button open_doors_button is clicked, it activates a function doors_open_all, which takes a long time to complete.

The problems appears if the user would like to stop this function from running. I added a function stop_doors_open_all, which changes a global boolean to stop the previous function. The issue is that func stop_doors_open_all will be called after func doors_open_all is finished, making the stop function useless.

    Wt::WPushButton *open_doors_button = new Wt::WPushButton("open all");
    container_box->addWidget(open_doors_button);
    open_doors_button->clicked().connect(boost::bind(&Servicemode::doors_open_all, this));

    Wt::WPushButton *stop_doors_button = new Wt::WPushButton("stop opening");
    container_box->addWidget(stop_doors_button);
    stop_doors_button->clicked().connect(boost::bind(&Servicemode::stop_doors_open_all, this));

Solution

  • You will need to run your slow function in its own thread if you don't want to block the thread that responds to Wt events. Multithreading is what you're describing.