History of Asynchronous Programming Models in .NET framework

.NET Framework has a rich history of asynchronous programming models starting from .NET framework 1.1 to .NET Framework 4.5. In this post i’ll be discussing briefly on these programming models and in future posts I’ll delve little more into each one of them.

For comparison purpose lets consider we are writing a time consuming API called ImportEmployees which will import employee details from an external medium into our Employee database.Synchronous version of this would have a signature like below.

public class EmployeeImport

        public int ImportEmployeeData(Employee employee);


Asynchronous Programming Model (APM) in .NET Framework 1.1

.NET framework 1.1 introduced something called Asynchronous Programming Model where we can have asynchronous versions of synchronous method implemented as a set of two methods called Begin<MethodName> and End<MethodName>.For example Socket class in System.Net.Sockets namespace implemented methods BeginSend/EndSend ,BeginReceive /EndReceive etc. This model is based on IAsyncResult pattern.Basically Begin method is used to start the asynchronous operations and End method acts as a callback method and is called when the asynchronous operation completes.Visit this link for more details.

Below is the APM version of ImportEmployeeData method signature.

public class EmployeeImport

        public IAsyncResult BeginImportEmployeeData(Employee employee,
               AsyncCallback callback, object state);
        public int EndImportEmployeeData(IAsyncResult asyncResult);


Event Asynchronous Pattern (EAP) in .NET Framework 2.0

This is the model where applications implemented multiple <MethodName>Async methods which will execute an operation in separate thread and use events to notify progress and completion status.One very common example for this pattern is BackgroundWorker class which is used to execute an operation in separate thread and  use events for updating the status.It has methods like  RunWorkerAsync which starts the operation and events like OnProgressChanged ,OnRunWorkerCompleted etc for updating the progress status.

BackgroundWorker class is commonly used in winodws application for executing task triggered from UI controls without blocking the UI thread.Visit this link for more details.

Below is the EAP version of ImportEmployeeData method signature.

public class EmployeeImport

        public void ImportEmployeeDataAsync(Employee employee);
        public event ImportEmployeeDataCompletedEventHandler ImportCompleted;


Note : Task Parallel Library or TPL introduced in .NET Framework 4.0 can be used with both of the above patterns to implement APIs which expose Tasks

Task based Asynchronous Pattern (TAP) in .NET Framework 4.5

This is the latest and recommended model of asynchronous programming introduced in .NET Framework 4.5.This uses newly introduced keywords async and await in C# 5.0 (Async  and Await  in VB.NET)

This pattern provides a non-blocking framework where long running requests don’t block the current thread which can continue doing next set of tasks till the time async request completes.Async methods by convention use Async as suffix e.g. <MethodName>Async will be the asynchronous version of <MethodName> method.Async method  uses Task or Task<TResult> return types.

.NET Framework 4.5 has incorporated lot of existing classes with Async version of existing synchronous methods e.g. CopyToAsync , ReadAsync, and WriteAsync method in System.IO.Stream class.

Below is the TAP version of ImportEmployeeData method signature.

public class EmployeeImport
        public Task<int> ImportEmployeeDataAsync(Employee employee);

Note : Although Async / Await are language features you still need .Net framework 4.5 as it exposes some of the APIs required for them to work properly.In case you want to use it with .Net framework 4.0 you can use nuget package available at following link (called Microsoft.Bcl.Async)

Each of these models are still perfectly valid and can be implemented in various scenarios but TAP is the recommended model for implementing asynchronous pattern in .NET applications which are developed newly and target framework version 4.5 or higher (MSDN Reference).

Recommended Books on Amazon :

Pro Asynchronous Programming with .NET

C# 5.0 Pocket Reference: Instant Help for C# 5.0 Programmers

Concurrency in C# Cookbook

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.