AsyncOperationManager 類別

定義

為支援非同步方法呼叫的類別,提供並行管理。Provides concurrency management for classes that support asynchronous method calls. 這個類別無法被繼承。This class cannot be inherited.

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

範例

下列程式碼範例將示範如何AsyncOperationManager使用類別來建立支援任何應用程式模型之非同步作業的類別。The following code example demonstrates using the AsyncOperationManager class to create a class that supports asynchronous operations for any application model. 它會示範如何執行測試數位的類別, 以判斷它是否為質數。It shows how to implement a class that tests a number to determine whether it is prime. 這種計算可能相當耗時, 因此會在個別的執行緒上完成。This calculation can be time consuming, so it is done on a separate thread. 「進度報表」、「累加結果」和「完成」 AsyncOperation通知會由類別處理, 以確保在適當的執行緒或內容上呼叫用戶端的事件處理常式。Progress reports, incremental results, and completion notifications are handled by the AsyncOperation class, which ensures that the client's event handlers are called on the proper thread or context.

如需完整的程式代碼清單, 請參閱如何:實作支援事件架構非同步模式的元件For a full code listing, see How to: Implement a Component That Supports the Event-based Asynchronous Pattern. 如需用戶端表單的完整程式代碼清單, 請參閱如何:實作事件架構非同步模式的用戶端For a full code listing of a client form, see How to: Implement a Client of the Event-based Asynchronous Pattern.

// 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

備註

如果您的類別需要根據事件架構非同步模式總覽來提供非同步行為, 您將會遇到一些並行管理問題。If your class needs to provide asynchronous behavior according to the Event-based Asynchronous Pattern Overview, you will encounter a number of concurrency management issues. 在這些情況下, 您必須確定事件處理常式是在適用于應用程式模型的執行緒或內容上呼叫 (例如, Windows Forms 應用程式、 ASP.NETASP.NET應用程式、主控台應用程式等)。Among these is the requirement to ensure that event handlers are called on a thread or context that is appropriate for the application model (for example, Windows Forms applications, ASP.NETASP.NET applications, console applications, and so on). 提供一個便利的方式, 讓您建立可在所支援的.NET Framework.NET Framework所有應用程式模型底下正常執行的類別。 AsyncOperationManagerThe AsyncOperationManager provides a convenient way to create a class that runs properly under all application models supported by the .NET Framework.NET Framework.

類別有一個方法, CreateOperationSystem.ComponentModel.AsyncOperation會傳回, 它可以用來追蹤特定非同步工作的持續時間。 AsyncOperationManagerThe AsyncOperationManager class has one method, CreateOperation, which returns an System.ComponentModel.AsyncOperation that can be used to track the duration of a particular asynchronous task. 工作System.ComponentModel.AsyncOperation完成時, 可以使用工作的來警示用戶端。The System.ComponentModel.AsyncOperation for a task can be used to alert clients when a task completes. 它也可以用來張貼進度更新和累加結果, 而不需要終止作業。It can also be used to post progress updates and incremental results without terminating the operation.

如需有關如何執行非同步類別的詳細資訊, 請參閱執行以事件為基礎的非同步模式For more information about implementing asynchronous classes, see Implementing the Event-based Asynchronous Pattern.

屬性

SynchronizationContext

取得或設定非同步作業的同步處理內容。Gets or sets the synchronization context for the asynchronous operation.

方法

CreateOperation(Object)

傳回 AsyncOperation,以追蹤特定非同步作業的持續期間。Returns an AsyncOperation for tracking the duration of a particular asynchronous operation.

適用於

另請參閱