| Start |
|---|
| Using a BackgroundWorker to complete a task. The following example demonstrates the use of a BackgroundWorker to update a WinForms ProgressBar. The backgroundWorker will update the value of the progress bar without blocking the UI thread, thus showing a reactive UI while work is done in the background.
|
| Assigning Event Handlers to a BackgroundWorker Once the instance of the BackgroundWorker has been declared, it must be given properties and event handlers for the tasks it performs. |
| Creating a new BackgroundWorker instance A BackgroundWorker is commonly used to perform tasks, sometimes time consuming, without blocking the UI thread. |
| Assigning Properties to a BackgroundWorker This allows the BackgroundWorker to be cancelled in between tasks This allows the worker to report progress between completion of tasks... SyntaxRemarksPerforming long-running operations within the UI thread can cause your application to become unresponsive, appearing to the user that it has stopped working. It is preferred that these tasks be run on a background thread. Once complete, the UI can be updated. Making changes to the UI during the BackgroundWorker's operation requires invoking the changes to the UI thread, typically by using the Control.Invoke (opens new window) method on the control you are updating. Neglecting to do so will cause your program to throw an exception. The BackgroundWorker is typically only used in Windows Forms applications. In WPF applications, Tasks (opens new window) are used to offload work onto background threads (possibly in combination with async/await (opens new window)). Marshalling updates onto the UI thread is typically done automatically, when the property being updated implements INotifyPropertyChanged (opens new window), or manually by using the UI thread's Dispatcher (opens new window). |
Read Article
https://develop.horje.com/learn/1434/reference