xtd - Reference Guide  0.1.1
Modern c++17/20 framework to create console, GUI and unit test applications on Windows, macOS, Linux, iOS and android.
background_worker.h
Go to the documentation of this file.
1 #pragma once
5 #include <thread>
6 #include "../forms_export.h"
7 #include "component.h"
8 #include "form.h"
12 
14 namespace xtd {
16  namespace forms {
32  public:
36 
40 
43  bool cancellation_pending() const {return cancellation_pending_;}
44 
47  bool is_busy() const {return is_busy_;}
48 
52  bool worker_reports_progress() const {return worker_reports_progress_;}
56  void worker_reports_progress(bool value) {worker_reports_progress_ = value;}
57 
61  bool worker_supports_cancellation() const {return worker_supports_cancellation_;}
65  void worker_supports_cancellation(bool value) {worker_supports_cancellation_ = value;}
66 
70  void cancel_async();
71 
74  virtual void on_do_work(do_work_event_args& e) {do_work(*this, e);}
75 
78  virtual void on_progress_changed(const progress_changed_event_args& e) {progress_changed(*this, e);}
79 
82  virtual void on_run_worker_completed(const run_worker_completed_event_args& e) {run_worker_completed(*this, e);}
83 
86 
89  template<typename argument_t>
90  void run_worker_async(argument_t argument) {
91  argument_ = argument;
92  run_worker_async();
93  }
94 
97  void report_progress(int32_t percent_progress);
98 
102  void report_progress(int32_t percent_progress, std::any user_state);
103 
107 
111 
115 
116  private:
117  std::any argument_;
118  bool cancellation_pending_ = false;
119  bool is_busy_ = false;
120  bool worker_reports_progress_ = false;
121  bool worker_supports_cancellation_ = false;
122  progress_changed_event_args e_ {0, std::any()};
123  std::unique_ptr<form> invoker_;
124  std::thread thread_;
125  };
126  }
127 }
Represents an event.
Definition: event.h:21
Executes an operation on a separate thread.
Definition: background_worker.h:31
void run_worker_async(argument_t argument)
Starts execution of a background operation.
Definition: background_worker.h:90
bool worker_reports_progress() const
Gets a value indicating whether the background_worker can report progress updates....
Definition: background_worker.h:52
void run_worker_async()
Starts execution of a background operation.
virtual void on_do_work(do_work_event_args &e)
Raises the background_worker::do_work event.
Definition: background_worker.h:74
virtual void on_run_worker_completed(const run_worker_completed_event_args &e)
Raises the background_worker::run_worker_completed event.
Definition: background_worker.h:82
bool worker_supports_cancellation() const
Gets a value indicating whether the background_worker supports asynchronous cancellation.
Definition: background_worker.h:61
void cancel_async()
Requests cancellation of a pending background operation.
background_worker()
Initializes a new instance of the background_worker class.
bool cancellation_pending() const
Gets a value indicating whether the application has requested cancellation of a background operation.
Definition: background_worker.h:43
bool is_busy() const
Gets a value indicating whether the background_worker is running an asynchronous operation.
Definition: background_worker.h:47
void worker_supports_cancellation(bool value)
Gets a value indicating whether the background_worker supports asynchronous cancellation.
Definition: background_worker.h:65
virtual void on_progress_changed(const progress_changed_event_args &e)
Raises the background_worker::progress_changed event.
Definition: background_worker.h:78
void report_progress(int32_t percent_progress, std::any user_state)
Raises the ProgressChanged event.
void worker_reports_progress(bool value)
Sets a value indicating whether the background_worker can report progress updates....
Definition: background_worker.h:56
void report_progress(int32_t percent_progress)
Raises the ProgressChanged event.
Provides the base implementation and enables object sharing between applications.
Definition: component.h:21
Provides data for the do_work event handler.
Definition: do_work_event_args.h:19
Provides data for the do_work event handler.
Definition: progress_changed_event_args.h:19
Provides data for the do_work event handler.
Definition: run_worker_completed_event_args.h:20
Contains xtd::forms::component class.
Contains xtd::forms::do_work_event_handler event handler.
Contains xtd::forms::form container.
event< background_worker, progress_changed_event_handler > progress_changed
Occurs when report_progress(int32_t) is called.
Definition: background_worker.h:110
event< background_worker, run_worker_completed_event_handler > run_worker_completed
Occurs when the background operation has completed, has been canceled, or has raised an exception.
Definition: background_worker.h:114
event< background_worker, do_work_event_handler > do_work
Occurs when run_worker_async() is called.
Definition: background_worker.h:106
#define forms_export_
Define shared library export.
Definition: forms_export.h:13
@ e
The E key.
The xtd::forms namespace contains classes for creating Windows-based applications that take full adva...
Definition: about_box.h:13
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition: system_report.h:17
Contains xtd::forms::progress_changed_event_handler event handler.
Contains xtd::forms::run_worker_completed_event_handler event handler.