Sdílet prostřednictvím


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 ilustruje 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.
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

Při použití asynchronního vzoru založeného na událostech pro asynchronní operace spustí formulář nebo ovládací prvek Windows Forms asynchronní operaci voláním BackgroundWorker.RunWorkerAsync metody. Metoda zase vyvolá BackgroundWorker.DoWork událost asynchronně a předá ji 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 je vyvolána událost a je předána RunWorkerCompletedEventArgs instance, která obsahuje informace o stavu operace (zda byla zrušena, chybována nebo úspěšně dokončena). Pokud byla úspěšně dokončena, její Result vlastnost obsahuje hodnotu vrácenou asynchronní operací a dříve přiřazena k DoWorkEventArgs.Result vlastnosti.

Poznámka:

Atribut HostProtectionAttribute použitý pro tuto třídu má následující Resources hodnotu vlastnosti: SharedState. Nemá HostProtectionAttribute vliv na desktopové aplikace (které se obvykle spouští poklikáním na ikonu, zadáním příkazu nebo zadáním adresy URL v prohlížeči). Další informace naleznete v HostProtectionAttribute tématu třídy nebo programování SYSTÉMU SQL Server a atributy ochrany hostitele.

Konstruktory

Name Description
RunWorkerCompletedEventArgs(Object, Exception, Boolean)

Inicializuje novou instanci RunWorkerCompletedEventArgs třídy.

Vlastnosti

Name Description
Cancelled

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

(Zděděno od AsyncCompletedEventArgs)
Error

Získá hodnotu urč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

Name Description
Equals(Object)

Určuje, zda je zadaný objekt roven aktuálnímu objektu.

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

Slouží jako výchozí funkce hash.

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

Získá Type aktuální instance.

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

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

(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é