BackgroundWorker::RunWorkerCompleted Event
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
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.
If the operation completes successfully and its result is assigned in the DoWork event handler, you can access the result through the RunWorkerCompletedEventArgs::Result property.
The Error property of System.ComponentModel::RunWorkerCompletedEventArgs indicates that an exception was thrown by the operation.
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.
Caution: |
|---|
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 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, seeManaged Threading Best Practices. |
Your 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.
The following code example demonstrates the use of the RunWorkerCompleted 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.
Caution: