Export (0) Print
Expand All

AsyncOperationManager Class

Provides concurrency management for classes that support asynchronous method calls. This class cannot be inherited.

System.Object
  System.ComponentModel.AsyncOperationManager

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

'Declaration
<HostProtectionAttribute(SecurityAction.LinkDemand, SharedState := True)> _
Public NotInheritable Class AsyncOperationManager

The AsyncOperationManager type exposes the following members.

  NameDescription
Public propertyStatic memberSynchronizationContextGets or sets the synchronization context for the asynchronous operation.
Top

  NameDescription
Public methodStatic memberCreateOperationReturns an System.ComponentModel.AsyncOperation for tracking the duration of a particular asynchronous operation.
Top

If your class needs to provide asynchronous behavior according to the Event-based Asynchronous Pattern Overview, you will encounter a number of concurrency management issues. Among these is the requirement to ensure that event handlers are called on a thread or context that is appropriate for the application model (for example, Windows Forms applications, ASP.NET applications, console applications, and so on). The AsyncOperationManager provides a convenient way to create a class that runs properly under all application models supported by the .NET Framework.

The AsyncOperationManager class has one method, CreateOperation, which returns an System.ComponentModel.AsyncOperation that can be used to track the duration of a particular asynchronous task. The System.ComponentModel.AsyncOperation for a task can be used to alert clients when a task completes. It can also be used to post progress updates and incremental results without terminating the operation.

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

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.

The following code example demonstrates using the AsyncOperationManager class to create a class that supports asynchronous operations for any application model. It shows how to implement a class that tests a number to determine whether it is prime. This calculation can be time consuming, so it is done on a separate thread. Progress reports, incremental results, and completion notifications are handled by the AsyncOperation class, which ensures that the client's event handlers are called on the proper thread or context.

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 Overridable Sub CalculatePrimeAsync( _
    ByVal numberToTest As Integer, _
    ByVal taskId As Object)

    ' Create an AsyncOperation for taskId. 
    Dim asyncOp As AsyncOperation = _
        AsyncOperationManager.CreateOperation(taskId)

    ' Multiple threads will access the task dictionary, 
    ' so it must be locked to serialize access. 
    SyncLock userStateToLifetime.SyncRoot
        If userStateToLifetime.Contains(taskId) Then 
            Throw New ArgumentException( _
                "Task ID parameter must be unique", _
                "taskId")
        End If

        userStateToLifetime(taskId) = asyncOp
    End SyncLock 

    ' Start the asynchronous operation. 
    Dim workerDelegate As New WorkerEventHandler( _
        AddressOf CalculateWorker)

    workerDelegate.BeginInvoke( _
        numberToTest, _
        asyncOp, _
        Nothing, _
        Nothing)

End Sub

.NET Framework

Supported in: 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 Silverlight 8.1, Windows Phone Silverlight 8

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