RunWorkerCompletedEventArgs RunWorkerCompletedEventArgs RunWorkerCompletedEventArgs RunWorkerCompletedEventArgs Class

Definizione

Specifica dati per l'evento MethodNameCompleted.Provides data for the MethodNameCompleted event.

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

Esempi

Nell'esempio di codice riportato di seguito viene illustrato RunWorkerCompletedEventArgsl'utilizzo di.The following code example illustrates the use of RunWorkerCompletedEventArgs. Questo esempio fa parte di un esempio più ampio per BackgroundWorker la classe.This example is part of a larger sample for the BackgroundWorker class.

// 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, un Windows Forms form o un controllo avvia un'operazione asincrona chiamando BackgroundWorker.RunWorkerAsync il metodo.When using the event-based asynchronous pattern for asynchronous operations, a Windows Forms form or control initiates an asynchronous operation by calling the BackgroundWorker.RunWorkerAsync method. Il metodo a sua volta genera BackgroundWorker.DoWork l'evento in modo asincrono e lo DoWorkEventArgs passa a un'istanza.The method in turn raises the BackgroundWorker.DoWork event asynchronously and passes it a DoWorkEventArgs instance. Se l'operazione asincrona restituisce un valore, il BackgroundWorker.DoWork gestore eventi lo assegna in genere DoWorkEventArgs.Result alla proprietà.If the asynchronous operation returns a value, the BackgroundWorker.DoWork event handler typically assigns it to the DoWorkEventArgs.Result property. Quando l'operazione asincrona viene completata, viene BackgroundWorker.RunWorkerCompleted generato l'evento a cui viene passata RunWorkerCompletedEventArgs un'istanza di che contiene informazioni sullo stato dell'operazione (se è stata annullata, con errori o è stata completata correttamente).When the asynchronous operation completes, the BackgroundWorker.RunWorkerCompleted event is raised and is passed a RunWorkerCompletedEventArgs instance that contains information about the status of the operation (whether it was cancelled, faulted, or completed successfully). Se è stata completata correttamente, Result la relativa proprietà contiene il valore restituito dall'operazione asincrona e precedentemente assegnato DoWorkEventArgs.Result alla proprietà.If it completed successfully, its Result property contains the value returned by the asynchronous operation and previously assigned to the DoWorkEventArgs.Result property.

Nota

L' HostProtectionAttribute attributo applicato a questa classe presenta il valore Resources della proprietà seguente SharedState:.The HostProtectionAttribute attribute applied to this class has the following Resources property value: 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.The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). Per ulteriori informazioni, vedere la HostProtectionAttribute classe o SQL Server la programmazione e gli attributi di protezione host.For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes.

Costruttori

RunWorkerCompletedEventArgs(Object, Exception, Boolean) RunWorkerCompletedEventArgs(Object, Exception, Boolean) RunWorkerCompletedEventArgs(Object, Exception, Boolean) RunWorkerCompletedEventArgs(Object, Exception, Boolean)

Inizializza una nuova istanza della classe RunWorkerCompletedEventArgs.Initializes a new instance of the RunWorkerCompletedEventArgs class.

Proprietà

Cancelled Cancelled Cancelled Cancelled

Ottiene un valore che indica se un'operazione asincrona è stata annullata.Gets a value indicating whether an asynchronous operation has been canceled.

(Inherited from AsyncCompletedEventArgs)
Error Error Error Error

Ottiene un valore che indica l'errore verificatosi durante un'operazione asincrona.Gets a value indicating which error occurred during an asynchronous operation.

(Inherited from AsyncCompletedEventArgs)
Result Result Result Result

Ottiene un valore che rappresenta il risultato di un'operazione asincrona.Gets a value that represents the result of an asynchronous operation.

UserState UserState UserState UserState

Ottiene un valore che rappresenta lo stato dell'utente.Gets a value that represents the user state.

Metodi

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

Determina se l'oggetto specificato è uguale all'oggetto corrente.Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

Funge da funzione hash predefinita.Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

Ottiene l'oggetto Type dell'istanza corrente.Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

Crea una copia superficiale dell'oggetto Object corrente.Creates a shallow copy of the current Object.

(Inherited from Object)
RaiseExceptionIfNecessary() RaiseExceptionIfNecessary() RaiseExceptionIfNecessary() RaiseExceptionIfNecessary()

Genera un'eccezione fornita dall'utente in caso di errore in un'operazione asincrona.Raises a user-supplied exception if an asynchronous operation failed.

(Inherited from AsyncCompletedEventArgs)
ToString() ToString() ToString() ToString()

Restituisce una stringa che rappresenta l'oggetto corrente.Returns a string that represents the current object.

(Inherited from Object)

Si applica a

Vedi anche