非同步程式設計模式Asynchronous programming patterns

.NET 提供三種模式來執行非同步作業:.NET provides three patterns for performing asynchronous operations:

  • 工作架構非同步模式 (TAP) ,使用單一方法來代表非同步作業的起始與完成。Task-based Asynchronous Pattern (TAP), which uses a single method to represent the initiation and completion of an asynchronous operation. TAP 是在 .NET Framework 4 中引進。TAP was introduced in the .NET Framework 4. 它是在 .NET 中進行非同步程式設計的建議方法。It's the recommended approach to asynchronous programming in .NET. C# 中的 asyncawait 關鍵字,以及 Visual Basic 中的 AsyncAwait 運算子,都加入對 TAP 的語言支援。The async and await keywords in C# and the Async and Await operators in Visual Basic add language support for TAP. 如需詳細資訊,請參閱以工作為基礎的非同步模式 (TAP)For more information, see Task-based Asynchronous Pattern (TAP).

  • 事件架構非同步模式 (EAP) ,這是用於提供非同步行為的事件架構傳統模型。Event-based Asynchronous Pattern (EAP), which is the event-based legacy model for providing asynchronous behavior. 它需要一個具有 Async 尾碼的方法、一或多個事件,事件處理常式委派類型,以及 EventArg 衍生類型。It requires a method that has the Async suffix and one or more events, event handler delegate types, and EventArg-derived types. 在 .NET Framework 2.0 中採用了 EAP。EAP was introduced in the .NET Framework 2.0. 在新的程式開發時,不再建議使用它。It's no longer recommended for new development. 如需詳細資訊,請參閱事件架構非同步模式 (EAP)For more information, see Event-based Asynchronous Pattern (EAP).

  • 非同步程式設計模型 (APM) 模式 (亦稱為 IAsyncResult 模式),它是使用 IAsyncResult 介面來提供非同步行為的傳統模型。Asynchronous Programming Model (APM) pattern (also called the IAsyncResult pattern), which is the legacy model that uses the IAsyncResult interface to provide asynchronous behavior. 在此模式中,同步作業需要 BeginEnd 方法 (例如,BeginWriteEndWrite 來實作非同步寫入作業)。In this pattern, synchronous operations require Begin and End methods (for example, BeginWrite and EndWrite to implement an asynchronous write operation). 在新的程式開發時,不建議使用此模式。This pattern is no longer recommended for new development. 如需詳細資訊,請參閱非同步程式設計模型 (APM)For more information, see Asynchronous Programming Model (APM).

模式的比較Comparison of patterns

若要快速比較三個模式如何建立非同步作業的模型,請考慮 Read 方法,它會將指定的資料量讀取到所提供的緩衝區,並且在指定的位移處開始:For a quick comparison of how the three patterns model asynchronous operations, consider a Read method that reads a specified amount of data into a provided buffer starting at a specified offset:

public class MyClass  
{  
    public int Read(byte [] buffer, int offset, int count);  
}  

此方法的 TAP 對應項會公開下列單一 ReadAsync 方法:The TAP counterpart of this method would expose the following single ReadAsync method:

public class MyClass  
{  
    public Task<int> ReadAsync(byte [] buffer, int offset, int count);  
}  

EAP 對應項會公開下列類型和成員的集合:The EAP counterpart would expose the following set of types and members:

public class MyClass  
{  
    public void ReadAsync(byte [] buffer, int offset, int count);  
    public event ReadCompletedEventHandler ReadCompleted;  
}  

APM 對應項會公開 BeginReadEndRead 方法:The APM counterpart would expose the BeginRead and EndRead methods:

public class MyClass  
{  
    public IAsyncResult BeginRead(  
        byte [] buffer, int offset, int count,   
        AsyncCallback callback, object state);  
    public int EndRead(IAsyncResult asyncResult);  
}  

另請參閱See also