RunWorkerCompletedEventArgs Klasa

Definicja

Dostarcza dane dla zdarzenia MethodNameCompleted .

public ref class RunWorkerCompletedEventArgs : System::ComponentModel::AsyncCompletedEventArgs
public class RunWorkerCompletedEventArgs : System.ComponentModel.AsyncCompletedEventArgs
type RunWorkerCompletedEventArgs = class
    inherit AsyncCompletedEventArgs
Public Class RunWorkerCompletedEventArgs
Inherits AsyncCompletedEventArgs
Dziedziczenie
RunWorkerCompletedEventArgs

Przykłady

Poniższy przykład kodu ilustruje użycie metody RunWorkerCompletedEventArgs. Ten przykład jest częścią większej próbki BackgroundWorker dla klasy .

// 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

Uwagi

W przypadku korzystania ze wzorca asynchronicznego opartego na zdarzeniach dla operacji asynchronicznych, formularz lub kontrolka Windows Forms inicjuje operację asynchroniczną przez wywołanie BackgroundWorker.RunWorkerAsync metody . Metoda z kolei wywołuje BackgroundWorker.DoWork zdarzenie asynchronicznie i przekazuje je do DoWorkEventArgs wystąpienia. Jeśli operacja asynchroniczna zwraca wartość, BackgroundWorker.DoWork procedura obsługi zdarzeń zazwyczaj przypisuje ją do DoWorkEventArgs.Result właściwości . Po zakończeniu BackgroundWorker.RunWorkerCompleted operacji asynchronicznej jest zgłaszane zdarzenie i jest przekazywane RunWorkerCompletedEventArgs wystąpienie zawierające informacje o stanie operacji (niezależnie od tego, czy został anulowany, uszkodzony, czy ukończony pomyślnie). Jeśli operacja została ukończona pomyślnie, jej Result właściwość zawiera wartość zwróconą przez operację asynchroniczną i wcześniej przypisaną DoWorkEventArgs.Result do właściwości .

Uwaga

Atrybut HostProtectionAttribute zastosowany do tej klasy ma następującą Resources wartość właściwości: SharedState. Atrybut HostProtectionAttribute nie ma wpływu na aplikacje pulpitu (które typowo są uruchamiane przez dwukrotne kliknięcie ikony, wpisanie polecenia albo wprowadzenie adresu URL w przeglądarce). Aby uzyskać więcej informacji, zobacz HostProtectionAttribute klasa lub SQL Server Programowanie i atrybuty ochrony hosta.

Konstruktory

RunWorkerCompletedEventArgs(Object, Exception, Boolean)

Inicjuje nowe wystąpienie klasy RunWorkerCompletedEventArgs.

Właściwości

Cancelled

Pobiera wartość wskazującą, czy operacja asynchroniczna została anulowana.

(Odziedziczone po AsyncCompletedEventArgs)
Error

Pobiera wartość wskazującą, który błąd wystąpił podczas operacji asynchronicznej.

(Odziedziczone po AsyncCompletedEventArgs)
Result

Pobiera wartość reprezentującą wynik operacji asynchronicznej.

UserState

Pobiera wartość reprezentującą stan użytkownika.

Metody

Equals(Object)

Określa, czy dany obiekt jest taki sam, jak bieżący obiekt.

(Odziedziczone po Object)
GetHashCode()

Służy jako domyślna funkcja skrótu.

(Odziedziczone po Object)
GetType()

Type Pobiera wartość bieżącego wystąpienia.

(Odziedziczone po Object)
MemberwiseClone()

Tworzy płytkią kopię bieżącego Objectelementu .

(Odziedziczone po Object)
RaiseExceptionIfNecessary()

Zgłasza wyjątek dostarczony przez użytkownika, jeśli operacja asynchroniczna nie powiodła się.

(Odziedziczone po AsyncCompletedEventArgs)
ToString()

Zwraca ciąg reprezentujący bieżący obiekt.

(Odziedziczone po Object)

Dotyczy

Zobacz też