Events
17 Mar, 23 - 21 Mar, 23
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
.NET provides three patterns for performing asynchronous operations:
Task-based Asynchronous Pattern (TAP), which uses a single method to represent the initiation and completion of an asynchronous operation. TAP was introduced in .NET Framework 4. It's the recommended approach to asynchronous programming in .NET. The async and await keywords in C# and the Async and Await operators in Visual Basic add language support for TAP. For more information, see Task-based Asynchronous Pattern (TAP).
Event-based Asynchronous Pattern (EAP), which is the event-based legacy model for providing asynchronous behavior. It requires a method that has the Async
suffix and one or more events, event handler delegate types, and EventArg
-derived types. EAP was introduced in .NET Framework 2.0. It's no longer recommended for new development. For more information, see Event-based Asynchronous Pattern (EAP).
Asynchronous Programming Model (APM) pattern (also called the IAsyncResult pattern), which is the legacy model that uses the IAsyncResult interface to provide asynchronous behavior. In this pattern, asynchronous 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. For more information, see Asynchronous Programming Model (APM).
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 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);
}
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 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);
}
.NET feedback
.NET is an open source project. Select a link to provide feedback:
Events
17 Mar, 23 - 21 Mar, 23
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowTraining
Module
Apply interfaces in code - Training
Discover how default implementations in interfaces enable you to add new methods directly into an interface and provide a default implementation.
Documentation
Task-based Asynchronous Pattern (TAP): Introduction and overview - .NET
Learn about the Task-based Asynchronous Pattern (TAP), and compare it to the legacy patterns: Asynchronous Programming Model (APM) and Event-based Asynchronous Pattern (EAP).
Advanced .NET programming documentation
Learn about advanced programming concepts in .NET such as threading, parallel programming, asynchronous programming models, native interop, and memory management.
Implementing the Task-based Asynchronous Pattern - .NET
This article explains how to implement the Task-based Asynchronous Pattern. You can use it to implement compute-bound and I/O-bound asynchronous operations.