System.ComponentModel.AsyncOperationManager.CreateOperation Method

Returns an System.ComponentModel.AsyncOperation for tracking the duration of a particular asynchronous operation.

Syntax

public static AsyncOperation CreateOperation (object userSuppliedState)

Parameters

userSuppliedState
An object used to associate a piece of client state, such as a task ID, with a particular asynchronous operation.

Returns

An System.ComponentModel.AsyncOperation that you can use to track the duration of an asynchronous method invocation.

Remarks

The AsyncOperationManager.CreateOperation(object) method returns an System.ComponentModel.AsyncOperation that you can use to track the duration of a particular asynchronous operation and to alert the application model when the operation completes. You can also use it to post progress updates and incremental results without terminating the operation. The System.ComponentModel.AsyncOperation will correctly marshal these calls to the appropriate thread or context for the application model.

If you implement a class that supports the Event-based Asynchronous Pattern, your class should call AsyncOperationManager.CreateOperation(object) each time your MethodNameAsync method is called. The client application that makes calls to the method can use the userSuppliedState parameter to uniquely identify each invocation, so as to distinguish events raised during the execution of the asynchronous operation.

Note:

Client code must provide a unique value for the userSuppliedState parameter. Non-unique task IDs may cause your implementation to report progress and other events incorrectly. Your code should check for a non-unique task ID and throw an ArgumentException if one is detected.

Your code should track every System.ComponentModel.AsyncOperation returned by AsyncOperationManager.CreateOperation(object) and use the object in the corresponding underlying asynchronous operation to post updates and terminate the operation. This tracking can be as simple as passing the System.ComponentModel.AsyncOperation as a parameter among delegates. In more sophisticated designs, your class can maintain a collection of System.ComponentModel.AsyncOperation objects, adding objects when tasks are started and removing them when tasks are completed or canceled. This approach allows you to check for unique userSuppliedState parameter values, and is the method you should use when working with classes that support multiple concurrent invocations.

For more information about implementing asynchronous classes, see Implementing the Event-based Asynchronous Pattern.

Requirements

Namespace: System.ComponentModel
Assembly: System (in System.dll)
Assembly Versions: 2.0.0.0, 4.0.0.0
Since: .NET 2.0