AsyncCompletedEventArgs.Cancelled Propriété

Définition

Obtient une valeur qui indique si une opération asynchrone a été annulée.

public:
 property bool Cancelled { bool get(); };
public bool Cancelled { get; }
member this.Cancelled : bool
Public ReadOnly Property Cancelled As Boolean

Valeur de propriété

true si l'opération d'arrière-plan a été annulée ; sinon, false. La valeur par défaut est false.

Exemples

L’exemple de code suivant illustre l’utilisation d’un AsyncOperation 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.Data;
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.
private 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

Lorsque la propriété a la Cancelled valeur true, l’opération asynchrone a été interrompue.

Le délégué du gestionnaire d’événements de l’application cliente doit vérifier la Cancelled propriété avant d’accéder aux propriétés d’une classe dérivée de AsyncCompletedEventArgs; sinon, la propriété déclenche un InvalidOperationException si l’opération asynchrone a été interrompue.

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 . Cela empêche les clients d’accéder aux propriétés potentiellement non valides en raison d’un échec de l’opération asynchrone.

S’applique à

Voir aussi