AsyncCompletedEventArgs.RaiseExceptionIfNecessary 메서드

정의

비동기 작업에 실패한 경우 사용자가 제공한 예외를 발생시킵니다.Raises a user-supplied exception if an asynchronous operation failed.

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

예외

Cancelled 속성이 true일 경우(이 속성은The Cancelled property is true.

Error 속성이 비동기 작업에서 설정된 경우The Error property has been set by the asynchronous operation. InnerException 속성에는 Error에 대한 참조가 들어 있습니다.The InnerException property holds a reference to Error.

예제

다음 코드 예제에서는 RaiseExceptionIfNecessary 파생 된 클래스 속성입니다.The following code example demonstrates using RaiseExceptionIfNecessary in derived class properties.

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() 속성 값을 반환 하기 전에 메서드.If you have derived your own class from the AsyncCompletedEventArgs class, your read-only properties should call the RaiseExceptionIfNecessary() method before returning the property value. 작업자 코드 구성 요소의 비동기 예외가 할당 합니다 Error 가져오거나 속성을 Cancelled 속성을 true, 속성은 클라이언트에서 해당 값을 읽을 하려고 하면 예외가 발생 합니다.If the component's asynchronous worker code assigns an exception to the Error property or sets the Cancelled property to true, the property will raise an exception if a client tries to read its value. 이렇게 하면 클라이언트를 잠재적으로 잘못 된 비동기 작업에서 오류로 인해 속성에 액세스 하지 못하도록 않습니다.This prevents clients from accessing properties that are potentially not valid due to a failure in the asynchronous operation.

적용 대상

추가 정보