BackgroundWorker.RunWorkerCompleted Zdarzenie
Definicja
Ważne
Niektóre informacje odnoszą się do produktu w wersji wstępnej, który może zostać znacząco zmodyfikowany przed wydaniem. Firma Microsoft nie udziela żadnych gwarancji, jawnych lub domniemanych, w odniesieniu do informacji podanych w tym miejscu.
Występuje, gdy operacja w tle została ukończona, została anulowana lub zgłosiła wyjątek.
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
Typ zdarzenia
Przykłady
W poniższym przykładzie kodu pokazano użycie RunWorkerCompleted zdarzenia do obsługi wyniku operacji asynchronicznej. Ten przykład kodu jest częścią większego przykładu udostępnionego dla klasy BackgroundWorker.
// 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
Uwagi
To zdarzenie jest zgłaszane, gdy DoWork program obsługi zdarzeń zwraca.
Jeśli operacja zostanie ukończona pomyślnie, a jej wynik zostanie przypisany w procedurze DoWork obsługi zdarzeń, możesz uzyskać dostęp do wyniku za pośrednictwem RunWorkerCompletedEventArgs.Result właściwości .
Właściwość Error polecenia System.ComponentModel.RunWorkerCompletedEventArgs wskazuje, że wyjątek został zgłoszony przez operację.
Właściwość Cancelled elementu System.ComponentModel.RunWorkerCompletedEventArgs wskazuje, czy żądanie anulowania zostało przetworzone przez operację w tle. Jeśli kod w procedurze obsługi zdarzeń DoWork wykryje żądanie anulowania, sprawdzając CancellationPending flagę i ustawiając Cancel flagę trueSystem.ComponentModel.DoWorkEventArgs na , Cancelled flaga System.ComponentModel.RunWorkerCompletedEventArgs również zostanie ustawiona na true.
Ostrzeżenie
Należy pamiętać, że kod w procedurze DoWork obsługi zdarzeń może zakończyć swoją pracę, ponieważ jest wykonywane żądanie anulowania, a pętla sondowania może nie CancellationPending być ustawiona na truewartość . W takim przypadku flaga CancelledSystem.ComponentModel.RunWorkerCompletedEventArgs programu RunWorkerCompleted obsługi zdarzeń nie zostanie ustawiona na true, mimo że zostało wykonane żądanie anulowania. Ta sytuacja jest nazywana warunkiem wyścigu i jest częstym problemem w programowaniu wielowątkowym. Aby uzyskać więcej informacji na temat problemów z projektowaniem wielowątkowym, zobacz Managed Threading Best Practices (Najlepsze rozwiązania dotyczące zarządzania wątkami).
Procedura RunWorkerCompleted obsługi zdarzeń powinna zawsze sprawdzać AsyncCompletedEventArgs.Error właściwości i AsyncCompletedEventArgs.Cancelled przed uzyskaniem RunWorkerCompletedEventArgs.Result dostępu do właściwości. Jeśli zgłoszono wyjątek lub jeśli operacja została anulowana, uzyskanie dostępu do RunWorkerCompletedEventArgs.Result właściwości zgłasza wyjątek.