AsyncCompletedEventArgs.Error Propriété
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.
Obtient une valeur indiquant quelle erreur s’est produite pendant une opération asynchrone.
public:
property Exception ^ Error { Exception ^ get(); };
public Exception Error { get; }
public Exception? Error { get; }
member this.Error : Exception
Public ReadOnly Property Error As Exception
Valeur de propriété
Instance Exception , si une erreur s’est produite pendant une opération asynchrone ; sinon null.
Exemples
L’exemple de code suivant illustre l’utilisation d’un AsyncOperation modèle pour suivre la durée de vie des opérations asynchrones. Cet exemple de code fait partie d’un exemple plus grand fourni pour la System.ComponentModel.AsyncOperationManager classe.
using System;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
using System.Threading;
using System.Windows.Forms;
Imports System.Collections
Imports System.Collections.Specialized
Imports System.ComponentModel
Imports System.Drawing
Imports System.Globalization
Imports System.Threading
Imports System.Windows.Forms
// This event handler updates the ListView control when the
// PrimeNumberCalculator raises the CalculatePrimeCompleted
// event. The ListView item is updated with the appropriate
// outcome of the calculation: Canceled, Error, or result.
void primeNumberCalculator1_CalculatePrimeCompleted(
object sender,
CalculatePrimeCompletedEventArgs e)
{
Guid taskId = (Guid)e.UserState;
if (e.Cancelled)
{
string result = "Canceled";
ListViewItem lvi = UpdateListViewItem(taskId, result);
if (lvi != null)
{
lvi.BackColor = Color.Pink;
lvi.Tag = null;
}
}
else if (e.Error != null)
{
string result = "Error";
ListViewItem lvi = UpdateListViewItem(taskId, result);
if (lvi != null)
{
lvi.BackColor = Color.Red;
lvi.ForeColor = Color.White;
lvi.Tag = null;
}
}
else
{
bool result = e.IsPrime;
ListViewItem lvi = UpdateListViewItem(
taskId,
result,
e.FirstDivisor);
if (lvi != null)
{
lvi.BackColor = Color.LightGray;
lvi.Tag = null;
}
}
}
' This event handler updates the ListView control when the
' PrimeNumberCalculator raises the CalculatePrimeCompleted
' event. The ListView item is updated with the appropriate
' outcome of the calculation: Canceled, Error, or result.
Private Sub primeNumberCalculator1_CalculatePrimeCompleted( _
ByVal sender As Object, _
ByVal e As CalculatePrimeCompletedEventArgs) _
Handles primeNumberCalculator1.CalculatePrimeCompleted
Dim taskId As Guid = CType(e.UserState, Guid)
If e.Cancelled Then
Dim result As String = "Canceled"
Dim lvi As ListViewItem = UpdateListViewItem( _
taskId, _
result)
If (lvi IsNot Nothing) Then
lvi.BackColor = Color.Pink
lvi.Tag = Nothing
End If
ElseIf e.Error IsNot Nothing Then
Dim result As String = "Error"
Dim lvi As ListViewItem = UpdateListViewItem( _
taskId, result)
If (lvi IsNot Nothing) Then
lvi.BackColor = Color.Red
lvi.ForeColor = Color.White
lvi.Tag = Nothing
End If
Else
Dim result As Boolean = e.IsPrime
Dim lvi As ListViewItem = UpdateListViewItem( _
taskId, _
result, _
e.FirstDivisor)
If (lvi IsNot Nothing) Then
lvi.BackColor = Color.LightGray
lvi.Tag = Nothing
End If
End If
End Sub
Remarques
Si une exception est levée pendant une opération asynchrone, la classe affecte l’exception à la Error propriété. Le délégué du gestionnaire d’événements de l’application cliente doit vérifier la Error propriété avant d’accéder à toutes les propriétés d’une classe dérivée AsyncCompletedEventArgs; sinon, la propriété génère une TargetInvocationException propriété contenant InnerException une référence à Error.
La valeur de la Error propriété est null si l’opération a été annulée.
Notes pour les héritiers
Si vous fournissez des propriétés en lecture seule dans une classe dérivée, veillez à appeler la RaiseExceptionIfNecessary() méthode dans votre implémentation de propriété. Cela empêche les clients d’accéder aux propriétés potentiellement non valides en raison d’un échec dans l’opération asynchrone.