RunWorkerCompletedEventArgs Třída

Definice

Poskytuje data pro událost MethodNameCompleted .

public ref class RunWorkerCompletedEventArgs : System::ComponentModel::AsyncCompletedEventArgs
public class RunWorkerCompletedEventArgs : System.ComponentModel.AsyncCompletedEventArgs
type RunWorkerCompletedEventArgs = class
    inherit AsyncCompletedEventArgs
Public Class RunWorkerCompletedEventArgs
Inherits AsyncCompletedEventArgs
Dědičnost
RunWorkerCompletedEventArgs

Příklady

Následující příklad kódu znázorňuje použití .RunWorkerCompletedEventArgs Tento příklad je součástí větší ukázky 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

Při použití asynchronního vzoru založeného na událostech pro asynchronní operace model Windows Forms formulář nebo ovládací prvek iniciuje asynchronní operaci voláním BackgroundWorker.RunWorkerAsync metody. Metoda zase asynchronně vyvolá BackgroundWorker.DoWork událost a předá jí DoWorkEventArgs instanci. Pokud asynchronní operace vrátí hodnotu, BackgroundWorker.DoWork obslužná rutina události ji obvykle přiřadí vlastnosti DoWorkEventArgs.Result . Po dokončení BackgroundWorker.RunWorkerCompleted asynchronní operace se vyvolá událost a předá RunWorkerCompletedEventArgs se instance, která obsahuje informace o stavu operace (zda byla zrušena, chybována nebo úspěšně dokončena). Pokud se úspěšně dokončil, jeho Result vlastnost obsahuje hodnotu vrácenou asynchronní operací a dříve přiřazenou DoWorkEventArgs.Result vlastnosti.

Poznámka

Atribut HostProtectionAttribute použitý pro tuto třídu má následující Resources hodnotu vlastnosti: SharedState. Atribut HostProtectionAttribute nemá vliv na běžné aplikace (které jsou obvykle spouštěny dvojitým kliknutím na ikonu, zadáním příkazu nebo zadáním adresy URL v prohlížeči). Další informace najdete v HostProtectionAttribute tématu třídy nebo SQL Server atributy programování a ochrany hostitele.

Konstruktory

RunWorkerCompletedEventArgs(Object, Exception, Boolean)

Inicializuje novou instanci RunWorkerCompletedEventArgs třídy.

Vlastnosti

Cancelled

Získá hodnotu označující, zda asynchronní operace byla zrušena.

(Zděděno od AsyncCompletedEventArgs)
Error

Získá hodnotu označující, ke které chybě došlo během asynchronní operace.

(Zděděno od AsyncCompletedEventArgs)
Result

Získá hodnotu, která představuje výsledek asynchronní operace.

UserState

Získá hodnotu, která představuje stav uživatele.

Metody

Equals(Object)

Určí, zda se zadaný objekt rovná aktuálnímu objektu.

(Zděděno od Object)
GetHashCode()

Slouží jako výchozí hashovací funkce.

(Zděděno od Object)
GetType()

Získá aktuální Type instanci.

(Zděděno od Object)
MemberwiseClone()

Vytvoří mělkou kopii aktuálního Objectsouboru .

(Zděděno od Object)
RaiseExceptionIfNecessary()

Vyvolá výjimku zadanou uživatelem, pokud asynchronní operace selhala.

(Zděděno od AsyncCompletedEventArgs)
ToString()

Vrátí řetězec, který představuje aktuální objekt.

(Zděděno od Object)

Platí pro

Viz také