July 28, 2014
Occurs when the background operation has completed, has been canceled, or has raised an exception.
Assembly: System (in System.dll)
This event is raised when the DoWork event handler returns.
The Cancelled property of System.ComponentModel.RunWorkerCompletedEventArgs indicates whether a cancellation request was processed by the background operation. If your code in the DoWork event handler detects a cancellation request by checking the CancellationPending property and setting the Cancel property of System.ComponentModel.DoWorkEventArgs to true, the Cancelled property of System.ComponentModel.RunWorkerCompletedEventArgs will also be set to true.
Be aware that your code in the DoWork event handler may complete its work as a cancellation request is being made, and your polling loop may miss CancellationPending being set to true. In this case, the Cancelled flag of System.ComponentModel.RunWorkerCompletedEventArgs in your 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, seeManaged Threading Best Practices.
Your 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.
The following code example demonstrates the use of the event to handle the completion of a background operation. To view the complete code for this sample, see How to use a background worker for Windows Phone 8.