AsyncCompletedEventArgs.RaiseExceptionIfNecessary Yöntem

Tanım

Zaman uyumsuz bir işlem başarısız olursa kullanıcı tarafından sağlanan bir özel durum oluşturur.

C#
protected void RaiseExceptionIfNecessary ();

Özel durumlar

Error özelliği zaman uyumsuz işlem tarafından ayarlandı. InnerException özelliği için bir başvuru barındırmaktadırError.

Örnekler

Aşağıdaki kod örneği türetilmiş sınıf özelliklerinde kullanmayı RaiseExceptionIfNecessary gösterir.

C#
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;
        }
    }
}

Devralanlara Notlar

Sınıfından AsyncCompletedEventArgs kendi sınıfınızı türetdiyseniz, salt okunur özelliklerinizin özellik değerini döndürmeden önce yöntemini çağırması RaiseExceptionIfNecessary() gerekir. Bileşenin zaman uyumsuz çalışan kodu özelliğine Error bir özel durum atarsa veya özelliğini olarak trueayarlarsaCancelled, istemci değerini okumayı denerse özellik bir özel durum oluşturur. Bu, istemcilerin zaman uyumsuz işlemdeki bir hata nedeniyle geçerli olmayabilecek özelliklere erişmesini engeller.

Şunlara uygulanır

Ürün Sürümler
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

Ayrıca bkz.