BackgroundWorker.RunWorkerCompleted Esemény
Definíció
Fontos
Egyes információk olyan, kiadás előtti termékekre vonatkoznak, amelyek a kiadásig még jelentősen módosulhatnak. A Microsoft nem vállal kifejezett vagy törvényi garanciát az itt megjelenő információért.
Akkor fordul elő, ha a háttérművelet befejeződött, megszakadt vagy kivételt okozott.
public:
event System::ComponentModel::RunWorkerCompletedEventHandler ^ RunWorkerCompleted;
public event System.ComponentModel.RunWorkerCompletedEventHandler RunWorkerCompleted;
member this.RunWorkerCompleted : System.ComponentModel.RunWorkerCompletedEventHandler
Public Custom Event RunWorkerCompleted As RunWorkerCompletedEventHandler
Eseménytípus
Példák
Az alábbi példakód bemutatja, hogy az RunWorkerCompleted esemény hogyan kezeli az aszinkron művelet eredményét. Ez a példakód egy nagyobb, az BackgroundWorker osztályhoz tartozó példa része.
// 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
Megjegyzések
Ez az esemény akkor jön elő, amikor az DoWork eseménykezelő visszatér.
Ha a művelet sikeresen befejeződött, és az eredmény hozzá van rendelve az DoWork eseménykezelőhöz, az eredményt a RunWorkerCompletedEventArgs.Result tulajdonságon keresztül érheti el.
A Error tulajdonság System.ComponentModel.RunWorkerCompletedEventArgs azt jelzi, hogy a művelet kivételt okozott.
A Cancelled tulajdonság System.ComponentModel.RunWorkerCompletedEventArgs azt jelzi, hogy a háttérművelet feldolgozta-e a lemondási kérelmet. Ha az eseménykezelőben lévő DoWork kód egy lemondási kérelmet észlel a CancellationPending jelölő ellenőrzésével és a Cancel jelző beállításávaltrueSystem.ComponentModel.DoWorkEventArgs, akkor a Cancelled jelölő System.ComponentModel.RunWorkerCompletedEventArgs is a következőre truelesz állítva.
Caution
Vegye figyelembe, hogy az DoWork eseménykezelőben lévő kód befejezheti a munkát lemondási kérelemként, és előfordulhat, hogy a lekérdezési ciklus nem CancellationPending lesz beállítva true. Ebben az esetben az Cancelled eseménykezelő jelölője System.ComponentModel.RunWorkerCompletedEventArgsRunWorkerCompleted nem lesz beállítva true, még akkor sem, ha lemondási kérelmet tettek. Ezt a helyzetet nevezik versenyfeltételnek , és gyakori probléma a többszálú programozásban. A többszálú kialakítással kapcsolatos problémákról további információt a Felügyelt szálkezelés ajánlott eljárásai című témakörben talál.
A RunWorkerCompleted tulajdonság elérése előtt az eseménykezelőnek mindig ellenőriznie kell a AsyncCompletedEventArgs.Error tulajdonságokat és AsyncCompletedEventArgs.Cancelled a RunWorkerCompletedEventArgs.Result tulajdonságokat. Ha kivételt emeltek ki, vagy ha a műveletet megszakították, a RunWorkerCompletedEventArgs.Result tulajdonság elérése kivételt eredményez.