RunWorkerCompletedEventArgs Classe
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Fournit des données pour l’événement MethodNameCompleted .
public ref class RunWorkerCompletedEventArgs : System::ComponentModel::AsyncCompletedEventArgs
public class RunWorkerCompletedEventArgs : System.ComponentModel.AsyncCompletedEventArgs
type RunWorkerCompletedEventArgs = class
inherit AsyncCompletedEventArgs
Public Class RunWorkerCompletedEventArgs
Inherits AsyncCompletedEventArgs
- Héritage
Exemples
L’exemple de code suivant illustre l’utilisation de RunWorkerCompletedEventArgs. Cet exemple fait partie d’un échantillon plus grand pour 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
Remarques
Lorsque vous utilisez le modèle asynchrone basé sur les événements pour les opérations asynchrones, un formulaire ou un contrôle Windows Forms lance une opération asynchrone en appelant la BackgroundWorker.RunWorkerAsync méthode. La méthode déclenche à son tour l’événement BackgroundWorker.DoWork de façon asynchrone et la transmet à une DoWorkEventArgs instance. Si l’opération asynchrone retourne une valeur, le BackgroundWorker.DoWork gestionnaire d’événements l’affecte généralement à la DoWorkEventArgs.Result propriété. Une fois l’opération asynchrone terminée, l’événement BackgroundWorker.RunWorkerCompleted est déclenché et passe une RunWorkerCompletedEventArgs instance qui contient des informations sur l’état de l’opération (qu’elle ait été annulée, défectueuse ou terminée avec succès). Si elle s’est terminée correctement, sa Result propriété contient la valeur retournée par l’opération asynchrone et précédemment affectée à la DoWorkEventArgs.Result propriété.
Note
L’attribut HostProtectionAttribute appliqué à cette classe a la valeur de propriété suivante Resources : SharedState. Cela HostProtectionAttribute n’affecte pas les applications de bureau (généralement démarrées en double-cliquant sur une icône, en tapant une commande ou en entrant une URL dans un navigateur). Pour plus d’informations, consultez la HostProtectionAttribute classe ou les attributs de programmation et de protection de l’hôte SQL Server.
Constructeurs
| Nom | Description |
|---|---|
| RunWorkerCompletedEventArgs(Object, Exception, Boolean) |
Initialise une nouvelle instance de la classe RunWorkerCompletedEventArgs. |
Propriétés
| Nom | Description |
|---|---|
| Cancelled |
Obtient une valeur indiquant si une opération asynchrone a été annulée. (Hérité de AsyncCompletedEventArgs) |
| Error |
Obtient une valeur indiquant quelle erreur s’est produite pendant une opération asynchrone. (Hérité de AsyncCompletedEventArgs) |
| Result |
Obtient une valeur qui représente le résultat d’une opération asynchrone. |
| UserState |
Obtient une valeur qui représente l’état utilisateur. |
Méthodes
| Nom | Description |
|---|---|
| Equals(Object) |
Détermine si l’objet spécifié est égal à l’objet actuel. (Hérité de Object) |
| GetHashCode() |
Sert de fonction de hachage par défaut. (Hérité de Object) |
| GetType() |
Obtient la Type de l’instance actuelle. (Hérité de Object) |
| MemberwiseClone() |
Crée une copie superficielle du Objectactuel. (Hérité de Object) |
| RaiseExceptionIfNecessary() |
Déclenche une exception fournie par l’utilisateur si une opération asynchrone a échoué. (Hérité de AsyncCompletedEventArgs) |
| ToString() |
Retourne une chaîne qui représente l’objet actuel. (Hérité de Object) |