RunWorkerCompletedEventArgs Classe

Definizione

Fornisce i dati per l'evento MethodNameCompleted .

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

Esempio

Nell'esempio di codice seguente viene illustrato l'uso di RunWorkerCompletedEventArgs. Questo esempio fa parte di un esempio più ampio per la BackgroundWorker classe .

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

Commenti

Quando si usa il modello asincrono basato su eventi per le operazioni asincrone, una maschera o un controllo Windows Forms avvia un'operazione asincrona chiamando il BackgroundWorker.RunWorkerAsync metodo . Il metodo genera l'evento BackgroundWorker.DoWork in modo asincrono e lo passa a un'istanza DoWorkEventArgs . Se l'operazione asincrona restituisce un valore, il BackgroundWorker.DoWork gestore eventi in genere lo assegna alla DoWorkEventArgs.Result proprietà . Al termine dell'operazione asincrona, viene generato l'evento BackgroundWorker.RunWorkerCompleted e viene passata un'istanza RunWorkerCompletedEventArgs che contiene informazioni sullo stato dell'operazione , indipendentemente dal fatto che sia stato annullato, generato o completato correttamente. Se l'operazione è stata completata correttamente, la relativa Result proprietà contiene il valore restituito dall'operazione asincrona e precedentemente assegnato alla DoWorkEventArgs.Result proprietà .

Nota

L'attributo HostProtectionAttribute applicato a questa classe ha il valore della proprietà seguente Resources : SharedState. L'oggetto HostProtectionAttribute non influisce sulle applicazioni desktop, che in genere vengono avviate facendo doppio clic sull'icona, digitando un comando oppure immettendo un URL in un browser. Per altre informazioni, vedere la HostProtectionAttribute classe o gli attributi di programmazione e protezione host SQL Server.

Costruttori

RunWorkerCompletedEventArgs(Object, Exception, Boolean)

Inizializza una nuova istanza della classe RunWorkerCompletedEventArgs.

Proprietà

Cancelled

Ottiene un valore che indica se un'operazione asincrona è stata annullata.

(Ereditato da AsyncCompletedEventArgs)
Error

Ottiene un valore che indica l'errore verificatosi durante un'operazione asincrona.

(Ereditato da AsyncCompletedEventArgs)
Result

Ottiene un valore che rappresenta il risultato di un'operazione asincrona.

UserState

Ottiene un valore che rappresenta lo stato dell'utente.

Metodi

Equals(Object)

Determina se l'oggetto specificato è uguale all'oggetto corrente.

(Ereditato da Object)
GetHashCode()

Funge da funzione hash predefinita.

(Ereditato da Object)
GetType()

Ottiene l'oggetto Type dell'istanza corrente.

(Ereditato da Object)
MemberwiseClone()

Crea una copia superficiale dell'oggetto Object corrente.

(Ereditato da Object)
RaiseExceptionIfNecessary()

Genera un'eccezione fornita dall'utente in caso di errore in un'operazione asincrona.

(Ereditato da AsyncCompletedEventArgs)
ToString()

Restituisce una stringa che rappresenta l'oggetto corrente.

(Ereditato da Object)

Si applica a

Vedi anche