Freigeben über


BackgroundWorker.RunWorkerCompleted Event

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

Namespace:  System.ComponentModel
Assembly:  System (in System.dll)

Syntax

'Declaration
Public Event RunWorkerCompleted As RunWorkerCompletedEventHandler
public event RunWorkerCompletedEventHandler RunWorkerCompleted

Remarks

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 noteCaution:

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, see Best Practices for Managed Threading.

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.

Examples

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.

Private Sub bw_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs)
    If e.Cancelled = True Then
        Me.tbProgress.Text = "Canceled!"
    ElseIf e.Error IsNot Nothing Then
        Me.tbProgress.Text = "Error: " & e.Error.Message
    Else
        Me.tbProgress.Text = "Done!"
    End If
End Sub
private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    if ((e.Cancelled == true))
    {
        this.tbProgress.Text = "Canceled!";
    }

    else if (!(e.Error == null))
    {
        this.tbProgress.Text = ("Error: " + e.Error.Message);
    }

    else
    {
        this.tbProgress.Text = "Done!";
    }
}

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.