This event is raised when the BackgroundWorker.DoWork event handler returns.
The AsyncCompletedEventArgs.Cancelled property of System.ComponentModel.RunWorkerCompletedEventArgs indicates whether a cancellation request was processed by the background operation. If your code in the BackgroundWorker.DoWork event handler detects a cancellation request by checking the BackgroundWorker.CancellationPending flag and setting the CancelEventArgs.Cancel flag of System.ComponentModel.DoWorkEventArgs to true, the AsyncCompletedEventArgs.Cancelled flag of System.ComponentModel.RunWorkerCompletedEventArgs also 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.
Your BackgroundWorker.RunWorkerCompleted event handler should always check the AsyncCompletedEventArgs.Error and AsyncCompletedEventArgs.Cancelled properties before accessing the RunWorkerCompletedEventArgs.Result property. If an exception was raised or if the operation was canceled, accessing the RunWorkerCompletedEventArgs.Result property raises an exception.