异步编程模式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. EAP 是在 .NET Framework 2.0 中引入的。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