次の方法で共有


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",
                nameof(taskId));
        }

        userStateToLifetime[taskId] = asyncOp;
    }

    // Start the asynchronous operation.
    WorkerEventHandler workerDelegate = new(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 フォーム アプリケーション、ASP.NET アプリケーション、コンソール アプリケーションなど) に適したスレッドまたはコンテキストでイベント ハンドラーが呼び出されるようにする必要があります。 AsyncOperationManager は、.NET Framework でサポートされているすべてのアプリケーション モデルで適切に実行されるクラスを作成する便利な方法を提供します。

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

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

プロパティ

名前 説明
SynchronizationContext

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

メソッド

名前 説明
CreateOperation(Object)

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

適用対象

こちらもご覧ください