xtd - Reference Guide  0.1.0
Modern c++17 framework to create console, gui and unit test applications on Windows, macOS and Linux.
xtd::forms::background_worker Class Reference

Executes an operation on a separate thread. More...

#include <background_worker.h>

Inheritance diagram for xtd::forms::background_worker:
xtd::forms::component

Public Member Functions

 background_worker ()
 Initializes a new instance of the background_worker class. More...
 
void cancel_async ()
 
bool cancellation_pending () const
 
bool is_busy () const
 
void on_do_work (do_work_event_args &e)
 
void on_progres_changed (const progress_changed_event_args &e)
 
void on_run_worker_completed (const run_worker_completed_event_args &e)
 
void report_progress (int32_t percent_progress)
 
void report_progress (int32_t percent_progress, std::any user_state)
 
void run_worker_async ()
 Starts execution of a background operation. More...
 
template<typename argument_t >
void run_worker_async (argument_t argument)
 Starts execution of a background operation. More...
 
bool worker_reports_progress () const
 
void worker_reports_progress (bool value)
 
bool worker_supports_cancellation () const
 
void worker_supports_cancellation (bool value)
 

Public Attributes

event< background_worker, do_work_event_handler< component & > > do_work
 
event< background_worker, progress_changed_event_handler< component & > > progress_changed
 
event< background_worker, run_worker_completed_event_handler< component & > > run_worker_completed
 

Protected Member Functions

virtual bool can_raise_events () const
 Gets a value indicating whether the component can raise an event. More...
 
bool design_mode () const
 Gets a value that indicates whether the component is currently in design mode. More...
 

Detailed Description

Executes an operation on a separate thread.

Remarks
The background_worker class allows you to run an operation on a separate, dedicated thread. Time-consuming operations like downloads and database transactions can cause your user interface (UI) to seem as though it has stopped responding while they are running. When you want a responsive UI and you are faced with long delays associated with such operations, the background_worker class provides a convenient solution.
To execute a time-consuming operation in the background, create a background_worker and listen for events that report the progress of your operation and signal when your operation is finished.
To set up for a background operation, add an event handler for the do_work event. Call your time-consuming operation in this event handler. To start the operation, call run_worker_async. To receive notifications of progress updates, handle the progress_changed event. To receive a notification when the operation is completed, handle the run_worker_completed event.
Note
You must be careful not to manipulate any user-interface objects in your do_work event handler. Instead, communicate to the user interface through the progress_changed and run_worker_completed events.
Remarks
If your background operation requires a parameter, call run_worker_async with your parameter. Inside the do_work event handler, you can extract the parameter from the do_work_event_args.argument property.

The documentation for this class was generated from the following file: