The BackgroundWorker class allows you to run an operation on a separate, dedicated thread. Time-consuming operations, such as downloads and database transactions, can cause your user interface to stop responding. When you want a responsive user interface and you must perform time-consuming operations, the BackgroundWorker class provides a convenient solution.
To run an operation in the background, create a BackgroundWorker. You can listen for events that report the progress of your operation and signal when your operation is completed.
To set up a background operation, add an event handler for the DoWork event. Call your time-consuming operation in this event handler. To start the background operation, call the RunWorkerAsync method. To receive notifications of progress updates, handle the ProgressChanged event. To receive a notification when the operation is completed, handle the RunWorkerCompleted event.
Note: |
|---|
You must be careful not to manipulate any user-interface objects in your DoWork event handler. Instead, communicate to the user interface through the ProgressChanged and RunWorkerCompleted events. |
If your background operation requires a parameter, call RunWorkerAsync with your parameter. Inside the DoWork event handler, you can extract the parameter from the DoWorkEventArgs..::.Argument property.
For more information about BackgroundWorker, see How to: Use a Background Worker.