AsyncCompletedEventArgs.RaiseExceptionIfNecessary Method
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Raises a user-supplied exception if an asynchronous operation failed.
Assembly: System (in System.dll)
| Exception | Condition |
|---|---|
| InvalidOperationException | The Cancelled property is true. |
| TargetInvocationException | The Error property has been set by the asynchronous operation. |
If you have derived your own class from the AsyncCompletedEventArgs class, your read-only properties should call the RaiseExceptionIfNecessary method before they return the property value. 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 because of a failure in the asynchronous operation.
The following code example demonstrates how to use RaiseExceptionIfNecessary in derived class properties. This code example is part of a larger example provided for the System.ComponentModel.AsyncOperationManager class.
// Define a custom Completed-event arguments class allowing // the data gathered by GetPersons to be returned. public class GetPersonsCompletedEventArgs : AsyncCompletedEventArgs { private List<Person> dataListValue; public GetPersonsCompletedEventArgs( List<Person> dataList, Exception error, bool cancelled, object userState) : base(error, cancelled, userState) { dataListValue = dataList; } public List<Person> DataListValue { get { this.RaiseExceptionIfNecessary(); return dataListValue; } } }