AsyncOperationManager.CreateOperation(Object) 方法

定義

傳回 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.AsyncOperation
Public 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 。 非唯一的工作標識碼可能會導致您的實作報告進度和其他事件不正確。 您的程式代碼應該檢查是否有非唯一的工作識別碼,並在偵測到工作識別子時擲 System.ArgumentException 回 。

您的程式代碼應該追蹤 所CreateOperation傳回的每個 System.ComponentModel.AsyncOperation ,並在對應的基礎異步操作中使用 對象來張貼更新並終止作業。 此追蹤可以像在委派之間傳遞 System.ComponentModel.AsyncOperation 做為參數一樣簡單。 在更複雜的設計中,您的類別可以維護物件的集合 System.ComponentModel.AsyncOperation ,並在工作完成或取消時新增物件,並在工作完成或取消時加以移除。 此方法可讓您檢查唯一 userSuppliedState 的參數值,而且是使用支援多個並行調用的類別時應該使用的方法。

如需實作異步類別的詳細資訊,請參閱 實作事件架構異步模式

適用於

另請參閱