AsyncOperationManager AsyncOperationManager AsyncOperationManager AsyncOperationManager Class

定義

為支援非同步方法呼叫的類別,提供並行管理。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
繼承
AsyncOperationManagerAsyncOperationManagerAsyncOperationManagerAsyncOperationManager

範例

下列程式碼範例示範如何使用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.

如需完整的程式碼清單,請參閱How to:實作支援事件架構非同步模式的元件For a full code listing, see How to: Implement a Component That Supports the Event-based Asynchronous Pattern. 如需完整的程式碼的清單用戶端,請參閱How to:實作事件架構非同步模式的用戶端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). AsyncOperationManager提供便利的方式建立所支援的所有應用程式模型下正常執行的類別.NET Framework.NET FrameworkThe AsyncOperationManager provides a convenient way to create a class that runs properly under all application models supported by the .NET Framework.NET Framework.

AsyncOperationManager類別具有一個方法CreateOperation,以傳回System.ComponentModel.AsyncOperation,可用來追蹤特定非同步工作的持續時間。The 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 SynchronizationContext SynchronizationContext SynchronizationContext

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

方法

CreateOperation(Object) CreateOperation(Object) CreateOperation(Object) CreateOperation(Object)

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

適用於

另請參閱