BackgroundWorker.RunWorkerCompleted Událost

Definice

Vyvolá se v případě, že operace na pozadí byla dokončena, byla zrušena nebo vyvolala výjimku.

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 

Event Type

Příklady

Následující příklad kódu ukazuje použití RunWorkerCompleted události ke zpracování výsledku asynchronní operace. Tento příklad kódu je součástí většího příkladu BackgroundWorker pro třídu .

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

Poznámky

Tato událost je vyvolána, když se vrátí obslužná rutina DoWork události.

Pokud se operace úspěšně dokončí a její výsledek je přiřazen v obslužné rutině DoWork události, můžete k výsledku přistupovat prostřednictvím RunWorkerCompletedEventArgs.Result vlastnosti .

Vlastnost Error označuje System.ComponentModel.RunWorkerCompletedEventArgs , že operace vyvolala výjimku.

Vlastnost Cancelled určuje System.ComponentModel.RunWorkerCompletedEventArgs , jestli byla žádost o zrušení zpracována operací na pozadí. Pokud kód v obslužné rutině DoWork události zjistí požadavek na zrušení kontrolou příznaku CancellationPending a nastavením příznaku CancelSystem.ComponentModel.DoWorkEventArgs na truehodnotu , Cancelled příznak System.ComponentModel.RunWorkerCompletedEventArgs také se nastaví na true.

Upozornění

Mějte na paměti, že váš kód v DoWork obslužné rutině události může dokončit svou práci při vytváření požadavku na zrušení a že smyčka dotazování nemusí být CancellationPending nastavená na truehodnotu . V takovém případě Cancelled se příznak v System.ComponentModel.RunWorkerCompletedEventArgsRunWorkerCompleted obslužné rutině události nenastaví na truehodnotu , i když byla podána žádost o zrušení. Tato situace se nazývá konflikt časování a je běžným problémem při vícevláknovém programování. Další informace o problémech s návrhem multithreadingu najdete v tématu Osvědčené postupy pro spravovaná vlákna.

Vaše RunWorkerCompleted obslužná rutina události by měla před přístupem k RunWorkerCompletedEventArgs.Result vlastnosti vždy zkontrolovat AsyncCompletedEventArgs.Error vlastnosti a AsyncCompletedEventArgs.Cancelled . Pokud byla vyvolána výjimka nebo byla operace zrušena, vyvolá se RunWorkerCompletedEventArgs.Result při přístupu k vlastnosti výjimka.

Platí pro

Viz také