How different async programming is from Threads? How different async programming is from Threads? asp.net asp.net

How different async programming is from Threads?


Threads are not necessary for asynchronous programming.

"Asynchronous" means that the API doesn't block the calling thread. It does not mean that there is another thread that is blocking.

First, consider your UI example, this time using actual asynchronous APIs:

  1. You have UI Thread to take input, give output.
  2. You can handle things in UI Thread but it makes the UI unresponsive.
  3. So lets say we have a stream-related operation and we need to download some sort of data.
  4. And we also allow users to do other things while it is being downloaded.
  5. We use asynchronous APIs to download the file. No worker thread is necessary.
  6. The asynchronous operation reports its progress back to the UI thread (which updates the progress bar), and it also reports its completion to the UI thread (which can respond to it like any other event).

This shows how there can be only one thread involved (the UI thread), yet also have asynchronous operations going on. You can start up multiple asynchronous operations and yet only have one thread involved in those operations - no threads are blocked on them.

async/await provides a very nice syntax for starting an asynchronous operation and then returning, and having the rest of the method continue when that operation completes.

ASP.NET is similar, except it doesn't have a main/UI thread. Instead, it has a "request context" for every incomplete request. ASP.NET threads come from a thread pool, and they enter the "request context" when they work on a request; when they're done, they exit their "request context" and return to the thread pool.

ASP.NET keeps track of incomplete asynchronous operations for each request, so when a thread returns to the thread pool, it checks to see if there are any asynchronous operations in progress for that request; if there are none, then the request is complete.

So, when you await an incomplete asynchronous operation in ASP.NET, the thread will increment that counter and return. ASP.NET knows the request isn't complete because the counter is non-zero, so it doesn't finish the response. The thread returns to the thread pool, and at that point: there are no threads working on that request.

When the asynchronous operation completes, it schedules the remainder of the async method to the request context. ASP.NET grabs one of its handler threads (which may or may not be the same thread that executed the earlier part of the async method), the counter is decremented, and the thread executes the async method.

ASP.NET vNext is slightly different; there's more support for asynchronous handlers throughout the framework. But the general concept is the same.

For more information:


First time when I saw async and await, I thougth they were C# Syntactic sugar for Asynchronous Programming Model. I was wrong, async and await are more than that. It is a brand new asynchronous pattern Task-based Asynchronous Pattern, http://www.microsoft.com/en-us/download/details.aspx?id=19957 is a good article to get start. Most of the FCL classes which inplement TAP are call APM methods (BegingXXX() and EndXXX()). Here are two code snaps for TAP and AMP:

TAP sample:

    static void Main(string[] args)    {        GetResponse();        Console.ReadLine();    }    private static async Task<WebResponse> GetResponse()    {        var webRequest = WebRequest.Create("http://www.google.com");        Task<WebResponse> response = webRequest.GetResponseAsync();        Console.WriteLine(new StreamReader(response.Result.GetResponseStream()).ReadToEnd());        return response.Result;    }

APM sample:

    static void Main(string[] args)    {        var webRequest = WebRequest.Create("http://www.google.com");        webRequest.BeginGetResponse(EndResponse, webRequest);        Console.ReadLine();    }    static void EndResponse(IAsyncResult result)    {        var webRequest = (WebRequest) result.AsyncState;        var response = webRequest.EndGetResponse(result);        Console.WriteLine(new StreamReader(response.GetResponseStream()).ReadToEnd());    }

Finally these two will be the same, because GetResponseAsync() call BeginGetResponse() and EndGetResponse() inside. When we reflector the source code of GetResponseAsync(), we will get code like this:

task = Task<WebResponse>.Factory.FromAsync(       new Func<AsyncCallback, object, IAsyncResult>(this.BeginGetResponse),        new Func<IAsyncResult, WebResponse>(this.EndGetResponse), null);

For APM, in the BeginXXX(), there is an argument for a callback method which will invoked when the task (typically is an IO heavy operation) was completed. Creating a new thread and asynchronous, both of them will immediately return in main thread, both of them are unblocked. On performance side, creating new thread will cost more resource when process I/O-bound operations such us read file, database operation and network read. There are two disadvantages in creating new thread,

  1. like in your mentioned article, there are memory cost and CLR are
    limitation on thread pool.
  2. Context switch will happen. On the other hander, asynchronous willnot create any thread manually and it will not have context switchwhen the the IO-bound operations return.

Here is an picture which can help to understand the differences:

enter image description here

This diagram is from a MSDN article "Asynchronous Pages in ASP.NET 2.0", which explain very detail about how the old asynchronous working in ASP.NET 2.0.

About Asynchronous Programming Model, please get more detail from Jeffrey Richter's article "Implementing the CLR Asynchronous Programming Model", also there are more detail on his book "CLR via Csharp 3rd Edition" in chapter 27.


Let’s imagine that you are implementing a web application and as each client request comes in toyour server, you need to make a database request. When a client request comes in, a thread poolthread will call into your code. If you now issue a database request synchronously, the thread will blockfor an indefinite amount of time waiting for the database to respond with the result. If during this timeanother client request comes in, the thread pool will have to create another thread and again thisthread will block when it makes another database request. As more and more client requests come in,more and more threads are created, and all these threads block waiting for the database to respond.The result is that your web server is allocating lots of system resources (threads and their memory) thatare barely even used!And to make matters worse, when the database does reply with the various results, threads becomeunblocked and they all start executing. But since you might have lots of threads running and relativelyfew CPU cores, Windows has to perform frequent context switches, which hurts performance evenmore. This is no way to implement a scalable application.

To read data from the file, I now call ReadAsync instead of Read. ReadAsync internally allocates aTask object to represent the pending completion of the read operation. Then, ReadAsynccalls Win32’s ReadFile function (#1). ReadFile allocates its IRP, initializes it just like it did in thesynchronous scenario (#2), and then passes it down to the Windows kernel (#3). Windows adds the IRPto the hard disk driver’s IRP queue (#4), but now, instead of blocking your thread, your thread isallowed to return to your code; your thread immediately returns from its call to ReadAsync (#5, #6,and #7). Now, of course, the IRP has not necessarily been processed yet, so you cannot have code afterReadAsync that attempts to access the bytes in the passed-in Byte[].