System.ComponentModel.BackgroundWorker.RunWorkerCompleted Event

Occurs when the background operation has completed, has been canceled, or has raised an exception.


public event RunWorkerCompletedEventHandler RunWorkerCompleted


This event is raised when the BackgroundWorker.DoWork event handler returns.

If the operation completes successfully and its result is assigned in the BackgroundWorker.DoWork event handler, you can access the result through the RunWorkerCompletedEventArgs.Result property.

The AsyncCompletedEventArgs.Error property of System.ComponentModel.RunWorkerCompletedEventArgs indicates that an exception was thrown by the operation.

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.


Namespace: System.ComponentModel
Assembly: System (in System.dll)
Assembly Versions:,
Since: .NET 2.0