RunWorkerCompletedEventArgs Classe
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Fornisce 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à
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.
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
Commenti
Quando si usa il modello asincrono basato su eventi per le operazioni asincrone , un modulo o un controllo Windows Form avvia un'operazione asincrona chiamando il BackgroundWorker.RunWorkerAsync metodo . Il metodo genera a sua volta 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 lo assegna in genere 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 (se è stata annullata, non riuscita o completata correttamente). Se l'operazione è stata completata correttamente, la Result relativa proprietà contiene il valore restituito dall'operazione asincrona e precedentemente assegnato alla DoWorkEventArgs.Result proprietà .
Annotazioni
L'attributo HostProtectionAttribute applicato a questa classe ha il valore della proprietà seguente Resources : SharedState. Non HostProtectionAttribute influisce sulle applicazioni desktop (che in genere vengono avviate facendo doppio clic su un'icona, digitando un comando o immettendo un URL in un browser). Per altre informazioni, vedere la HostProtectionAttribute classe o gli attributi di programmazione e protezione host di SQL Server.
Costruttori
| Nome | Descrizione |
|---|---|
| RunWorkerCompletedEventArgs(Object, Exception, Boolean) |
Inizializza una nuova istanza della classe RunWorkerCompletedEventArgs. |
Proprietà
| Nome | Descrizione |
|---|---|
| Cancelled |
Ottiene un valore che indica se un'operazione asincrona è stata annullata. (Ereditato da AsyncCompletedEventArgs) |
| Error |
Ottiene un valore che indica l'errore che si è verificato 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 utente. |
Metodi
| Nome | Descrizione |
|---|---|
| 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 il Type dell'istanza corrente. (Ereditato da Object) |
| MemberwiseClone() |
Crea una copia superficiale del Objectcorrente. (Ereditato da Object) |
| RaiseExceptionIfNecessary() |
Genera un'eccezione fornita dall'utente se un'operazione asincrona non è riuscita. (Ereditato da AsyncCompletedEventArgs) |
| ToString() |
Restituisce una stringa che rappresenta l'oggetto corrente. (Ereditato da Object) |