AsyncCompletedEventArgs.Error 속성

정의

비동기 작업 중 발생한 오류를 나타내는 값을 가져옵니다.

public:
 property Exception ^ Error { Exception ^ get(); };
public Exception Error { get; }
public Exception? Error { get; }
member this.Error : Exception
Public ReadOnly Property Error As Exception

속성 값

Exception

비동기 작업 중 오류가 발생했으면 Exception 인스턴스이고, 그렇지 않으면 null입니다.

예제

다음 코드 예제에서는 비동기 작업의 수명을 추적 하는 데 사용 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

설명

비동기 작업 중에 예외가 발생하는 경우 클래스는 속성에 예외를 Error 할당합니다. 클라이언트 애플리케이션의 이벤트 처리기 대리자를 확인 해야 합니다 Error 에서 파생 된 클래스의 모든 속성에 액세스 하기 전에 속성 AsyncCompletedEventArgs고, 그렇지 않으면 속성에서 발생을 TargetInvocationException 사용 하 여 해당 InnerException 속성 보유를 에 대 한 참조 Error합니다.

속성 값 Error 은 작업이 취소된 경우입니다 null .

상속자 참고

파생 클래스에서 읽기 전용 속성을 제공하는 경우 속성 구현에서 메서드를 RaiseExceptionIfNecessary() 호출해야 합니다. 이렇게 하면 클라이언트가 비동기 작업의 오류로 인해 유효하지 않은 속성에 액세스할 수 없습니다.

적용 대상

추가 정보