AsyncOperation Class
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Tracks the lifetime of an asynchronous operation.
Assembly: System (in System.dll)
The AsyncOperation type exposes the following members.
| Name | Description | |
|---|---|---|
![]() | SynchronizationContext | Gets the SynchronizationContext object that was passed to the constructor when the object was created by AsyncOperationManager. |
![]() | UserSuppliedState | Gets an object that is used to uniquely identify an asynchronous operation. |
| Name | Description | |
|---|---|---|
![]() | Equals(Object) | Determines whether the specified Object is equal to the current Object. (Inherited from Object.) |
![]() | Finalize | Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.) |
![]() | GetHashCode | Serves as a hash function for a particular type. (Inherited from Object.) |
![]() | GetType | Gets the Type of the current instance. (Inherited from Object.) |
![]() | MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) |
![]() | OperationCompleted | Ends the lifetime of an asynchronous operation. |
![]() | Post | Invokes a delegate on the thread or context appropriate for the application model. |
![]() | PostOperationCompleted | Ends the lifetime of an asynchronous operation. |
![]() | ToString | Returns a string that represents the current object. (Inherited from Object.) |
When you implement a class according to the Event-based Asynchronous Pattern Overview topic in the .NET Framework documentation, you may want to track the lifetime of each asynchronous operation invoked on an instance of your class. The AsyncOperation class provides ways to track and report the progress of an asynchronous task.
The following list identifies ways to use an AsyncOperation object:
To report progress and interim results to the client, call Post from your asynchronous worker code.
To indicate that an asynchronous task has completed, or to cancel a pending asynchronous task, call PostOperationCompleted.
Your class should get an AsyncOperation object for each asynchronous task by calling AsyncOperationManager::CreateOperation when each task starts. To enable the client to distinguish separate asynchronous tasks, AsyncOperationManager::CreateOperation takes a parameter for a unique client-provided token, which becomes the UserSuppliedState property. It can then be used by client code to identify the particular asynchronous task that is raising progress or completion events.
Note: |
|---|
The BackgroundWorker class provides a simpler programming model for handling asynchronous behavior. Implementing the event-based asynchronous pattern by using AsyncOperation is more complex, but also more flexible, and is necessary if you must be able to communicate between the parent class and the worker class by using custom events. By using BackgroundWorker, you are limited to its ProgressChanged and RunWorkerCompleted events. Depending on the scenario, the AsyncOperation pattern may have less overhead than using BackgroundWorker because it is delegate-based and not event-based. |
Implementers must make sure that the PostOperationCompleted and Post invocations are asynchronous, so that class library providers do not need to concern themselves with potential stack overflows if they assume asynchronous behavior in a particular application model that happens to be synchronous.
For more information about how to implement asynchronous classes, see Implementing the Event-based Asynchronous Pattern in the .NET Framework documentation.
The following code example demonstrates how to use an AsyncOperation object to track the lifetime of asynchronous operations. This code example is part of a larger example provided for the System.ComponentModel::AsyncOperationManager class.



Note: