通过


AsyncOperationManager 类

定义

为支持异步方法调用的类提供并发管理。 无法继承此类。

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

示例

下面的代码示例演示如何使用该 AsyncOperationManager 类创建支持任何应用程序模型的异步操作的类。 它演示如何实现一个测试数字的类,以确定它是否为质。 此计算可能很耗时,因此可以在单独的线程上完成。 进度报告、增量结果和完成通知由 AsyncOperation 类处理,这可确保在正确的线程或上下文上调用客户端的事件处理程序。

有关客户端表单的完整代码列表,请参阅 如何:实现基于事件的异步模式的客户端

// 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",
                nameof(taskId));
        }

        userStateToLifetime[taskId] = asyncOp;
    }

    // Start the asynchronous operation.
    WorkerEventHandler workerDelegate = new(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

注解

如果类需要根据 基于事件的异步模式概述提供异步行为,你将遇到许多并发管理问题。 其中要求确保在适用于应用程序模型的线程或上下文上调用事件处理程序(例如,Windows 窗体应用程序、ASP.NET 应用程序、控制台应用程序等)。 AsyncOperationManager提供了一种便捷的方法,用于创建在 .NET Framework 支持的所有应用程序模型下正常运行的类。

AsyncOperationManager 类有一种方法, CreateOperation该方法返回 System.ComponentModel.AsyncOperation 可用于跟踪特定异步任务的持续时间。 任务 System.ComponentModel.AsyncOperation 可用于在任务完成时向客户端发出警报。 它还可用于发布进度更新和增量结果,而无需终止操作。

有关实现异步类的详细信息,请参阅 实现基于事件的异步模式

属性

名称 说明
SynchronizationContext

获取或设置异步操作的同步上下文。

方法

名称 说明
CreateOperation(Object)

返回跟踪特定异步操作持续时间的一个 AsyncOperation

适用于

另请参阅