BackgroundWorker.RunWorkerCompleted Evento

Definizione

Viene generato quando l'operazione in background è stata annullata o ha generato un'eccezione.

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

Tipo evento

Esempio

Nell'esempio di codice seguente viene illustrato l'uso dell'evento RunWorkerCompleted per gestire il risultato di un'operazione asincrona. Questo esempio di codice fa parte di un esempio più grande fornito per la BackgroundWorker classe.

// 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.
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.
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

Commenti

Questo evento viene generato quando il DoWork gestore eventi restituisce.

Se l'operazione viene completata correttamente e il relativo risultato viene assegnato nel DoWork gestore eventi, è possibile accedere al risultato tramite la RunWorkerCompletedEventArgs.Result proprietà .

La Error proprietà di System.ComponentModel.RunWorkerCompletedEventArgs indica che un'eccezione è stata generata dall'operazione.

La Cancelled proprietà di indica se una richiesta di System.ComponentModel.RunWorkerCompletedEventArgs annullamento è stata elaborata dall'operazione in background. Se il codice nel DoWork gestore eventi rileva una richiesta di annullamento controllando il CancellationPending flag e impostando il Cancel flag di su true, il Cancelled flag di System.ComponentModel.DoWorkEventArgsSystem.ComponentModel.RunWorkerCompletedEventArgs verrà impostato anche su true.

Attenzione

Tenere presente che il DoWork codice nel gestore eventi può completare il proprio lavoro come richiesta di annullamento e il ciclo di polling potrebbe non CancellationPending essere impostato su true. In questo caso, il Cancelled flag di nel RunWorkerCompleted gestore eventi non verrà impostato su true, anche se è stata effettuata una richiesta di System.ComponentModel.RunWorkerCompletedEventArgs annullamento. Questa situazione è chiamata condizione di gara ed è una preoccupazione comune nella programmazione multithreaded. Per altre informazioni sui problemi di progettazione multithreading, vedere Procedure consigliate per il threading gestito.

Il RunWorkerCompleted gestore eventi deve sempre controllare le AsyncCompletedEventArgs.Error proprietà e AsyncCompletedEventArgs.Cancelled prima di accedere alla RunWorkerCompletedEventArgs.Result proprietà. Se è stata generata un'eccezione o se l'operazione è stata annullata, l'accesso alla RunWorkerCompletedEventArgs.Result proprietà genera un'eccezione.

Si applica a

Vedi anche