| title | ms.custom | ms.date | ms.prod | ms.reviewer | ms.suite | ms.technology | ms.tgt_pltfrm | ms.topic | helpviewer_keywords | ms.assetid | caps.latest.revision | author | ms.author | manager | ||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Asynchronous Programming Patterns |
03/30/2017 |
.net |
dotnet-standard |
article |
|
4ece5c0b-f8fe-4114-9862-ac02cfe5a5d7 |
5 |
rpetrusha |
ronpet |
wpickett |
Asynchronous Programming Patterns
The .NET Framework provides three patterns for performing asynchronous operations:
-
Asynchronous Programming Model (APM) pattern (also called the xref:System.IAsyncResult pattern), where asynchronous operations require
BeginandEndmethods (for example,BeginWriteandEndWritefor asynchronous write operations). This pattern is no longer recommended for new development. For more information, see Asynchronous Programming Model (APM). -
Event-based Asynchronous Pattern (EAP), which requires a method that has the
Asyncsuffix, and also requires one or more events, event handler delegate types, andEventArg-derived types. EAP was introduced in the .NET Framework 2.0. It is no longer recommended for new development. For more information, see Event-based Asynchronous Pattern (EAP). -
Task-based Asynchronous Pattern (TAP), which uses a single method to represent the initiation and completion of an asynchronous operation. TAP was introduced in the .NET Framework 4 and is the recommended approach to asynchronous programming in the .NET Framework. The async and await keywords in C# and the Async and Await operators in Visual Basic Language add language support for TAP. For more information, see Task-based Asynchronous Pattern (TAP).
Comparing Patterns
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);
} The APM counterpart of this method 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);
} 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;
} The TAP counterpart would expose the following single ReadAsync method:
public class MyClass
{
public Task<int> ReadAsync(byte [] buffer, int offset, int count);
} For a comprehensive discussion of TAP, APM, and EAP, see the links provided in the next section.
Related topics
| Title | Description |
|---|---|
| Asynchronous Programming Model (APM) | Describes the legacy model that uses the xref:System.IAsyncResult interface to provide asynchronous behavior. This model is no longer recommended for new development. |
| Event-based Asynchronous Pattern (EAP) | Describes the event-based legacy model for providing asynchronous behavior. This model is no longer recommended for new development. |
| Task-based Asynchronous Pattern (TAP) | Describes the new asynchronous pattern based on the xref:System.Threading.Tasks namespace. This model is the recommended approach to asynchronous programming in the .NET Framework 4 and later versions. |
See also
Asynchronous programming in C#
Async Programming in F#
Asynchronous Programming with Async and Await (Visual Basic)