BackgroundWorker.RunWorkerCompleted Událost
Definice
Důležité
Některé informace platí pro předběžně vydaný produkt, který se může zásadně změnit, než ho výrobce nebo autor vydá. Microsoft neposkytuje žádné záruky, výslovné ani předpokládané, týkající se zde uváděných informací.
Nastane, když je operace na pozadí 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 uvedeného pro třídu 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
Poznámky
Tato událost je vyvolána, když obslužná rutina DoWork události vrátí.
Pokud se operace úspěšně dokončí a její výsledek je přiřazen v DoWork obslužné rutině události, můžete k výsledku RunWorkerCompletedEventArgs.Result získat přístup prostřednictvím vlastnosti.
Vlastnost ErrorSystem.ComponentModel.RunWorkerCompletedEventArgs označuje, že operace vyvolala výjimku.
Vlastnost CancelledSystem.ComponentModel.RunWorkerCompletedEventArgs označuje, zda byla žádost o zrušení zpracována operací na pozadí. Pokud váš kód v DoWork obslužné rutině události zjistí žádost o zrušení zaškrtnutím CancellationPending příznaku a nastavením Cancel příznaku System.ComponentModel.DoWorkEventArgs na true, Cancelled příznak System.ComponentModel.RunWorkerCompletedEventArgs bude také nastaven 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 smyčka dotazování může chybět CancellationPending nastavena na true. V takovém případě Cancelled nebude příznak System.ComponentModel.RunWorkerCompletedEventArgs obslužné RunWorkerCompleted rutiny události nastaven na true, i když byla provedena žádost o zrušení. Tato situace se nazývá stav časování a je běžným zájmem o vícevláknové programování. Další informace o problémech s návrhem vícevláknového formátování najdete v tématu Osvědčené postupy pro spravované vlákno.
Obslužná RunWorkerCompleted rutina události by měla před přístupem k RunWorkerCompletedEventArgs.Result vlastnosti vždy zkontrolovat AsyncCompletedEventArgs.Error vlastnosti a AsyncCompletedEventArgs.Cancelled vlastnosti. Pokud byla vyvolána výjimka nebo pokud byla operace zrušena, vyvolá přístup k RunWorkerCompletedEventArgs.Result vlastnosti výjimku.