AsyncOperationManager.CreateOperation(Object) 方法

定義

回傳 和 AsyncOperation 以追蹤特定非同步操作的持續時間。

public:
 static System::ComponentModel::AsyncOperation ^ CreateOperation(System::Object ^ userSuppliedState);
public static System.ComponentModel.AsyncOperation CreateOperation(object userSuppliedState);
static member CreateOperation : obj -> System.ComponentModel.AsyncOperation
Public Shared Function CreateOperation (userSuppliedState As Object) As AsyncOperation

參數

userSuppliedState
Object

一個用來將用戶端狀態(如任務 ID)與特定非同步操作關聯的物件。

傳回

你可以 AsyncOperation 用來追蹤非同步方法調用的持續時間。

範例

以下程式碼範例示範如何使用此 CreateOperation 方法建立 System.ComponentModel.AsyncOperation for 追蹤非同步操作的持續時間。 此程式碼範例是本類別更大範例 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",
                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

備註

該方法回 CreateOperation 傳 , System.ComponentModel.AsyncOperation 你可以用來追蹤特定非同步操作的持續時間,並在操作完成時提醒應用程式模型。 你也可以用它來發布進度更新和增量結果,而不必終止操作。 他們 System.ComponentModel.AsyncOperation 會正確地將這些呼叫導管到適合應用模型的執行緒或上下文。

如果你實作一個支援事件導向非同步模式的類別,每次呼叫 MethodNameAsync 方法時,你的類別都應該CreateOperation呼叫。 呼叫該方法的客戶端應用程式可利用該 userSuppliedState 參數唯一識別每個調用,以區分執行非同步操作時所產生的事件。

Caution

用戶端程式碼必須為參數提供唯一值 userSuppliedState 。 非唯一任務 ID 可能導致實作錯誤回報進度及其他事件。 你的程式碼應該會檢查非唯一任務 ID,如果偵測到會拋出一個。System.ArgumentException

你的程式碼應該追蹤每個 System.ComponentModel.AsyncOperation 回傳, CreateOperation 並在相應的非同步操作中使用該物件來發布更新並終止該操作。 這種追蹤可以簡單到將 作為參數傳遞 System.ComponentModel.AsyncOperation 給代表。 在更複雜的設計中,你的類別可以維護一組 System.ComponentModel.AsyncOperation 物件,任務開始時新增物件,任務完成或取消時移除。 這種方法可以檢查唯一的 userSuppliedState 參數值,也是處理支援多個並行調用類別時應該採用的方法。

欲了解更多關於實作非同步類別的資訊,請參閱 「實作基於事件的非同步模式」。

適用於

另請參閱