AsyncCompletedEventArgs.Error Property
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Gets a value that indicates which error occurred during an asynchronous operation.
Assembly: System (in System.dll)
Property Value
Type: System.ExceptionAn Exception instance, if an error occurred during an asynchronous operation; otherwise null.
If an exception is raised during an asynchronous operation, the class will assign the exception to the Error property. The client application's event-handler delegate should check the Error property before accessing any properties in a class derived from AsyncCompletedEventArgs. Otherwise, the property will raise a TargetInvocationException with its InnerException property holding a reference to Error.
The value of the Error property is null if the operation was canceled.
Notes to InheritorsIf you provide read-only properties in a derived class, make sure that you call the RaiseExceptionIfNecessary method in your property implementation. This prevents clients from accessing properties that are potentially not valid because of a failure in the asynchronous operation.
The following code example demonstrates the using the Error property to inform a client that an asynchronous operation failed. This code example is part of a larger example provided for the System.ComponentModel.AsyncOperationManager class.
// The worker process for data retrieval. Simulates a // time-consuming operation by using Thread.Sleep. public void GetPersons (int itemsCount, string Name, AsyncOperation asyncOperation) { bool canceled = false; Exception exception = null; List<Person> personList = new List<Person>(); for (int i = 1; i <= itemsCount || itemsCount == 0; i++) { string currentName = Name + i; personList.Add(new Person() { Name = currentName, Age = i, Birthday = DateTime.Today.AddYears(-i), Available = (i % 2 == 0) }); // Delay 1 second for each person. Thread.Sleep(1000); // Report progress by using AsyncOperation to raise // the ProgressChanged event. int percentComplete = 0; try { percentComplete = Convert.ToInt32(i * 100 / itemsCount); } catch (Exception ex) { exception = ex; break; } GetPersonsProgressChangedEventArgs progressChangedEventArgs = new GetPersonsProgressChangedEventArgs( currentName, percentComplete, asyncOperation.UserSuppliedState); asyncOperation.Post(AsyncOpProgressReportHandler, progressChangedEventArgs); if (GetPersonsCheckForCancellation (asyncOperation.UserSuppliedState)) { canceled = true; break; } } // Report completion by raising the Completed event. GetPersonsCompletedEventArgs completedEventArgs = new GetPersonsCompletedEventArgs( personList, exception, canceled, asyncOperation.UserSuppliedState); asyncOperation.PostOperationCompleted (AsyncOpCompletedHandler, completedEventArgs); }