AsyncCompletedEventArgs 類別

定義

提供MethodNameCompleted 事件的資料。

public ref class AsyncCompletedEventArgs : EventArgs
public class AsyncCompletedEventArgs : EventArgs
type AsyncCompletedEventArgs = class
    inherit EventArgs
Public Class AsyncCompletedEventArgs
Inherits EventArgs
繼承
AsyncCompletedEventArgs
衍生

範例

下列程式碼範例示範如何使用 AsyncOperation 來追蹤非同步作業的存留期。 此程式碼範例是提供給 類別之較大範例的 System.ComponentModel.AsyncOperationManager 一部分。

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

備註

如果您使用實作事件架構非同步模式概觀的類別,類別會提供MethodNameCompleted 事件。 如果您將委派的 System.ComponentModel.AsyncCompletedEventHandler 實例新增至 事件,您會收到對應事件處理常式方法之 參數中 AsyncCompletedEventArgs 非同步作業結果的相關資訊。

用戶端應用程式的事件處理常式委派可以檢查 Cancelled 屬性,以判斷非同步工作是否已取消。

用戶端應用程式的事件處理常式委派可以檢查 屬性, Error 以判斷非同步工作執行期間是否發生例外狀況。

如果類別支援多個非同步方法,或多次呼叫相同的非同步方法,您可以檢查 屬性的值 UserState 來判斷引發MethodNameCompleted 事件的工作。 您的程式碼必須追蹤這些權杖,稱為工作識別碼,因為其對應的非同步工作會開始和完成。

給繼承者的注意事項

遵循事件架構非同步模式的類別可以引發事件,以警示用戶端有關暫止非同步作業的狀態。 如果類別提供MethodNameCompleted 事件,您可以使用 AsyncCompletedEventArgs 來告訴用戶端非同步作業的結果。

您可能想要與用戶端通訊,以取得非同步作業結果的詳細資訊,而不是 AsyncCompletedEventArgs 因應。 在此情況下,您可以從 類別衍生自己的類別 AsyncCompletedEventArgs ,並提供額外的私用執行個體變數和對應的唯讀公用屬性。 RaiseExceptionIfNecessary()在傳回屬性值之前呼叫 方法,以防作業已取消或發生錯誤。

建構函式

AsyncCompletedEventArgs()
已淘汰.

初始化 AsyncCompletedEventArgs 類別的新執行個體。

AsyncCompletedEventArgs(Exception, Boolean, Object)

初始化 AsyncCompletedEventArgs 類別的新執行個體。

屬性

Cancelled

取得值,指出非同步作業是否已取消。

Error

取得值,指出非同步作業期間是否發生錯誤。

UserState

取得非同步工作的唯一識別項。

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
RaiseExceptionIfNecessary()

如果非同步作業失敗,引發使用者提供的例外狀況。

ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

適用於

另請參閱