Skip to content

Lesson 12. Asynchronous programming

kudriako edited this page Apr 13, 2019 · 5 revisions

Introduction

If you have any I/O-bound needs (such as requesting data from a network or accessing a database), you'll want to utilize asynchronous programming. You could also have CPU-bound code, such as performing an expensive calculation, which is also a good scenario for writing async code.

.NET provides three patterns for performing asynchronous operations.

  • Event-based Asynchronous Pattern (EAP)
  • Asynchronous Programming Model (APM)
  • Task-based Asynchronous Pattern (TAP)

Event-based Asynchronous Pattern

The Event-based Asynchronous Pattern (EAP) was introduced with .NET 2.0.

The major advance of this pattern over the delegate-based asynchronous pattern (DAP) is that it allows us to pass around a synchronization context. It requires a method that has the Async suffix and one or more events, event handler delegate types, and EventArg-derived types.

However, although it’s easy to consume by a client simply subscribing to events, the EAP introduces a lot of overhead and complexity for the asynchronous operation author. It's no longer recommended for new development.

Asynchronous Programming Model

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, 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.

Task-based Asynchronous Pattern

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. 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).

Clone this wiki locally