Occurs when the background operation has completed, has been canceled, or has raised an exception.
Namespace:
System.ComponentModel
Assembly:
System (in System.dll)
Visual Basic (Declaration)
Public Event RunWorkerCompleted As RunWorkerCompletedEventHandler
Dim instance As BackgroundWorker
Dim handler As RunWorkerCompletedEventHandler
AddHandler instance.RunWorkerCompleted, handler
public event RunWorkerCompletedEventHandler RunWorkerCompleted
public:
event RunWorkerCompletedEventHandler^ RunWorkerCompleted {
void add (RunWorkerCompletedEventHandler^ value);
void remove (RunWorkerCompletedEventHandler^ value);
}
JScript does not support events.
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 flag and setting the Cancel flag of System.ComponentModel..::.DoWorkEventArgs to true, the Cancelled flag of System.ComponentModel..::.RunWorkerCompletedEventArgs also will be set to true.
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 result of an asynchronous operation. This code example is part of a larger example provided for the BackgroundWorker class.
' This event handler deals with the results of the
' background operation.
Private Sub backgroundWorker1_RunWorkerCompleted( _
ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) _
Handles backgroundWorker1.RunWorkerCompleted
' First, handle the case where an exception was thrown.
If (e.Error IsNot Nothing) Then
MessageBox.Show(e.Error.Message)
ElseIf e.Cancelled Then
' Next, handle the case where the user canceled the
' operation.
' Note that due to a race condition in
' the DoWork event handler, the Cancelled
' flag may not have been set, even though
' CancelAsync was called.
resultLabel.Text = "Canceled"
Else
' Finally, handle the case where the operation succeeded.
resultLabel.Text = e.Result.ToString()
End If
' Enable the UpDown control.
Me.numericUpDown1.Enabled = True
' Enable the Start button.
startAsyncButton.Enabled = True
' Disable the Cancel button.
cancelAsyncButton.Enabled = False
End Sub 'backgroundWorker1_RunWorkerCompleted
// This event handler deals with the results of the
// background operation.
private void backgroundWorker1_RunWorkerCompleted(
object sender, RunWorkerCompletedEventArgs e)
{
// First, handle the case where an exception was thrown.
if (e.Error != null)
{
MessageBox.Show(e.Error.Message);
}
else if (e.Cancelled)
{
// Next, handle the case where the user canceled
// the operation.
// Note that due to a race condition in
// the DoWork event handler, the Cancelled
// flag may not have been set, even though
// CancelAsync was called.
resultLabel.Text = "Canceled";
}
else
{
// Finally, handle the case where the operation
// succeeded.
resultLabel.Text = e.Result.ToString();
}
// Enable the UpDown control.
this.numericUpDown1.Enabled = true;
// Enable the Start button.
startAsyncButton.Enabled = true;
// Disable the Cancel button.
cancelAsyncButton.Enabled = false;
}
// This event handler deals with the results of the
// background operation.
void backgroundWorker1_RunWorkerCompleted( Object^ /*sender*/, RunWorkerCompletedEventArgs^ e )
{
// First, handle the case where an exception was thrown.
if ( e->Error != nullptr )
{
MessageBox::Show( e->Error->Message );
}
else
if ( e->Cancelled )
{
// Next, handle the case where the user cancelled
// the operation.
// Note that due to a race condition in
// the DoWork event handler, the Cancelled
// flag may not have been set, even though
// CancelAsync was called.
resultLabel->Text = "Cancelled";
}
else
{
// Finally, handle the case where the operation
// succeeded.
resultLabel->Text = e->Result->ToString();
}
// Enable the UpDown control.
this->numericUpDown1->Enabled = true;
// Enable the Start button.
startAsyncButton->Enabled = true;
// Disable the Cancel button.
cancelAsyncButton->Enabled = false;
}
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
.NET Framework
Supported in: 3.5, 3.0, 2.0
Reference
Other Resources