Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First time when I saw <strong>async</strong> and <strong>await</strong>, I thougth they were C# Syntactic sugar for Asynchronous Programming Model. I was wrong, <strong>async</strong> and <strong>await</strong> are more than that. It is a brand new asynchronous pattern Task-based Asynchronous Pattern, <a href="http://www.microsoft.com/en-us/download/details.aspx?id=19957" rel="nofollow noreferrer">http://www.microsoft.com/en-us/download/details.aspx?id=19957</a> 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:</p> <p>TAP sample:</p> <pre><code> static void Main(string[] args) { GetResponse(); Console.ReadLine(); } private static async Task&lt;WebResponse&gt; GetResponse() { var webRequest = WebRequest.Create("http://www.google.com"); Task&lt;WebResponse&gt; response = webRequest.GetResponseAsync(); Console.WriteLine(new StreamReader(response.Result.GetResponseStream()).ReadToEnd()); return response.Result; } </code></pre> <p>APM sample:</p> <pre><code> 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()); } </code></pre> <p>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:</p> <pre><code>task = Task&lt;WebResponse&gt;.Factory.FromAsync( new Func&lt;AsyncCallback, object, IAsyncResult&gt;(this.BeginGetResponse), new Func&lt;IAsyncResult, WebResponse&gt;(this.EndGetResponse), null); </code></pre> <p>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, </p> <ol> <li>like in your mentioned article, there are memory cost and CLR are<br> limitation on thread pool. </li> <li>Context switch will happen. On the other hander, asynchronous will not create any thread manually and it will not have context switch when the the IO-bound operations return.</li> </ol> <p>Here is an picture which can help to understand the differences:</p> <p><img src="https://i.stack.imgur.com/q3xU7.gif" alt="enter image description here"></p> <p>This diagram is from a MSDN article "<a href="http://msdn.microsoft.com/en-us/magazine/cc163725.aspx" rel="nofollow noreferrer">Asynchronous Pages in ASP.NET 2.0</a>", which explain very detail about how the old asynchronous working in ASP.NET 2.0. </p> <p>About Asynchronous Programming Model, please get more detail from Jeffrey Richter's article "<a href="http://msdn.microsoft.com/en-us/magazine/cc163467.aspx" rel="nofollow noreferrer">Implementing the CLR Asynchronous Programming Model</a>", also there are more detail on his book "CLR via Csharp 3rd Edition" in chapter 27.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload