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", 
                "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

備註

如果您的類別需要根據 事件架構非同步模式概觀提供非同步行為,您將會遇到一些並行管理問題。 其中一項是確保事件處理常式在適用于應用程式模型 (的執行緒或內容上呼叫的需求,例如,Windows Forms應用程式、ASP.NET 應用程式、主控台應用程式等等) 。 AsyncOperationManager提供方便的方式,可讓您建立類別,以在.NET Framework支援的所有應用程式模型下正確執行。

類別 AsyncOperationManager 有一個方法, CreateOperation 它會傳 System.ComponentModel.AsyncOperation 回 可用來追蹤特定非同步工作持續時間的 。 當 System.ComponentModel.AsyncOperation 工作完成時,工作的 可用來警示用戶端。 它也可以用來張貼進度更新和累加結果,而不需要終止作業。

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

屬性

SynchronizationContext

取得或設定非同步作業的同步處理內容。

方法

CreateOperation(Object)

傳回 AsyncOperation,以追蹤特定非同步作業的持續期間。

適用於

另請參閱