AsyncCompletedEventArgs.RaiseExceptionIfNecessary 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
如果非同步作業失敗,引發使用者提供的例外狀況。
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為 ,則客戶端嘗試讀取其值時,屬性會引發例外狀況。 這可防止用戶端存取因異步操作失敗而可能無效的屬性。