AsyncCompletedEventArgs 类

定义

方法名称 Completed 事件提供数据。Provides data for the MethodNameCompleted event.

public ref class AsyncCompletedEventArgs : EventArgs
public class AsyncCompletedEventArgs : EventArgs
type AsyncCompletedEventArgs = class
    inherit EventArgs
Public Class AsyncCompletedEventArgs
Inherits EventArgs
继承
AsyncCompletedEventArgs
派生

示例

下面的代码示例演示如何使用 AsyncOperation 跟踪异步操作的生存期。The following code example demonstrates using an AsyncOperation to track the lifetime of asynchronous operations. 此代码示例是为类提供的更大示例的一部分 System.ComponentModel.AsyncOperationManagerThis code example is part of a larger example provided for the System.ComponentModel.AsyncOperationManager class.

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

注解

如果你使用的是实现基于事件的异步模式概述的类,则类将提供一个方法名称 Completed 事件。If you are using a class that implements the Event-based Asynchronous Pattern Overview, the class will provide a MethodNameCompleted event. 如果将委托的实例添加 System.ComponentModel.AsyncCompletedEventHandler 到事件中,则将在 AsyncCompletedEventArgs 相应的事件处理程序方法的参数中收到异步操作结果的相关信息。If you add an instance of the System.ComponentModel.AsyncCompletedEventHandler delegate to the event, you will receive information about the outcome of asynchronous operations in the AsyncCompletedEventArgs parameter of the corresponding event-handler method.

客户端应用程序的事件处理程序委托可以检查 Cancelled 属性来确定异步任务是否已取消。The client application's event-handler delegate can check the Cancelled property to determine if the asynchronous task was cancelled.

客户端应用程序的事件处理程序委托可以检查 Error 属性,以确定是否在执行异步任务期间发生了异常。The client application's event-handler delegate can check the Error property to determine if an exception occurred during execution of the asynchronous task.

如果类支持多个异步方法或对同一个异步方法的多个调用,则可以通过检查属性的值来确定引发方法名称 Completed 事件的任务 UserStateIf the class supports multiple asynchronous methods, or multiple calls to the same asynchronous method, you can determine which task raised the MethodNameCompleted event by checking the value of the UserState property. 你的代码需要跟踪这些标记(称为任务 Id),因为它们对应的异步任务会开始和完成。Your code will need to track these tokens, known as task IDs, as their corresponding asynchronous tasks start and complete.

继承者说明

遵循基于事件的异步模式的类可以引发事件,以通知客户端有关挂起的异步操作的状态。Classes that follow the Event-based Asynchronous Pattern can raise events to alert clients about the status of pending asynchronous operations. 如果类提供方法名称 Completed 事件,则可以使用 AsyncCompletedEventArgs 来告知客户端有关异步操作的结果。If the class provides a MethodNameCompleted event, you can use the AsyncCompletedEventArgs to tell clients about the outcome of asynchronous operations.

你可能想要与客户端进行有关异步操作结果的详细信息 AsyncCompletedEventArgsYou may want to communicate to clients more information about the outcome of an asynchronous operation than an AsyncCompletedEventArgs accommodates. 在这种情况下,你可以从类派生你自己的类 AsyncCompletedEventArgs ,并提供附加的私有实例变量和对应的只读公共属性。In this case, you can derive your own class from the AsyncCompletedEventArgs class and provide additional private instance variables and corresponding read-only public properties. RaiseExceptionIfNecessary()如果操作被取消或发生错误,则在返回属性值之前调用方法。Call the RaiseExceptionIfNecessary() method before returning the property value, in case the operation was canceled or an error occurred.

构造函数

AsyncCompletedEventArgs()
已过时。

初始化 AsyncCompletedEventArgs 类的新实例。Initializes a new instance of the AsyncCompletedEventArgs class.

AsyncCompletedEventArgs(Exception, Boolean, Object)

初始化 AsyncCompletedEventArgs 类的新实例。Initializes a new instance of the AsyncCompletedEventArgs class.

属性

Cancelled

获取一个值,该值指示异步操作是否已被取消。Gets a value indicating whether an asynchronous operation has been canceled.

Error

获取一个值,该值指示异步操作期间发生的错误。Gets a value indicating which error occurred during an asynchronous operation.

UserState

获取异步任务的唯一标识符。Gets the unique identifier for the asynchronous task.

方法

Equals(Object)

确定指定对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetHashCode()

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
RaiseExceptionIfNecessary()

如果异步操作失败,则引发用户提供的异常。Raises a user-supplied exception if an asynchronous operation failed.

ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(继承自 Object)

适用于

另请参阅