xtd - Reference Guide  0.2.0
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 {
31  class forms_export_ background_worker : public component {
32  public:
34 
40 
44 
46 
50  bool cancellation_pending() const {return data_->cancellation_pending;}
51 
54  bool is_busy() const {return data_->is_busy;}
55 
59  bool worker_reports_progress() const {return data_->worker_reports_progress;}
63  void worker_reports_progress(bool value) {data_->worker_reports_progress = value;}
64 
68  bool worker_supports_cancellation() const {return data_->worker_supports_cancellation;}
72  void worker_supports_cancellation(bool value) {data_->worker_supports_cancellation = value;}
74 
76 
81  void cancel_async();
82 
85  virtual void on_do_work(do_work_event_args& e) {do_work(*this, e);}
86 
89  virtual void on_progress_changed(const progress_changed_event_args& e) {progress_changed(*this, e);}
90 
93  virtual void on_run_worker_completed(const run_worker_completed_event_args& e) {run_worker_completed(*this, e);}
94 
96  void run_worker_async();
97 
100  template<typename argument_t>
101  void run_worker_async(argument_t argument) {
102  data_->argument = argument;
103  run_worker_async();
104  }
105 
108  void report_progress(int32_t percent_progress);
109 
113  void report_progress(int32_t percent_progress, std::any user_state);
115 
117 
123 
128 
134 
135  private:
136  struct data {
137  std::any argument;
138  bool cancellation_pending = false;
139  bool is_busy = false;
140  bool worker_reports_progress = false;
141  bool worker_supports_cancellation = false;
143  std::unique_ptr<form> invoker;
144  std::thread thread;
145  };
146  std::shared_ptr<data> data_ = std::make_shared<data>();
147  };
148  }
149 }
bool worker_supports_cancellation() const
Gets a value indicating whether the background_worker supports asynchronous cancellation.
Definition: background_worker.h:68
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:101
virtual void on_do_work(do_work_event_args &e)
Raises the background_worker::do_work event.
Definition: background_worker.h:85
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:132
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:93
void worker_reports_progress(bool value)
Sets a value indicating whether the background_worker can report progress updates.Gets or sets a value indicating whether the background_worker can report progress updates.
Definition: background_worker.h:63
bool worker_reports_progress() const
Gets a value indicating whether the background_worker can report progress updates.Gets or sets a value indicating whether the background_worker can report progress updates.
Definition: background_worker.h:59
event< background_worker, progress_changed_event_handler > progress_changed
Occurs when report_progress(int32_t) is called.
Definition: background_worker.h:127
The xtd namespace contains all fundamental classes to access Hardware, Os, System, and more.
Definition: system_report.h:17
virtual void on_progress_changed(const progress_changed_event_args &e)
Raises the background_worker::progress_changed event.
Definition: background_worker.h:89
Provides data for the do_work event handler.
Definition: run_worker_completed_event_args.h:20
Contains xtd::forms::progress_changed_event_handler event handler.
Represents an event.
Definition: event.h:21
Contains xtd::forms::form container.
bool cancellation_pending() const
Gets a value indicating whether the application has requested cancellation of a background operation...
Definition: background_worker.h:50
Provides data for the do_work event handler.
Definition: do_work_event_args.h:19
Indicates that all styles except allow_binary_specifier, allow_octal_specifier and allow_hex_specifie...
Provides data for the do_work event handler.
Definition: progress_changed_event_args.h:19
The xtd::forms namespace contains classes for creating Windows-based applications that take full adva...
Definition: about_box.h:13
Contains xtd::forms::component class.
Contains xtd::forms::run_worker_completed_event_handler event handler.
Contains xtd::forms::do_work_event_handler event handler.
bool is_busy() const
Gets a value indicating whether the background_worker is running an asynchronous operation.
Definition: background_worker.h:54
event< background_worker, do_work_event_handler > do_work
Occurs when run_worker_async() is called.
Definition: background_worker.h:122
Provides the base implementation and enables object sharing between applications. ...
Definition: component.h:21
void worker_supports_cancellation(bool value)
Gets a value indicating whether the background_worker supports asynchronous cancellation.
Definition: background_worker.h:72