AsyncOperationManager 类

定义

提供支持异步方法调用的类的并发管理。Provides concurrency management for classes that support asynchronous method calls. 无法继承此类。This class cannot be inherited.

public ref class AsyncOperationManager abstract sealed
public static class AsyncOperationManager
type AsyncOperationManager = class
Public Class AsyncOperationManager
继承
AsyncOperationManager

示例

下面的代码示例演示如何使用 AsyncOperationManager 类创建支持任何应用程序模型的异步操作的类。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. 进度报告、增量结果和完成通知由 AsyncOperation 类处理,这可确保在适当的线程或上下文中调用客户端的事件处理程序。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 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);
}
' 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

注解

如果你的类需要根据基于事件的异步模式概述来提供异步行为,你会遇到许多并发管理问题。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. 在这两种情况下,需要确保在适合应用程序模型的线程或上下文中调用事件处理程序(例如 Windows 窗体应用程序、ASP.NETASP.NET 应用程序、控制台应用程序,等等)。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.NETASP.NET applications, console applications, and so on). AsyncOperationManager 提供了一种简便的方法来创建在 .NET Framework.NET Framework支持的所有应用程序模型下正常运行的类。The AsyncOperationManager provides a convenient way to create a class that runs properly under all application models supported by the .NET Framework.NET Framework.

AsyncOperationManager 类有一种方法,CreateOperation,该方法返回可用于跟踪特定异步任务的持续时间的 System.ComponentModel.AsyncOperationThe 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. 任务的 System.ComponentModel.AsyncOperation 可用于在任务完成时向客户端发出警报。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.

属性

SynchronizationContext

获取或设置用于异步操作的同步上下文。Gets or sets the synchronization context for the asynchronous operation.

方法

CreateOperation(Object)

返回可用于对特定异步操作的持续时间进行跟踪的 AsyncOperationReturns an AsyncOperation for tracking the duration of a particular asynchronous operation.

适用于

另请参阅