RunWorkerCompletedEventArgs Osztály

Definíció

Adatokat biztosít a MethodNameCompleted eseményhez.

public ref class RunWorkerCompletedEventArgs : System::ComponentModel::AsyncCompletedEventArgs
public class RunWorkerCompletedEventArgs : System.ComponentModel.AsyncCompletedEventArgs
type RunWorkerCompletedEventArgs = class
    inherit AsyncCompletedEventArgs
Public Class RunWorkerCompletedEventArgs
Inherits AsyncCompletedEventArgs
Öröklődés
RunWorkerCompletedEventArgs

Példák

Az alábbi példakód a következő kód használatát szemlélteti RunWorkerCompletedEventArgs. Ez a példa az osztály egy nagyobb mintájának BackgroundWorker 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

Ha a event-alapú aszinkron mintát aszinkron műveletekhez használja, egy Windows Forms űrlap vagy vezérlőelem aszinkron műveletet kezdeményez az BackgroundWorker.RunWorkerAsync metódus meghívásával. A metódus viszont aszinkron módon emeli ki az BackgroundWorker.DoWork eseményt, és átad egy példányt DoWorkEventArgs . Ha az aszinkron művelet egy értéket ad vissza, az BackgroundWorker.DoWork eseménykezelő általában hozzárendeli azt a DoWorkEventArgs.Result tulajdonsághoz. Amikor az aszinkron művelet befejeződik, az BackgroundWorker.RunWorkerCompleted esemény létre lesz hozva, és átad egy RunWorkerCompletedEventArgs példányt, amely információkat tartalmaz a művelet állapotáról (függetlenül attól, hogy megszakították, hibásak vagy sikeresen befejezték). Ha sikeresen befejeződött, a tulajdonsága Result tartalmazza az aszinkron művelet által visszaadott és a DoWorkEventArgs.Result tulajdonsághoz korábban hozzárendelt értéket.

Note

Az HostProtectionAttribute osztályra alkalmazott attribútum a következő Resources tulajdonságértéket tartalmazza: SharedState. Ez HostProtectionAttribute nem érinti az asztali alkalmazásokat (amelyek általában egy ikonra duplán kattintva, egy parancs beírásával vagy egy URL-cím beírásával indulnak el a böngészőben). További információ: HostProtectionAttribute osztály vagy SQL Server Programozási és gazdagépvédelmi attribútumok.

Konstruktorok

Name Description
RunWorkerCompletedEventArgs(Object, Exception, Boolean)

Inicializálja a RunWorkerCompletedEventArgs osztály új példányát.

Tulajdonságok

Name Description
Cancelled

Beolvas egy értéket, amely jelzi, hogy egy aszinkron művelet megszakadt-e.

(Öröklődés forrása AsyncCompletedEventArgs)
Error

Egy értéket kap, amely jelzi, hogy melyik hiba történt egy aszinkron művelet során.

(Öröklődés forrása AsyncCompletedEventArgs)
Result

Egy aszinkron művelet eredményét képviselő értéket kap.

UserState

Lekéri a felhasználói állapotot képviselő értéket.

Metódusok

Name Description
Equals(Object)

Meghatározza, hogy a megadott objektum egyenlő-e az aktuális objektummal.

(Öröklődés forrása Object)
GetHashCode()

Ez az alapértelmezett kivonatoló függvény.

(Öröklődés forrása Object)
GetType()

Lekéri az Type aktuális példányt.

(Öröklődés forrása Object)
MemberwiseClone()

Az aktuális Objectpéldány sekély másolatát hozza létre.

(Öröklődés forrása Object)
RaiseExceptionIfNecessary()

Felhasználó által megadott kivételt hoz létre, ha egy aszinkron művelet meghiúsult.

(Öröklődés forrása AsyncCompletedEventArgs)
ToString()

Az aktuális objektumot jelképező sztringet ad vissza.

(Öröklődés forrása Object)

A következőre érvényes:

Lásd még