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

注釈

クラスがイベント ベースの非同期パターンの概要に従って非同期動作を提供する必要がある場合は、コンカレンシー管理の問題が多数発生します。 これらの中で、アプリケーション モデルに適したスレッドまたはコンテキスト (アプリケーション、ASP.NET アプリケーション、コンソール アプリケーションなど Windows フォーム) に適したスレッドまたはコンテキストでイベント ハンドラーを呼び出す必要があります。 これはAsyncOperationManager、.NET Frameworkでサポートされているすべてのアプリケーション モデルで適切に実行されるクラスを作成する便利な方法を提供します。

この AsyncOperationManager クラスには 1 つのメソッドがあり、 CreateOperation特定の System.ComponentModel.AsyncOperation 非同期タスクの期間を追跡するために使用できるメソッドを返します。 System.ComponentModel.AsyncOperationタスクの場合は、タスクが完了したときにクライアントに警告するために使用できます。 また、操作を終了せずに、進行状況の更新と増分結果を投稿するためにも使用できます。

非同期クラスの実装の詳細については、「 イベント ベースの非同期パターンの実装」を参照してください。

プロパティ

SynchronizationContext

非同期操作の同期コンテキストを取得または設定します。

メソッド

CreateOperation(Object)

特定の非同期操作の存続期間を追跡するために使用する AsyncOperation を返します。

適用対象

こちらもご覧ください