Our world is becoming globalized, distributed/clouded more and more. That is very true especially for IT in both consumer and enterprise space. Some examples would be Amazon AWS, Google Documents, Windows Azure Services, Office 365, Windows 8 UI Applications. Within this world, to develop scalable, fluent, and less-dependent/resilient applications, asynchronous programming approach can be used.
In this post, I will give you the big picture of async world (what is it, why to use, and how and when/where to use it) within .NET platform. In the next post, I will focus on MS-recommended approach (task-based asynchronous programming TAP).
In short, asynchronous programming is to enabling delegation of application process to other threads, systems, and or devices. Synchronous programs runs in a sequential manner whereas asynchronous applications can start a new operation and without waiting the new ones’ completion it can continue working on its own flow (main operation). To simplify, let's visualize a case where a person send an email and can do nothing till a response received from a sender. Here is the tasks beyond sending email are blocked by the response that you have no control over and may take for a while. What would be the asynchronous way is to send the email and continue working on other tasks while waiting the response from sender. In order to capture the full context of asynchronous programming, we need to understand roles and meanings of OS, CLR, application domain, thread, process.
Any point where your application initiates an operation that is long-running and/or Input Output-bound. For example, you may leverage asynchronous programming in the following scenarios
Let me explain it with an example: A simple application that process data from 2 Xml documents into a list. Here is the pseudo code:
Now, I am interested in only line 1-3 to differentiate use of sync and Async calls.
Here is code snippet showing the ProcessXml and Book objects and the method that does the parsing:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using System.Threading.Tasks;
namespace AsyncDemo
{
public class ProcessXml
/// <summary>
/// Parses 'book' named elements from a file into a list of Book objects
/// </summary>
/// <param name="path">file path</param>
/// <returns>List of Books obj</returns>
public static List<Book> ParseToList(string path)
//System.Threading.Thread.Sleep(1000);
if (System.IO.File.Exists(path))
XDocument doc = XDocument.Load(path);
if (doc != null)
var coll = doc.Root.Elements("book").Select(p =>
new Book
Author = p.Elements("author").First().Value,
Title = p.Elements("title").First().Value,
Genre = p.Elements("genre").First().Value,
Price = double.Parse(p.Elements("price").First().Value),
PublishDate = DateTime.Parse(p.Elements("publish_date").First().Value),
Description = p.Elements("description").First().Value
});
return coll.ToList<Book>();
}
return null;
public static Task<List<Book>> XmlProcessEngineAsync(string path)
List<Book> list;
list = doc.Root.Elements("book").Select(p =>
}).ToList<Book>();
return null; // (Task<List<Book>>)list;
public class Book
public string Author { get; set; }
public string Title { get; set; }
public string Genre { get; set; }
public double Price { get; set; }
public DateTime PublishDate { get; set; }
public string Description { get; set; }
Here are 2 unit test methods that demonstrate the scenario both sync and Async manners:
/// Tests the method synchronously
[TestMethod]
public void ParseToListTestSync()
books1 = ProcessXml.ParseToList(file1);
books2 = ProcessXml.ParseToList(file2);
var list = MergeLists(books1, books2);
Assert.IsNotNull(list);
/// Tests the method asynchronously with APM approach w/o callback
public void ParseToListTestAsyncWithAPM()
books1 = books2 = null;
DelProcessing del = new DelProcessing(ProcessXml.ParseToList);
IAsyncResult result = del.BeginInvoke(file2, null, null);
books2 = ProcessXml.ParseToList(file1);
//if (!result.IsCompleted)
//{
//this runs in main thread; do some other stuff while async method call in-progress
//Thread.Sleep(1000);
//}
books1 = del.EndInvoke(result);
Here is the screenshot from the run results
As you can see, the 1st test method calls the function in a synchronous way:
2nd test method calls the function in a asynchronous way:
Here is the picture demonstrates this:
Since the version 1.1, .NET support asynchronous programming, since then each release brought new enhancements (delegates, TPL, etc.). Here is the picture taken when searching Async methods available within With FW 4.5.
With FW 4.5, there are 3 patterns available for Async development:
APM style can be implemented in 2 ways; with or without a callback. Sample call above (ParseToListTestAsyncWithAPM) is an example for non-callback APM. We can implement same functionality with a callback as seen below:
1: /// <summary>
2: /// Tests the method asynchronously with APM callback
3: /// </summary>
4: [TestMethod]
5: public void ParseToListTestAsyncWithAPM_Callback()
6: {
7: books1 = books2 = null;
8: int t1 = System.Threading.Thread.CurrentThread.ManagedThreadId;
9: int t2 = 0;
10:
11: DelProcessing del = new DelProcessing(ProcessXml.ParseToList);
12: IAsyncResult result = del.BeginInvoke(file2, (r) =>
13: {
14: books1 = del.EndInvoke(r);
15: t2 = System.Threading.Thread.CurrentThread.ManagedThreadId;
16: }, null);
17:
18: books2 = ProcessXml.ParseToList(file1);
19:
20: var list = MergeLists(books1, books2);
21:
22: Assert.IsNotNull(list);
23: Assert.IsTrue(t2 > 0);
24: }
Let me explain this little bit more in detail:
TAP is the simplest one and is recommended by MS. Here is the code for implementing the same scenario with TAP:
2: /// Tests the method asynchronously with TAP
5: public void ParseToListTestAsyncWithTAP()
7: int t1 = System.Threading.Thread.CurrentThread.ManagedThreadId;
8: int t2 = 0;
9:
10: books1 = books2 = null;
11: Task.Factory.StartNew(()=> {
12: books1 = ProcessXml.ParseToList(file2);
13: t2 = System.Threading.Thread.CurrentThread.ManagedThreadId;
14: });
15: books2 = ProcessXml.ParseToList(file1);
16:
17: var list = MergeLists(books1, books2);
18:
19: Assert.IsNotNull(list);
20: Assert.IsTrue(t2 > 0);
21: }
TAP is hot:), will explain this in detail in my next post hopefully. For now, I would like to share the results of my efforts so far with you:
Obviously, perhaps another post would be good for comparing sync vs async or APM vs TAP by running load tests. We will see. This is a very live world/sector and there are many things to unleash, is not it?
Wow, that has been my longest post:). Forgot how fast time passed here in Robert’s Coffee in Istanbul.
Well, in this post, I have explained various aspects of asynchronous programming; meaning, differentiations, why and how-to-use. Asynchronous programming can be implemented in both client and server side and provides scalability and performance advantages over synchronous programming. I would certainly recommend you to invest some time on this, since it is now simpler (TAP) and use of it becomes almost a must-have due to more integration to cloud applications.
This confused me.
Hi Livio, can you be specific please? Thanks,
Awesome! Love the code snippets!
Awesome post thanks for sharing code snippets
<a href="www.refactorthis.net/">Asynchronous Programming</a>