AsyncOperation クラス
アセンブリ: System (system.dll 内)


イベントベースの非同期パターンの概要に従ってクラスを実装する場合、使用するクラスのインスタンスで呼び出される各非同期操作の有効期間を追跡することが必要な場合があります。AsyncOperation クラスを使用すると、非同期タスクの進行状況を追跡して報告できます。
AsyncOperation オブジェクトの使用方法を次に示します。
クラスでは、各タスクの開始時に AsyncOperationManager.CreateOperation を呼び出すことによって、各非同期タスクの AsyncOperation オブジェクトを取得する必要があります。クライアントが個々の非同期タスクを区別できるようにするために、AsyncOperationManager.CreateOperation は、クライアントが提供する一意のトークンに対するパラメータを取得します。これは、UserSuppliedState プロパティになります。クライアント コードは、このプロパティを使用して、進行状況イベントまたは完了イベントを発生させる特定の非同期タスクを識別できます。
![]() |
---|
このクラスに適用される HostProtectionAttribute 属性の Resources プロパティの値は、SharedState です。HostProtectionAttribute は、デスクトップ アプリケーション (一般的には、アイコンをダブルクリック、コマンドを入力、またはブラウザに URL を入力して起動するアプリケーション) には影響しません。詳細については、HostProtectionAttribute クラスのトピックまたは「SQL Server プログラミングとホスト保護属性」を参照してください。 |

AsyncOperation オブジェクトを使用して、非同期操作の有効期間を追跡するコード例を次に示します。このコード例は、System.ComponentModel.AsyncOperationManager クラスのトピックで取り上げているコード例の一部分です。
完全なコードの一覧については、「方法 : イベントベースの非同期パターンをサポートするコンポーネントを実装する」を参照してください。クライアント フォームのコード全体については、「方法 : イベントベースの非同期パターンのクライアントを実装する」を参照してください。
' This method starts the 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. workerDelegate = New WorkerEventHandler( _ AddressOf CalculateWorker) workerDelegate.BeginInvoke( _ numberToTest, _ asyncOp, _ completionMethodDelegate, _ Nothing, _ Nothing) End Sub
// This method starts the 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. workerDelegate = new WorkerEventHandler(CalculateWorker); workerDelegate.BeginInvoke( numberToTest, asyncOp, completionMethodDelegate, null, null); }
public void CalculatePrimeAsync(int numberToTest, Object taskId) { // State must be unique. AsyncOperation asyncOp = AsyncOperationManager.CreateOperation(taskId); synchronized (userStateToLifetime.get_SyncRoot()) { if (userStateToLifetime.Contains(taskId)) { throw new ArgumentException("Task ID parameter must be unique", "taskId"); } userStateToLifetime.set_Item(taskId, asyncOp); } // Start the asynchronous operation. workerDelegate = new WorkerEventHandler(CalculateWorker); workerDelegate.BeginInvoke(numberToTest, asyncOp, completionMethodDelegate, null, null); } //CalculatePrimeAsync
public void CalculatePrimeAsync(int numberToTest, Object taskId) { // State must be unique. AsyncOperation asyncOp = AsyncOperationManager.CreateOperation(taskId); synchronized (userStateToLifetime.get_SyncRoot()) { if (userStateToLifetime.Contains(taskId)) { throw new System.ArgumentException( "Task ID parameter must be unique", "taskId"); } userStateToLifetime.set_Item(taskId, asyncOp); } // Start the asynchronous operation. workerDelegate = new WorkerEventHandler(CalculateWorker); workerDelegate.BeginInvoke(numberToTest, asyncOp, completionMethodDelegate, null, null); } //CalculatePrimeAsync

System.ComponentModel.AsyncOperation


Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- AsyncOperation クラスのページへのリンク