AsyncCompletedEventArgs.RaiseExceptionIfNecessary 메서드

정의

비동기 작업에 실패한 경우 사용자가 제공한 예외를 발생시킵니다.

protected:
 void RaiseExceptionIfNecessary();
protected void RaiseExceptionIfNecessary ();
member this.RaiseExceptionIfNecessary : unit -> unit
Protected Sub RaiseExceptionIfNecessary ()

예외

Cancelled 속성은 true입니다.

Error 속성이 비동기 작업에서 설정된 경우 InnerException 속성에는 Error에 대한 참조가 들어 있습니다.

예제

다음 코드 예제에서는 파생 클래스 속성에서 를 사용하는 RaiseExceptionIfNecessary 방법을 보여 줍니다.

public class CalculatePrimeCompletedEventArgs :
    AsyncCompletedEventArgs
{
    private int numberToTestValue = 0;
    private int firstDivisorValue = 1;
    private bool isPrimeValue;

    public CalculatePrimeCompletedEventArgs(
        int numberToTest,
        int firstDivisor,
        bool isPrime,
        Exception e,
        bool canceled,
        object state) : base(e, canceled, state)
    {
        this.numberToTestValue = numberToTest;
        this.firstDivisorValue = firstDivisor;
        this.isPrimeValue = isPrime;
    }

    public int NumberToTest
    {
        get
        {
            // Raise an exception if the operation failed or 
            // was canceled.
            RaiseExceptionIfNecessary();

            // If the operation was successful, return the 
            // property value.
            return numberToTestValue;
        }
    }

    public int FirstDivisor
    {
        get
        {
            // Raise an exception if the operation failed or 
            // was canceled.
            RaiseExceptionIfNecessary();

            // If the operation was successful, return the 
            // property value.
            return firstDivisorValue;
        }
    }

    public bool IsPrime
    {
        get
        {
            // Raise an exception if the operation failed or 
            // was canceled.
            RaiseExceptionIfNecessary();

            // If the operation was successful, return the 
            // property value.
            return isPrimeValue;
        }
    }
}
Public Class CalculatePrimeCompletedEventArgs
    Inherits AsyncCompletedEventArgs
    Private numberToTestValue As Integer = 0
    Private firstDivisorValue As Integer = 1
    Private isPrimeValue As Boolean


    Public Sub New( _
    ByVal numberToTest As Integer, _
    ByVal firstDivisor As Integer, _
    ByVal isPrime As Boolean, _
    ByVal e As Exception, _
    ByVal canceled As Boolean, _
    ByVal state As Object)

        MyBase.New(e, canceled, state)
        Me.numberToTestValue = numberToTest
        Me.firstDivisorValue = firstDivisor
        Me.isPrimeValue = isPrime

    End Sub


    Public ReadOnly Property NumberToTest() As Integer
        Get
            ' Raise an exception if the operation failed 
            ' or was canceled.
            RaiseExceptionIfNecessary()

            ' If the operation was successful, return 
            ' the property value.
            Return numberToTestValue
        End Get
    End Property


    Public ReadOnly Property FirstDivisor() As Integer
        Get
            ' Raise an exception if the operation failed 
            ' or was canceled.
            RaiseExceptionIfNecessary()

            ' If the operation was successful, return 
            ' the property value.
            Return firstDivisorValue
        End Get
    End Property


    Public ReadOnly Property IsPrime() As Boolean
        Get
            ' Raise an exception if the operation failed 
            ' or was canceled.
            RaiseExceptionIfNecessary()

            ' If the operation was successful, return 
            ' the property value.
            Return isPrimeValue
        End Get
    End Property
End Class

상속자 참고

클래스에서 고유한 클래스를 AsyncCompletedEventArgs 파생한 경우 읽기 전용 속성은 속성 값을 반환하기 전에 메서드를 호출 RaiseExceptionIfNecessary() 해야 합니다. 구성 요소의 비동기 작업자 코드가 속성에 예외를 Error 할당하거나 속성을 true로 설정 Cancelled 하면 클라이언트가 해당 값을 읽으려고 하면 속성이 예외를 발생합니다. 이렇게 하면 클라이언트가 비동기 작업의 오류로 인해 유효하지 않을 수 있는 속성에 액세스할 수 없습니다.

적용 대상

추가 정보