AsyncOperationManager.CreateOperation(Object) 方法    
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
返回可用于对特定异步操作的持续时间进行跟踪的 AsyncOperation。
public:
 static System::ComponentModel::AsyncOperation ^ CreateOperation(System::Object ^ userSuppliedState);public static System.ComponentModel.AsyncOperation CreateOperation (object userSuppliedState);public static System.ComponentModel.AsyncOperation CreateOperation (object? userSuppliedState);static member CreateOperation : obj -> System.ComponentModel.AsyncOperationPublic Shared Function CreateOperation (userSuppliedState As Object) As AsyncOperation参数
- userSuppliedState
- Object
一个对象,用于使一个客户端状态(如任务 ID)与一个特定异步操作相关联。
返回
可用于对异步方法调用的持续时间进行跟踪的 AsyncOperation。
示例
下面的代码示例演示如何使用 CreateOperation 方法创建 用于 System.ComponentModel.AsyncOperation 跟踪异步操作持续时间的 。 此代码示例是为 AsyncOperationManager 类提供的一个更大示例的一部分。
// 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
注解
方法 CreateOperation 返回一个 System.ComponentModel.AsyncOperation ,可用于跟踪特定异步操作的持续时间,并在操作完成时向应用程序模型发出警报。 还可以使用它来发布进度更新和增量结果,而无需终止操作。 会将 System.ComponentModel.AsyncOperation 这些调用正确封送给应用程序模型的相应线程或上下文。
如果实现支持基于事件的异步模式的类,则每次调用 MethodNameAsync 方法时,类都应调用CreateOperation。 调用 方法的客户端应用程序可以使用 userSuppliedState 参数来唯一标识每个调用,以便区分在执行异步操作期间引发的事件。
注意
客户端代码必须为 参数提供唯一值 userSuppliedState 。 非唯一任务 ID 可能会导致实现错误地报告进度和其他事件。 代码应检查非唯一任务 ID,如果检测到任务 ID,则引发 System.ArgumentException 。
代码应跟踪 返回CreateOperation的每个System.ComponentModel.AsyncOperation操作,并在相应的基础异步操作中使用 对象来发布更新并终止操作。 此跟踪可以像在委托之间传递 System.ComponentModel.AsyncOperation 参数一样简单。 在更复杂的设计中,类可以维护对象的集合 System.ComponentModel.AsyncOperation ,在任务启动时添加对象,并在任务完成或取消时删除它们。 此方法允许为唯一userSuppliedState参数值检查,并且是使用支持多个并发调用的类时应使用的方法。
有关实现异步类的详细信息,请参阅 实现基于事件的异步模式。