Export (0) Print
Expand All
0 out of 3 rated this helpful - Rate this topic

AsyncOperation Class

Tracks the lifetime of an asynchronous operation.

Namespace:  System.ComponentModel
Assembly:  System (in System.dll)
[HostProtectionAttribute(SecurityAction.LinkDemand, SharedState = true)]
public sealed class AsyncOperation
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.

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.

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);
}
System.Object
  System.ComponentModel.AsyncOperation
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

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

.NET Framework

Supported in: 3.5, 3.0, 2.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.