Regarding the usage of xtd::forms::busy_box #243
-
Hello! I hope you are doing fine! I have a question regarding It is necessary to call for example:
form1() {
text("Busy box example");
button1.auto_size(true);
button1.location({10, 10});
button1.parent(*this);
button1.text("Do something...");
button1.click += [] {
busy_box::show("Please wait while do something...", "Application busy");
for (auto count = 0; count < 500; ++count) {
application::do_events();
thread::sleep(10);
}
busy_box::hide();
};
}
form1(){
busy_box::show(*this, "Initializing...", "form1");
startup_panel_.auto_scroll(true);
startup_panel_.parent(*this);
startup_panel_.size(client_size() - drawing::size {0, 100});
application::do_events(); // <-- i must call this here to make busy_box visible
startup_open_recent_projects_list_box_.parent(startup_panel_);
startup_open_recent_projects_list_box_.location({50, 175});
application::do_events(); // <-- i must call this here to make busy_box visible
some_function(); // <-- should i call application::do_events also from within the function ?
application::do_events(); // <-- i must call this here to make busy_box visible
some_function_that_calls_other_init_functions(); // <-- should i call application::do_events() from within all sub-functions ?
busy_box::hide();
} Is there a better approach to use busy_box correctly ? Thanks in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
I'm fine thanks. First of all the xtd::application::do_events method processes all Windows messages currently in the message queue. This is a didactic example, designed to show you how to use the xtd::forms::message_box dialog box. The following lines simply emulate a time-consuming process: for (auto count = 0; count < 500; ++count) {
application::do_events();
thread::sleep(10);
} But in real code, you'll never use the for loop with the xtd::threading::thread::sleep and xtd::forms::application::do_event statements. Instead, create an xtd::threading::thread or, even better, an xtd::forms::background_worker in which you can call your business classes. Which can take a lot of time without blocking graphic thread events. See How to create a thread ? wiki for xtd::threading::thread and xtd::forms::background_worker usage. ExamplesThe follwing example shows how to use xtd::forms::busy_box with xtd::forms::background_worker : #include <xtd/forms/application>
#include <xtd/forms/background_worker>
#include <xtd/forms/busy_box>
#include <xtd/forms/button>
#include <xtd/forms/form>
#include <xtd/threading/thread>
using namespace xtd::forms;
using namespace xtd::threading;
class form1 : public form {
public:
form1() {
text("Busy box example with background_worker");
backgound_worker1.worker_supports_cancellation(true);
backgound_worker1.do_work += [] {
// Call your business class here.
// the folowing line simulate time consuming process
thread::sleep(5000);
};
backgound_worker1.run_worker_completed += [this] {
busy_box::hide();
button1.enabled(true);
};
button1.auto_size(true);
button1.location({10, 10});
button1.parent(*this);
button1.text("Do something...");
button1.click += [this] {
button1.enabled(false);
busy_box::show("Please wait while do something...", "Application busy");
backgound_worker1.run_worker_async();
};
form_closed += [this] {
backgound_worker1.cancel_async();
};
}
private:
button button1;
background_worker backgound_worker1;
};
auto main()->int {
xtd::forms::application::run(form1 {});
} The follwing example shows how to use xtd::forms::busy_box with xtd::threading::thread and xtd::forms::control::invoke method : #include <xtd/forms/application>
#include <xtd/forms/busy_box>
#include <xtd/forms/button>
#include <xtd/forms/form>
#include <xtd/threading/thread>
using namespace xtd::forms;
using namespace xtd::threading;
class form1 : public form {
public:
form1() {
text("Busy box example with thread");
button1.auto_size(true);
button1.location({10, 10});
button1.parent(*this);
button1.text("Do something...");
button1.click += [this] {
button1.enabled(false);
busy_box::show("Please wait while do something...", "Application busy");
thread1 = thread {[this] {
// Call your business class here.
// the following line simulates a time-consuming process
thread::sleep(5000);
invoke([this] {
busy_box::hide();
button1.enabled(true);
});
}};
thread1.start();
};
form_closing += [this](object& sender, form_closing_event_args& e) {
e.cancel(thread1.is_alive());
};
}
private:
button button1;
thread thread1;
};
auto main()->int {
xtd::forms::application::run(form1 {});
} RemarksThis explanation applies to all examples using the xtd::forms::applicaiton::do_events and xtd::threading::thread::sleep methods to simulate a long process. I hope that's clearer for you. |
Beta Was this translation helpful? Give feedback.
I'm fine thanks.
First of all the xtd::application::do_events method processes all Windows messages currently in the message queue.
Read the
Remarks
andWarning
sections for more information about this method.This is a didactic example, designed to show you how to use the xtd::forms::message_box dialog box.
This example is simple to focused the use of xtd::forms::busy_box.
The following lines simply emulate a time-consuming process:
But in real code, you'll never use the for loop with the xtd::threading::thread::sleep and xtd::forms::application::do_event statements.
It's a very bad idea to do …