BackgroundWorker.RunWorkerCompleted Gebeurtenis

Definitie

Treedt op wanneer de achtergrondbewerking is voltooid, is geannuleerd of een uitzondering heeft gegenereerd.

public:
 event System::ComponentModel::RunWorkerCompletedEventHandler ^ RunWorkerCompleted;
public event System.ComponentModel.RunWorkerCompletedEventHandler RunWorkerCompleted;
member this.RunWorkerCompleted : System.ComponentModel.RunWorkerCompletedEventHandler 
Public Custom Event RunWorkerCompleted As RunWorkerCompletedEventHandler 

Gebeurtenistype

Voorbeelden

In het volgende codevoorbeeld ziet u hoe de RunWorkerCompleted gebeurtenis wordt gebruikt om het resultaat van een asynchrone bewerking af te handelen. Dit codevoorbeeld maakt deel uit van een groter voorbeeld voor de BackgroundWorker klasse.

// 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;
}
// 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 != 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.
    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.
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.
    numericUpDown1.Enabled = True

    ' Enable the Start button.
    startAsyncButton.Enabled = True

    ' Disable the Cancel button.
    cancelAsyncButton.Enabled = False
End Sub

Opmerkingen

Deze gebeurtenis wordt gegenereerd wanneer de DoWork gebeurtenis-handler terugkeert.

Als de bewerking is voltooid en het resultaat ervan is toegewezen in de DoWork gebeurtenis-handler, hebt u toegang tot het resultaat via de RunWorkerCompletedEventArgs.Result eigenschap.

De Error eigenschap van System.ComponentModel.RunWorkerCompletedEventArgs geeft aan dat er een uitzondering is gegenereerd door de bewerking.

De Cancelled eigenschap van System.ComponentModel.RunWorkerCompletedEventArgs geeft aan of een annuleringsaanvraag is verwerkt door de achtergrondbewerking. Als uw code in de DoWork gebeurtenis-handler een annuleringsaanvraag detecteert door de CancellationPending vlag te controleren en de Cancel vlag van System.ComponentModel.DoWorkEventArgs op truein te stellen, wordt de Cancelled vlag ook System.ComponentModel.RunWorkerCompletedEventArgs ingesteld op true.

Caution

Houd er rekening mee dat uw code in de DoWork gebeurtenis-handler het werk kan voltooien als er een annuleringsaanvraag wordt gedaan en dat uw polling-lus mogelijk CancellationPending wordt ingesteld op true. In dit geval wordt de Cancelled vlag van System.ComponentModel.RunWorkerCompletedEventArgs uw RunWorkerCompleted gebeurtenis-handler niet ingesteld op true, ook al is er een annuleringsaanvraag ingediend. Deze situatie wordt een racevoorwaarde genoemd en is een veelvoorkomende zorg bij multithreaded programmeren. Zie Aanbevolen procedures voor beheerde threading voor meer informatie over ontwerpproblemen met meerdere threads.

De RunWorkerCompleted gebeurtenis-handler moet altijd de AsyncCompletedEventArgs.Error en AsyncCompletedEventArgs.Cancelled eigenschappen controleren voordat u de eigenschap opent RunWorkerCompletedEventArgs.Result . Als er een uitzondering is gegenereerd of als de bewerking is geannuleerd, wordt door het openen van de RunWorkerCompletedEventArgs.Result eigenschap een uitzondering gegenereerd.

Van toepassing op

Zie ook