AsyncOperationManager 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
為支援非同步方法呼叫的類別提供並行管理。 無法繼承這個類別。
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 Forms 應用程式、ASP.NET 應用程式、主控台應用程式等)。 AsyncOperationManager 提供了一種方便的方式,讓 .NET 框架能在所有支援的應用模型下正常執行類別。
該 AsyncOperationManager 類別有一個方法, CreateOperation為 ,可回傳 , System.ComponentModel.AsyncOperation 可用來追蹤特定非同步任務的持續時間。 任務的 for System.ComponentModel.AsyncOperation 可以用來提醒客戶任務完成。 它也可以用來發布進度更新和增量結果,而不會終止操作。
欲了解更多關於實作非同步類別的資訊,請參閱 「實作基於事件的非同步模式」。
屬性
| 名稱 | Description |
|---|---|
| SynchronizationContext |
取得或設定非同步操作的同步上下文。 |
方法
| 名稱 | Description |
|---|---|
| CreateOperation(Object) |
回傳 和 AsyncOperation 以追蹤特定非同步操作的持續時間。 |