Export (0) Print
Expand All

AsyncOperation Class

Tracks the lifetime of an asynchronous operation.

System.Object
  System.ComponentModel.AsyncOperation

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

[HostProtectionAttribute(SecurityAction.LinkDemand, SharedState = true)]
public sealed class AsyncOperation

The AsyncOperation type exposes the following members.

  NameDescription
Public propertySynchronizationContextGets the SynchronizationContext object that was passed to the constructor.
Public propertyUserSuppliedStateGets or sets an object used to uniquely identify an asynchronous operation.
Top

  NameDescription
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodOperationCompletedEnds the lifetime of an asynchronous operation.
Public methodPostInvokes a delegate on the thread or context appropriate for the application model.
Public methodPostOperationCompletedEnds the lifetime of an asynchronous operation.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

When you implement a class according to the Event-based Asynchronous Pattern Overview, you may need 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 allow 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.

NoteNote

The HostProtectionAttribute attribute applied to this type or member has the following Resources property value: SharedState. The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes.

Notes to Inheritors

Implementers must ensure 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 implementing asynchronous classes, see Implementing the Event-based Asynchronous Pattern.

The following code example demonstrates using 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.

For a full code listing, see How to: Implement a Component That Supports the Event-based Asynchronous Pattern. For a full code listing of a client form, see How to: Implement a Client of the Event-based Asynchronous Pattern.

// 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.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2014 Microsoft