Occurs when BackgroundWorker.RunWorkerAsync is called.
This event is raised when you call the BackgroundWorker.RunWorkerAsync method. This is where you start the operation that performs the potentially time-consuming work.
Your code in the BackgroundWorker.DoWork event handler should periodically check the BackgroundWorker.CancellationPending property value and abort the operation if it is true. When this occurs, you can set the CancelEventArgs.Cancel flag of System.ComponentModel.DoWorkEventArgs to true, and the AsyncCompletedEventArgs.Cancelled flag of System.ComponentModel.RunWorkerCompletedEventArgs in your BackgroundWorker.RunWorkerCompleted event handler will be set to true.
Be aware that your code in the BackgroundWorker.DoWork event handler may finish its work as a cancellation request is being made, and your polling loop may miss BackgroundWorker.CancellationPending being set to true. In this case, the AsyncCompletedEventArgs.Cancelled flag of System.ComponentModel.RunWorkerCompletedEventArgs in your BackgroundWorker.RunWorkerCompleted event handler will not be set to true, even though a cancellation request was made. This situation is called a race condition and is a common concern in multithreaded programming. For more information about multithreading design issues, see Managed Threading Best Practices.
If your operation produces a result, you can assign the result to the DoWorkEventArgs.Result property. This will be available to the BackgroundWorker.RunWorkerCompleted event handler in the RunWorkerCompletedEventArgs.Result property.
If the operation raises an exception that your code does not handle, the System.ComponentModel.BackgroundWorker catches the exception and passes it into the BackgroundWorker.RunWorkerCompleted event handler, where it is exposed as the AsyncCompletedEventArgs.Error property of System.ComponentModel.RunWorkerCompletedEventArgs. If you are running under the Visual Studio debugger, the debugger will break at the point in the BackgroundWorker.DoWork event handler where the unhandled exception was raised. If you have more than one System.ComponentModel.BackgroundWorker, you should not reference any of them directly, as this would couple your BackgroundWorker.DoWork event handler to a specific instance of System.ComponentModel.BackgroundWorker. Instead, you should access your System.ComponentModel.BackgroundWorker by casting the sender parameter in your BackgroundWorker.DoWork event handler.
You must be careful not to manipulate any user-interface objects in your BackgroundWorker.DoWork event handler. Instead, communicate to the user interface through the System.ComponentModel.BackgroundWorker events.
For more information about handling events, see Consuming Events.