AsyncOperationManager.CreateOperation Method

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

Namespace:  System.ComponentModel
Assembly:  System (in System.dll)

public static AsyncOperation CreateOperation(
	Object userSuppliedState
)

Parameters

userSuppliedState
Type: System.Object

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

Return Value

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

The CreateOperation 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 CreateOperation 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.

Caution noteCaution

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 System.ArgumentException if one is detected.

Your code should track every System.ComponentModel.AsyncOperation returned by CreateOperation 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.

The following code example demonstrates using the CreateOperation method to create an System.ComponentModel.AsyncOperation for tracking the duration of asynchronous operations. This code example is part of a larger example provided for the AsyncOperationManager class.

// This method starts an asynchronous calculation.  
// First, it checks the supplied task ID for uniqueness. 
// If taskId is unique, it creates a new WorkerEventHandler  
// and calls its BeginInvoke method to start the calculation. 
public virtual void CalculatePrimeAsync(
    int numberToTest,
    object taskId)
{
    // Create an AsyncOperation for taskId.
    AsyncOperation asyncOp =
        AsyncOperationManager.CreateOperation(taskId);

    // Multiple threads will access the task dictionary, 
    // so it must be locked to serialize access. 
    lock (userStateToLifetime.SyncRoot)
    {
        if (userStateToLifetime.Contains(taskId))
        {
            throw new ArgumentException(
                "Task ID parameter must be unique", 
                "taskId");
        }

        userStateToLifetime[taskId] = asyncOp;
    }

    // Start the asynchronous operation.
    WorkerEventHandler workerDelegate = new WorkerEventHandler(CalculateWorker);
    workerDelegate.BeginInvoke(
        numberToTest,
        asyncOp,
        null,
        null);
}

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft