<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.microsoft.co.il/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Bnaya Eshet : async, Parallel, .NET 4.5</title><link>http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/async/Parallel/.NET+4.5/default.aspx</link><description>Tags: async, Parallel, .NET 4.5</description><dc:language>en</dc:language><generator>CommunityServer 2007.1 (Build: 20917.1142)</generator><item><title>Async - Handling multiple Exceptions</title><link>http://blogs.microsoft.co.il/blogs/bnaya/archive/2012/12/10/async-handling-multiple-exceptions.aspx</link><pubDate>Mon, 10 Dec 2012 06:30:25 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:1535142</guid><dc:creator>bnaya</dc:creator><slash:comments>0</slash:comments><description>&lt;h2&gt;Async - Handling multiple Exceptions&lt;/h2&gt;  &lt;p&gt;the &lt;strong&gt;SDP conference&lt;/strong&gt; was ended a few week ago and I finally find a time to write some comments.&lt;/p&gt;  &lt;p&gt;It was a very successful conference, the feedback and evaluations, of most sessions scored higher than 4.5, some well-over it. For example, &lt;b&gt;one workshop had a perfect 5 / 5 score&lt;/b&gt;, and two other workshops scored 4.92 / 5 and 4.9 / 5. The highest score for a breakout session was 9.39 / 10, which is the highest score we’ve seen to date. my score was &lt;strong&gt;4.90 / 5&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;but this post isn&amp;#39;t about scores, it is about fixing a sample that I was showing at the SDP about &lt;strong&gt;exception handling&lt;/strong&gt; within the &lt;strong&gt;async / await context&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/IMG_6260_21812378.jpg"&gt;&lt;img style="background-image:none;border-bottom:0px;border-left:0px;padding-left:0px;padding-right:0px;display:inline;border-top:0px;border-right:0px;padding-top:0px;" title="Parallel,Task, async, await, exception, whenall, parent, child, .NET 4.5" border="0" alt="Parallel,Task, async, await, exception, whenall, parent, child, .NET 4.5" src="http://blogs.microsoft.co.il/blogs/bnaya/IMG_6260_thumb_697F2F94.jpg" width="407" height="305" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;when you do use the great &lt;strong&gt;async / await&lt;/strong&gt; syntax you may run into a problem while trying to &lt;strong&gt;handle multiple exception&lt;/strong&gt; which was thrown from async operations (it can happens in cases like &lt;strong&gt;parent child&lt;/strong&gt; relationship or when using &lt;strong&gt;WhenAll&lt;/strong&gt; API).&lt;/p&gt;  &lt;p&gt;by &lt;a href="http://blogs.msdn.com/b/pfxteam/archive/2011/09/28/10217876.aspx" target="_blank"&gt;design&lt;/a&gt; the &lt;strong&gt;catch&lt;/strong&gt; closure will handle the &lt;strong&gt;first exception&lt;/strong&gt; and ignore all the other, you can read about it in &lt;a href="http://blogs.msdn.com/b/pfxteam/archive/2011/09/28/10217876.aspx" target="_blank"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;the following code will catch a single NullReferenceException or ArgumentException exception (the AggregateException will be ignored):&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:9ec1c3e9-954e-4314-bc4d-dd63d2a182ba" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;try&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;await&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Task&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.Factory.StartNew(() =&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;            &lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;                &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Task&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.Factory.StartNew(() =&amp;gt; &lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;                    &lt;span style="background:#ffffff;color:#000000;"&gt;{ &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;throw&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;NullReferenceException&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(); },&lt;/span&gt;&lt;/li&gt; &lt;li&gt;                        &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;TaskCreationOptions&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.AttachedToParent);&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;                &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Task&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.Factory.StartNew(() =&amp;gt; &lt;/span&gt;&lt;/li&gt; &lt;li&gt;                    &lt;span style="background:#ffffff;color:#000000;"&gt;{ &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;throw&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ArgumentException&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(); },&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;                        &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;TaskCreationOptions&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.AttachedToParent);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;            &lt;span style="background:#ffffff;color:#000000;"&gt;});&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;catch&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; (&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;AggregateException&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; ex)&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#008000;"&gt;// this catch will never be target&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;catch&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; (&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Exception&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; ex)&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Console&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.WriteLine(&lt;/span&gt;&lt;span style="background:#ffffff;color:#a31515;"&gt;&amp;quot;## {0} ##&amp;quot;&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;, ex.GetType().Name);&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;if you do want to &lt;strong&gt;catch&lt;/strong&gt; the &lt;strong&gt;AggregateException&lt;/strong&gt; you can use the following work around :&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:96d7cb18-c4f6-4473-9fda-a0ed9b064f0a" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;try&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Task&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; t = &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Task&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.Factory.StartNew(() =&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;            &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Task&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.Factory.StartNew(() =&amp;gt; &lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;                &lt;span style="background:#ffffff;color:#000000;"&gt;{ &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;throw&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;NullReferenceException&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(); },&lt;/span&gt;&lt;/li&gt; &lt;li&gt;                    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;TaskCreationOptions&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.AttachedToParent);&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;            &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Task&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.Factory.StartNew(() =&amp;gt; &lt;/span&gt;&lt;/li&gt; &lt;li&gt;                &lt;span style="background:#ffffff;color:#000000;"&gt;{ &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;throw&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ArgumentException&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(); },&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;                    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;TaskCreationOptions&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.AttachedToParent);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;});&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;await&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; t.ContinueWith(tsk =&amp;gt; { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;if&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; (tsk.Exception != &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;null&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;) &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;throw&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; tsk.Exception; });&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;catch&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; (&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;AggregateException&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; ex)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;foreach&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; (&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; exc &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;in&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; ex.Flatten().InnerExceptions)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Console&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.WriteLine(exc.GetType().Name);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;the difference is at line 12, I&amp;#39;m using a &lt;strong&gt;continuation that re-throw the AggregateException&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;because this is a fairly common practice you may better wrap it within an &lt;strong&gt;extension method&lt;/strong&gt; like the following one:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:04f52111-060f-44c2-abe2-010a5f60b646" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;static&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Task&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; ThrowMultiple(&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;this&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Task&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; t)&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;return&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; t.ContinueWith(tsk =&amp;gt; { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;if&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; (tsk.Exception != &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;null&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;) &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;throw&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; tsk.Exception; });&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;static&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Task&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;lt;T&amp;gt; ThrowMultiple&amp;lt;T&amp;gt;(&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;this&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Task&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;lt;T&amp;gt; t)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;return&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; t.ContinueWith(tsk =&amp;gt; &lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;if&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; (tsk.Exception != &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;null&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;            &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;throw&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; tsk.Exception;&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;return&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; tsk.Result;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;});&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;you can use it as follow:&lt;/p&gt;  &lt;p&gt;   &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:ef7796b8-6397-45bf-9547-e77705d96328" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;try&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;await&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Task&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.Factory.StartNew(() =&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;            &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Task&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.Factory.StartNew(() =&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;                &lt;span style="background:#ffffff;color:#000000;"&gt;{ &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;throw&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;NullReferenceException&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(); },&lt;/span&gt;&lt;/li&gt; &lt;li&gt;                    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;TaskCreationOptions&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.AttachedToParent);&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;            &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Task&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.Factory.StartNew(() =&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;                &lt;span style="background:#ffffff;color:#000000;"&gt;{ &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;throw&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ArgumentException&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(); },&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;                    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;TaskCreationOptions&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.AttachedToParent);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;}).ThrowMultiple();&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;catch&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; (&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;AggregateException&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; ex)&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;foreach&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; (&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; exc &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;in&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; ex.Flatten().InnerExceptions)&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Console&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.WriteLine(exc.GetType().Name);&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/p&gt;        &lt;h5&gt;Summary&lt;/h5&gt;  &lt;p&gt;catching a single exception within an async method is a &lt;a href="http://blogs.msdn.com/b/pfxteam/archive/2011/09/28/10217876.aspx" target="_blank"&gt;design decision&lt;/a&gt;,    &lt;br /&gt;but you may want to override this decision in some scenarios.&lt;/p&gt;  &lt;p&gt;I hope that in a future release of .NET framework Microsoft consider to &lt;strong&gt;throw an AggregateException&lt;/strong&gt; when the compiler will detect that the developer was adding an &lt;strong&gt;AggregateException catch block&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;in the meantime you can use the extension methods.&lt;/p&gt;  &lt;p&gt;you should not forget to &lt;strong&gt;catch a non AggregateException&lt;/strong&gt; if you have any &lt;strong&gt;code before the await&lt;/strong&gt; (this code will run synchronously).&lt;/p&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=1535142" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/SELA/default.aspx">SELA</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Parallel/default.aspx">Parallel</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Task/default.aspx">Task</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/async/default.aspx">async</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/await/default.aspx">await</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/SDP/default.aspx">SDP</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/TAP/default.aspx">TAP</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/.NET+4.5/default.aspx">.NET 4.5</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/exception/default.aspx">exception</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/parent/default.aspx">parent</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/whenall/default.aspx">whenall</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/child/default.aspx">child</category></item><item><title>SDP 2012 - Day 1</title><link>http://blogs.microsoft.co.il/blogs/bnaya/archive/2012/11/18/sdp-2012-day-1.aspx</link><pubDate>Sun, 18 Nov 2012 21:21:40 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:1455452</guid><dc:creator>bnaya</dc:creator><slash:comments>0</slash:comments><description>&lt;h2&gt;SDP 2012 - Day 1&lt;/h2&gt;  &lt;p&gt;the &lt;a href="http://sela.co.il/s/sdp2012_july/index.html" target="_blank"&gt;SDP 2012 conference&lt;/a&gt; has started today.&lt;/p&gt;  &lt;p&gt;I was speaking about what&amp;#39;s new in &lt;strong&gt;.NET 4.5 parallelism&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;you can find the materials for my lecture in &lt;a href="https://skydrive.live.com/?cid=9bf7c1a515d76a9a&amp;amp;id=9BF7C1A515D76A9A%215689" target="_blank"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/IMG_6811_7822_6E3E272A.jpg"&gt;&lt;img style="background-image:none;border-bottom:0px;border-left:0px;padding-left:0px;padding-right:0px;display:inline;border-top:0px;border-right:0px;padding-top:0px;" title="sdp, parallel, .NET 4.5, Task, Async, await, Sela" border="0" alt="sdp, parallel, .NET 4.5, Task, Async, await, Sela" src="http://blogs.microsoft.co.il/blogs/bnaya/IMG_6811_7822_thumb_1216D8B8.jpg" width="463" height="320" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;tomorrow I&amp;#39;m giving a full day tutorial about &lt;strong&gt;async / await, Rx and TPL Dataflow&lt;/strong&gt;.&lt;/p&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=1455452" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/DEV/default.aspx">DEV</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/SELA/default.aspx">SELA</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Parallel/default.aspx">Parallel</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Task/default.aspx">Task</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/async/default.aspx">async</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/await/default.aspx">await</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/SDP/default.aspx">SDP</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/.NET+4.5/default.aspx">.NET 4.5</category></item><item><title>async / await, some reasoning</title><link>http://blogs.microsoft.co.il/blogs/bnaya/archive/2012/01/19/async-await-some-reasoning.aspx</link><pubDate>Thu, 19 Jan 2012 16:09:23 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:992172</guid><dc:creator>bnaya</dc:creator><slash:comments>0</slash:comments><description>&lt;h2&gt;async / await, some reasoning&lt;/h2&gt;  &lt;p&gt;this post will try to make some reasoning about the &lt;strong&gt;.NET 4.5 / C#5 await&lt;/strong&gt; keyword.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/IMG_6498_small_50439CCB.png"&gt;&lt;img style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" title="async, await, task, tpl, parallel, c#5, .NET 4.5" border="0" alt="async, await, task, tpl, parallel, c#5, .NET 4.5" src="http://blogs.microsoft.co.il/blogs/bnaya/IMG_6498_small_thumb_5CC1241A.png" width="339" height="230" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;I will begin with a quiz.&lt;/p&gt;  &lt;p&gt;how long will it take to the following method to produce the 42 value?&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:67a78d27-306f-4098-8621-b0f2bb0bdc82" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;async&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Task&lt;/span&gt;&amp;lt;&lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;&amp;gt; Execute()&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff;"&gt;await&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Task&lt;/span&gt;.Delay(1000);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#0000ff;"&gt;await&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Task&lt;/span&gt;.Delay(1000);&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; 42;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;you should remember that conceptually the &lt;strong&gt;await&lt;/strong&gt; keyword will translate to a &lt;strong&gt;continuation&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;the above code can be compare to the following &lt;strong&gt;TPL 4&lt;/strong&gt; code snippet:&lt;/p&gt;    &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:ca0e8452-65f9-434b-a932-2f214c8ef0c7" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#2b91af;"&gt;Task&lt;/span&gt;&amp;lt;&lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;&amp;gt; Execute()&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#2b91af;"&gt;Task&lt;/span&gt; t1 = &lt;span style="color:#2b91af;"&gt;Task&lt;/span&gt;.Factory.StartNew(&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        () =&amp;gt; &lt;span style="color:#2b91af;"&gt;Thread&lt;/span&gt;.Sleep(1000));&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#2b91af;"&gt;Task&lt;/span&gt; t2 = t1.ContinueWith(t1_ =&amp;gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        {&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#2b91af;"&gt;Thread&lt;/span&gt;.Sleep(1000);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        });&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#2b91af;"&gt;Task&lt;/span&gt;&amp;lt;&lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;&amp;gt; t3 = t2.ContinueWith(t2_ =&amp;gt; 42);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; t3;&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;    &lt;p&gt;whatever come after the &lt;strong&gt;await&lt;/strong&gt; will be compile into a &lt;strong&gt;continuation closure&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;therefore the 42 value will be produce after &lt;strong&gt;2 second&lt;/strong&gt;.&lt;/p&gt;  &lt;h5&gt;How can we await for multiple task?&lt;/h5&gt;  &lt;p&gt;there is couple of way for &lt;strong&gt;awaiting&lt;/strong&gt; on &lt;strong&gt;multiple tasks&lt;/strong&gt;, but the most recommended one is to use Task.&lt;strong&gt;WhenAll&lt;/strong&gt; (you can also use&lt;strong&gt; &lt;/strong&gt;Task.&lt;strong&gt;WhenAny&lt;/strong&gt; to continue after the completion of the first task).&lt;/p&gt;  &lt;p&gt;&lt;font color="#666666"&gt;&lt;em&gt;do not confuse the Task.&lt;strong&gt;WhenAll&lt;/strong&gt; with Task.&lt;strong&gt;WaitAll, WhenAll&lt;/strong&gt; is a continuation which happens when all the task come to completion, while &lt;strong&gt;WaitAll&lt;/strong&gt; is a blocking API which will block the execution until all tasks will be completed&lt;/em&gt;.&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;the following snippet demonstrate the Task.&lt;strong&gt;WhenAll&lt;/strong&gt; usage.&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:4ba378e0-0468-430b-bd58-49df1a733ed8" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;async&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Task&lt;/span&gt;&amp;lt;&lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;&amp;gt; Execute()&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#2b91af;"&gt;Task&lt;/span&gt; t1 = &lt;span style="color:#2b91af;"&gt;Task&lt;/span&gt;.Delay(1000);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#2b91af;"&gt;Task&lt;/span&gt; t2 = &lt;span style="color:#2b91af;"&gt;Task&lt;/span&gt;.Delay(1000);&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff;"&gt;await&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Task&lt;/span&gt;.WhenAll(t1, t2);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; 42;&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;the 42 value will now produce after &lt;strong&gt;1 second&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;it is somewhat equals to the following &lt;strong&gt;TPL 4&lt;/strong&gt; snippet:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:885dc749-3806-4412-bf71-730d307676ed" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#2b91af;"&gt;Task&lt;/span&gt;&amp;lt;&lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;&amp;gt; Execute()&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#2b91af;"&gt;Task&lt;/span&gt; t1 = &lt;span style="color:#2b91af;"&gt;Task&lt;/span&gt;.Factory.StartNew(&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        () =&amp;gt; &lt;span style="color:#2b91af;"&gt;Thread&lt;/span&gt;.Sleep(1000));&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#2b91af;"&gt;Task&lt;/span&gt; t2 = &lt;span style="color:#2b91af;"&gt;Task&lt;/span&gt;.Factory.StartNew(&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        () =&amp;gt; &lt;span style="color:#2b91af;"&gt;Thread&lt;/span&gt;.Sleep(1000));&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#2b91af;"&gt;Task&lt;/span&gt;&amp;lt;&lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;&amp;gt; t3 = &lt;span style="color:#2b91af;"&gt;Task&lt;/span&gt;.Factory.ContinueWhenAll(&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt;[]{t1, t2}, tsks =&amp;gt; 42);&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; t3;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;to wrap it up let think how long will it take to the following snippet to produce a value.&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:d8078248-512c-4284-9ebe-7ab2bfdfdce7" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;async&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Task&lt;/span&gt;&amp;lt;&lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;&amp;gt; Execute()&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff;"&gt;for&lt;/span&gt; (&lt;span style="color:#0000ff;"&gt;int&lt;/span&gt; i = 0; i &amp;lt; 10; i++)&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    {&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff;"&gt;await&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Task&lt;/span&gt;.Delay(1000);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    }&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; 42;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;the right answer is 10 seconds, each iteration will result in a continuation closure which will wrap the following iterations.&lt;/p&gt;  &lt;p&gt;this can be translate to something like the following snippet (&lt;strong&gt;TPL 4&lt;/strong&gt;).&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:86275192-eaa8-4e3d-8386-31ba63cc824e" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#2b91af;"&gt;Task&lt;/span&gt;&amp;lt;&lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;&amp;gt; Execute()&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff;"&gt;var&lt;/span&gt; stateMachine = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;StateMachine&lt;/span&gt;();&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; stateMachine.OnNext();&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;StateMachine&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;int&lt;/span&gt; _i;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;TaskCompletionSource&lt;/span&gt;&amp;lt;&lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;&amp;gt; _semanticTask =&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;TaskCompletionSource&lt;/span&gt;&amp;lt;&lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;&amp;gt;();&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Task&lt;/span&gt;&amp;lt;&lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;&amp;gt; OnNext()&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    {&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#2b91af;"&gt;Interlocked&lt;/span&gt;.Increment(&lt;span style="color:#0000ff;"&gt;ref&lt;/span&gt; _i);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#2b91af;"&gt;Task&lt;/span&gt; t = &lt;span style="color:#2b91af;"&gt;Task&lt;/span&gt;.Factory.StartNew(() =&amp;gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;            &lt;span style="color:#2b91af;"&gt;Thread&lt;/span&gt;.Sleep(1000));&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (_i &amp;lt;= 10)&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;            t.ContinueWith(t_ =&amp;gt; OnNext());&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff;"&gt;else&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;            _semanticTask.SetResult(42);&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; _semanticTask.Task;&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;the execute method will create a state machine which will &lt;strong&gt;chain task continuation 10 times&lt;/strong&gt; and then set the value of 42.     &lt;br /&gt;the &lt;strong&gt;TaskCompletionSource&lt;/strong&gt; represent semantics of task (TAP - Task Async Pattern).&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;font color="#666666"&gt;TaskCompletionSource&lt;/font&gt;&lt;/strong&gt; &lt;font color="#666666"&gt;does not produce any concurrency (doesn&amp;#39;t attached to any thread), it just present a task which can be project result, exception or cancellation.&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;the OnNext method immediately return a semantic task which will signal as complete at line 22 (the exit term of the recursion).&lt;/p&gt;  &lt;h5&gt;Summary&lt;/h5&gt;  &lt;p&gt;&lt;strong&gt;await&lt;/strong&gt; present a &lt;strong&gt;continuation&lt;/strong&gt;. each time the code is hitting the await it construct a new continuation closure.&lt;/p&gt;  &lt;br /&gt;&lt;a href="http://dotnetshoutout.com/async-await-some-reasoning-Bnaya-Eshet"&gt;&lt;img style="border-right-width:0px;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" alt="Shout it" src="http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fblogs.microsoft.co.il%2Fblogs%2Fbnaya%2Farchive%2F2012%2F01%2F19%2Fasync-await-some-reasoning.aspx" /&gt;&lt;/a&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=992172" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/DEV/default.aspx">DEV</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/SELA/default.aspx">SELA</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Parallel/default.aspx">Parallel</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Task/default.aspx">Task</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/TPL/default.aspx">TPL</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/async/default.aspx">async</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/await/default.aspx">await</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/TAP/default.aspx">TAP</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/.NET+4.5/default.aspx">.NET 4.5</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/C_2300_5/default.aspx">C#5</category></item><item><title>Using async / await</title><link>http://blogs.microsoft.co.il/blogs/bnaya/archive/2012/01/15/using-async-await.aspx</link><pubDate>Sun, 15 Jan 2012 05:46:58 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:987991</guid><dc:creator>bnaya</dc:creator><slash:comments>0</slash:comments><description>&lt;h2&gt;Using async / await&lt;/h2&gt;  &lt;p&gt;this post will discuss parallel disposal.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/IMG_9746_2168_small_1F348538.png"&gt;&lt;img style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" title="async, await, parallel, task,tpl, using" border="0" alt="async, await, parallel, task,tpl, using" src="http://blogs.microsoft.co.il/blogs/bnaya/IMG_9746_2168_small_thumb_5D0DCA5C.png" width="334" height="244" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;whenever we want to &lt;strong&gt;dispose&lt;/strong&gt; a &lt;strong&gt;parallel execution&lt;/strong&gt; upon completion we can&amp;#39;t use the convenient &lt;strong&gt;using&lt;/strong&gt; keyword.&lt;/p&gt;  &lt;p&gt;for example, the following code may be dispose the command before completion:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:9e272259-3eb9-47d2-b059-2b06b2d0d4cd" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Very bad Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (&lt;span style="color:#0000ff;"&gt;var&lt;/span&gt; conn = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;SqlConnection&lt;/span&gt;(CONN_STR))&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (&lt;span style="color:#0000ff;"&gt;var&lt;/span&gt; cmd = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;SqlCommand&lt;/span&gt;(&lt;span style="color:#a31515;"&gt;&amp;quot;Select * from Employee&amp;quot;&lt;/span&gt;, conn)) &lt;/li&gt; &lt;li&gt;{&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    conn.Open();&lt;/li&gt; &lt;li&gt;    cmd.BeginExecuteReader(ar =&amp;gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        {&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#0000ff;"&gt;int&lt;/span&gt; affected = cmd.EndExecuteNonQuery(ar);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        });&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;the &lt;strong&gt;using&lt;/strong&gt; is absolutely wrong for the above sample.&lt;/p&gt;  &lt;p&gt;what should be done is:&lt;/p&gt;    &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:549dca5a-4369-48a4-99b2-c5dab5b1eb96" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;var&lt;/span&gt; conn = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;SqlConnection&lt;/span&gt;(CONN_STR);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;var&lt;/span&gt; cmd = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;SqlCommand&lt;/span&gt;(&lt;span style="color:#a31515;"&gt;&amp;quot;Select * from Employee&amp;quot;&lt;/span&gt;, conn);&lt;/li&gt; &lt;li&gt;conn.Open();&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;cmd.BeginExecuteReader(ar =&amp;gt;&lt;/li&gt; &lt;li&gt;{&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#0000ff;"&gt;int&lt;/span&gt; affected = cmd.EndExecuteNonQuery(ar);&lt;/li&gt; &lt;li&gt;    cmd.Dispose();&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    conn.Dispose();&lt;/li&gt; &lt;li&gt;}, &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;);&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;    &lt;p&gt;we can write it slightly friendlier by using the TPL FromAsync wrapper:&lt;/p&gt;    &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:17352ac0-7dff-4f25-85a9-5ae6029242ac" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;var&lt;/span&gt; conn = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;SqlConnection&lt;/span&gt;(CONN_STR);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;var&lt;/span&gt; cmd = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;SqlCommand&lt;/span&gt;(&lt;span style="color:#a31515;"&gt;&amp;quot;Select * from Employee&amp;quot;&lt;/span&gt;, conn);&lt;/li&gt; &lt;li&gt;conn.Open();&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#2b91af;"&gt;Task&lt;/span&gt;&amp;lt;&lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;&amp;gt; t = &lt;span style="color:#2b91af;"&gt;Task&lt;/span&gt;.Factory.FromAsync&amp;lt;&lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;&amp;gt;(&lt;/li&gt; &lt;li&gt;    cmd.BeginExecuteReader,&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    cmd.EndExecuteNonQuery,&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;t.ContinueWith(tsk =&amp;gt;&lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="color:#0000ff;"&gt;int&lt;/span&gt; affected = tsk.Result;&lt;/li&gt; &lt;li&gt;        cmd.Dispose();&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        conn.Dispose();&lt;/li&gt; &lt;li&gt;    });&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;    &lt;h5&gt;but what if the compiler can rewrite our code?&lt;/h5&gt;  &lt;p&gt;in that case we can write a similar code to the code in the first code snippet and get it compiled into something like the above code snippet above.&lt;/p&gt;  &lt;p&gt;this is exactly what happens when in&lt;strong&gt; .NET 4.5 / C#5&lt;/strong&gt; (&lt;strong&gt;async / await pattern&lt;/strong&gt;).&lt;/p&gt;  &lt;p&gt;when we write the following code:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:e7dedbdc-7d82-49dc-8fa5-d71f6539012f" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;async&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Task&lt;/span&gt;&amp;lt;&lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;&amp;gt; ExecuteNonQueryAsync()&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff;"&gt;int&lt;/span&gt; affected = 0;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (&lt;span style="color:#0000ff;"&gt;var&lt;/span&gt; conn = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;SqlConnection&lt;/span&gt;(CONN_STR))&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (&lt;span style="color:#0000ff;"&gt;var&lt;/span&gt; cmd = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;SqlCommand&lt;/span&gt;(&lt;span style="color:#a31515;"&gt;&amp;quot;Select * from Employee&amp;quot;&lt;/span&gt;, conn))&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    {&lt;/li&gt; &lt;li&gt;        conn.Open();&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        affected = &lt;span style="color:#0000ff;"&gt;await&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Task&lt;/span&gt;.Factory.FromAsync&amp;lt;&lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;&amp;gt;(&lt;/li&gt; &lt;li&gt;            cmd.BeginExecuteReader,&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;            cmd.EndExecuteNonQuery,&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    }&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; affected;  &lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;the compiler does rewrite this async method.&lt;/p&gt;  &lt;p&gt;everything that follow the await keyword (line 8), &lt;/p&gt;  &lt;p&gt;will be put into a continuation state machine, including the closing of the curly brackets (of the using).&lt;/p&gt;  &lt;h5&gt;Summary&lt;/h5&gt;  &lt;p&gt;using the new async / await pattern will dispose our resources on time.&lt;/p&gt;  &lt;p&gt;using keyword is a very clean syntax and now we can apply it to parallel execution.&lt;/p&gt;  &lt;br /&gt;&lt;a href="http://dotnetshoutout.com/Using-async-await-Bnaya-Eshet"&gt;&lt;img style="border-right-width:0px;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" alt="Shout it" src="http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fblogs.microsoft.co.il%2Fblogs%2Fbnaya%2Farchive%2F2012%2F01%2F15%2Fusing-async-await.aspx" /&gt;&lt;/a&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=987991" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/DEV/default.aspx">DEV</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/SELA/default.aspx">SELA</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Parallel/default.aspx">Parallel</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Task/default.aspx">Task</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/TPL/default.aspx">TPL</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/async/default.aspx">async</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/await/default.aspx">await</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/continuewith/default.aspx">continuewith</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/continuation/default.aspx">continuation</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/.NET+4.5/default.aspx">.NET 4.5</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/C_2300_5/default.aspx">C#5</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/using/default.aspx">using</category></item><item><title>async \ await and Exception Handling</title><link>http://blogs.microsoft.co.il/blogs/bnaya/archive/2012/01/12/async-await-and-exception-handling.aspx</link><pubDate>Thu, 12 Jan 2012 13:11:07 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:985364</guid><dc:creator>bnaya</dc:creator><slash:comments>0</slash:comments><description>&lt;h2&gt;async \ await and Exception Handling&lt;/h2&gt;  &lt;p&gt;this post will discuss how &lt;strong&gt;async / await&lt;/strong&gt; is &lt;strong&gt;handling exceptions&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/IMG_1829_small_72A36ABC.png"&gt;&lt;img style="background-image:none;border-bottom:0px;border-left:0px;padding-left:0px;padding-right:0px;display:inline;border-top:0px;border-right:0px;padding-top:0px;" title="async, await, continuation, continue,exception, tpl,.net 4.5, c#5" border="0" alt="async, await, continuation, continue,exception, tpl,.net 4.5, c#5" src="http://blogs.microsoft.co.il/blogs/bnaya/IMG_1829_small_thumb_60A68B55.png" width="242" height="269" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;as we mention in previous &lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2011/12/29/the-concept-of-async-await.aspx" target="_blank"&gt;post&lt;/a&gt;, about the &lt;strong&gt;async / await&lt;/strong&gt; concept, &lt;strong&gt;await is all about continuation&lt;/strong&gt;. &lt;/p&gt;  &lt;p&gt;before .NET 4.5 parallel execution exceptions has to be handle in separate of the synchronic handling.&lt;/p&gt;  &lt;p&gt;for example:&lt;/p&gt;  &lt;p&gt;handling ThreadPool execution:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:cadffb80-4f21-42ec-b433-ab46acaa8e21" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Foo()&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff;"&gt;try&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    {&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#2b91af;"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color:#a31515;"&gt;&amp;quot;Synchronic&amp;quot;&lt;/span&gt;);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="color:#2b91af;"&gt;ThreadPool&lt;/span&gt;.QueueUserWorkItem(state =&amp;gt;&lt;/li&gt; &lt;li&gt;            {&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;                &lt;span style="color:#0000ff;"&gt;try&lt;/span&gt;&lt;/li&gt; &lt;li&gt;                {&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;                    &lt;span style="color:#2b91af;"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color:#a31515;"&gt;&amp;quot;Parallel&amp;quot;&lt;/span&gt;);&lt;/li&gt; &lt;li&gt;                }&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;                &lt;span style="color:#0000ff;"&gt;catch&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;Exception&lt;/span&gt; exAsync)&lt;/li&gt; &lt;li&gt;                {&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;                    &lt;span style="color:#2b91af;"&gt;EventLog&lt;/span&gt;.WriteEntry(&lt;span style="color:#a31515;"&gt;&amp;quot;application&amp;quot;&lt;/span&gt;, exAsync.ToString());&lt;/li&gt; &lt;li&gt;                }&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;            }, &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;);&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#0000ff;"&gt;catch&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;Exception&lt;/span&gt; ex)&lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="color:#2b91af;"&gt;EventLog&lt;/span&gt;.WriteEntry(&lt;span style="color:#a31515;"&gt;&amp;quot;application&amp;quot;&lt;/span&gt;, ex.ToString());&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;as you can see we have to handle the parallel exception (line 12) in separate from the synchronic handling (line 18).&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;TPL&lt;/strong&gt; has brought new option for handling parallel exception, now you can use &lt;strong&gt;ContinueWith&lt;/strong&gt; with &lt;strong&gt;TaskContinuationOptions.OnlyOnFault &lt;/strong&gt;(line 8).     &lt;br /&gt;but still you have to handle the parallel exception is in separate of the synchronic one:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:a5cc18e1-f3b5-407f-b94f-4c5600395331" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Foo()&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff;"&gt;try&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    {&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#2b91af;"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color:#a31515;"&gt;&amp;quot;Synchronic&amp;quot;&lt;/span&gt;);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="color:#2b91af;"&gt;Task&lt;/span&gt; t = &lt;span style="color:#2b91af;"&gt;Task&lt;/span&gt;.Factory.StartNew(() =&amp;gt; &lt;span style="color:#2b91af;"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color:#a31515;"&gt;&amp;quot;Parallel&amp;quot;&lt;/span&gt;));&lt;/li&gt; &lt;li&gt;        t.ContinueWith(tsk =&amp;gt; &lt;span style="color:#2b91af;"&gt;EventLog&lt;/span&gt;.WriteEntry(&lt;span style="color:#a31515;"&gt;&amp;quot;application&amp;quot;&lt;/span&gt;, tsk.Exception.ToString()), &lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;            &lt;span style="color:#2b91af;"&gt;TaskContinuationOptions&lt;/span&gt;.OnlyOnFaulted);&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#0000ff;"&gt;catch&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;Exception&lt;/span&gt; ex)&lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="color:#2b91af;"&gt;EventLog&lt;/span&gt;.WriteEntry(&lt;span style="color:#a31515;"&gt;&amp;quot;application&amp;quot;&lt;/span&gt;, ex.ToString());&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;h5&gt;async / await pattern&lt;/h5&gt;  &lt;p&gt;using the async / await pattern we can handle both synchronic and parallel exception in the same place:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:c95c67bf-6fd4-495d-88a8-0a641cd8a920" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;async&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Foo()&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff;"&gt;try&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    {&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#2b91af;"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color:#a31515;"&gt;&amp;quot;Synchronic&amp;quot;&lt;/span&gt;);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="color:#0000ff;"&gt;await&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Task&lt;/span&gt;.Factory.StartNew(() =&amp;gt; &lt;span style="color:#2b91af;"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color:#a31515;"&gt;&amp;quot;Parallel&amp;quot;&lt;/span&gt;));&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#0000ff;"&gt;catch&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;Exception&lt;/span&gt; ex)&lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="color:#008000;"&gt;// handling both synchronic and parallel exceptions&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#2b91af;"&gt;EventLog&lt;/span&gt;.WriteEntry(&lt;span style="color:#a31515;"&gt;&amp;quot;application&amp;quot;&lt;/span&gt;, ex.ToString());&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    }&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;when we are using the &lt;strong&gt;async / await&lt;/strong&gt; pattern at &lt;strong&gt;compile time&lt;/strong&gt; the compiler convert our code into continuation state machine.     &lt;br /&gt;therefore the compiler can take the code within the catch area and apply it both for the synchronic and the parallel execution.&lt;/p&gt;  &lt;h5&gt;Summary&lt;/h5&gt;  &lt;p&gt;async / await pattern does simplify the exception handling. we do write our exception handling once and it will apply for both synchronic and parallel execution.&lt;/p&gt; &lt;br /&gt;&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fblogs.microsoft.co.il%2fblogs%2fbnaya%2farchive%2f2012%2f01%2f12%2fasync-await-and-exception-handling.aspx"&gt;&lt;img border="0" alt="kick it on DotNetKicks.com" /&gt;&lt;/a&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=985364" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/DEV/default.aspx">DEV</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/SELA/default.aspx">SELA</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Parallel/default.aspx">Parallel</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Task/default.aspx">Task</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Thread/default.aspx">Thread</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/TPL/default.aspx">TPL</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/async/default.aspx">async</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/await/default.aspx">await</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/continuation/default.aspx">continuation</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/.NET+4.5/default.aspx">.NET 4.5</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/C_2300_5/default.aspx">C#5</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/exception/default.aspx">exception</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/continue/default.aspx">continue</category></item><item><title>the concept of async \ await</title><link>http://blogs.microsoft.co.il/blogs/bnaya/archive/2011/12/29/the-concept-of-async-await.aspx</link><pubDate>Thu, 29 Dec 2011 22:41:42 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:972828</guid><dc:creator>bnaya</dc:creator><slash:comments>0</slash:comments><description>&lt;h2&gt;the concept of async \ await&lt;/h2&gt;  &lt;p&gt;in this post I will survey the new .NET 4.5 / C# 5 concept of async / await.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/IMG_2101-small_58EC151B.png"&gt;&lt;img style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" title="async, await, .NET 4.5, C#5, continuation" border="0" alt="async, await, .NET 4.5, C#5, continuation" src="http://blogs.microsoft.co.il/blogs/bnaya/IMG_2101-small_thumb_06F59F6F.png" width="371" height="316" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;I will focus on how to understand what is really happens behind the new &lt;strong&gt;async / await&lt;/strong&gt; syntax.&lt;/p&gt;  &lt;h5&gt;What&amp;#39;s it all about?&lt;/h5&gt;  &lt;p&gt;the new &lt;strong&gt;async / await &lt;/strong&gt;syntax is using the C# syntactic compiler to generate &lt;strong&gt;async operation&lt;/strong&gt; from code that is looking very much &lt;strong&gt;like a synchronous&lt;/strong&gt; code.&lt;/p&gt;  &lt;p&gt;but before we start we should discus the new C# 5&amp;#160; syntax.&lt;/p&gt;  &lt;p&gt;the syntax include 2 keywords:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;strong&gt;async&lt;/strong&gt; - which is only a marker for async method. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;await&lt;/strong&gt; - indicate a callback boundary. &lt;/li&gt; &lt;/ul&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:5895840e-138b-401c-9704-2a3fa47b4ace" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;async&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Task&lt;/span&gt; Execute()&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#2b91af;"&gt;Console&lt;/span&gt;.WriteLine(run on calling thread);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff;"&gt;await&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Task&lt;/span&gt;.Factory.StartNew(() =&amp;gt; &lt;span style="color:#2b91af;"&gt;Thread&lt;/span&gt;.Sleep(1000));&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;      &lt;span style="color:#2b91af;"&gt;Console&lt;/span&gt;.WriteLine(run on callback thread); &lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;so how should we understand what was written in the above code?&lt;/p&gt;  &lt;p&gt;actually it is a different way to represent a &lt;strong&gt;continuation&lt;/strong&gt; (you can read more about the continuation concept in &lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2011/12/28/tpl-continuation.aspx" target="_blank"&gt;here&lt;/a&gt;).&lt;/p&gt;  &lt;p&gt;the above code is somewhat identical to the following TPL 4 code:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:cb5a075e-4cf3-4cc3-8696-c220bb5b29df" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Task&lt;/span&gt; Execute()&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#2b91af;"&gt;Console&lt;/span&gt;.WriteLine(run on calling thread);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#2b91af;"&gt;Task&lt;/span&gt; t = &lt;span style="color:#2b91af;"&gt;Task&lt;/span&gt;.Factory.StartNew(() =&amp;gt; &lt;span style="color:#2b91af;"&gt;Thread&lt;/span&gt;.Sleep(1000));&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; t.ContinueWith (tsk =&amp;gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        {&lt;/li&gt; &lt;li&gt;              &lt;span style="color:#2b91af;"&gt;Console&lt;/span&gt;.WriteLine(run on callback thread);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        });&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;the &lt;strong&gt;syntactic compiler&lt;/strong&gt; will translate the code below the await keyword into &lt;strong&gt;continuation&lt;/strong&gt; &lt;strong&gt;state machine&lt;/strong&gt;, which is logically (not technically) identical to the above code.&lt;/p&gt;  &lt;h5&gt;Point of interest:&lt;/h5&gt;  &lt;p&gt;you may have been notice that the async method return a &lt;strong&gt;Task&lt;/strong&gt; even though there is no return within the method block.     &lt;br /&gt;surveying the TPL 4 code snippet we can understand that the async method will actually return to the caller immediately after the &lt;strong&gt;Task.Factory.StartNew&lt;/strong&gt; start the task and the rest of the code is actually a &lt;strong&gt;continuation callback&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;what we got back from the async method is a &lt;strong&gt;task&lt;/strong&gt; which represent the &lt;strong&gt;async part of the method&lt;/strong&gt;.&lt;/p&gt;  &lt;h5&gt;async / await with return value&lt;/h5&gt;  &lt;p&gt;&lt;strong&gt;async / await&lt;/strong&gt; can represent a continuation of a callback that accept async result.&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:c8d7ba84-d088-4fe6-b3b3-231a4c4aab83" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;async&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Task&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;DateTime&lt;/span&gt;&amp;gt; Execute()&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#2b91af;"&gt;DateTime&lt;/span&gt; result = &lt;span style="color:#0000ff;"&gt;await&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Task&lt;/span&gt;.Factory.StartNew(() =&amp;gt; &lt;span style="color:#2b91af;"&gt;DateTime&lt;/span&gt;.Now );&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;      &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; result.AddDays(1);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;the above code will logically translate to:&lt;/p&gt;    &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:4503bc00-ebfb-4f7c-ac52-12c5cafedae1" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Task&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;DateTime&lt;/span&gt;&amp;gt; Execute()&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#2b91af;"&gt;Task&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;DateTime&lt;/span&gt;&amp;gt; t = &lt;span style="color:#2b91af;"&gt;Task&lt;/span&gt;.Factory.StartNew(() =&amp;gt; &lt;span style="color:#2b91af;"&gt;DateTime&lt;/span&gt;.Now);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; t.ContinueWith (tsk =&amp;gt;&lt;/li&gt; &lt;li&gt;        {&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;              &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; tsk.Result.AddDays(1);&lt;/li&gt; &lt;li&gt;        });&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;    &lt;p&gt;you may have notice that the &lt;strong&gt;return value&lt;/strong&gt; (&lt;font color="#666666"&gt;on the left side of the await&lt;/font&gt;) was &lt;strong&gt;unwrapped&lt;/strong&gt; (&lt;font color="#666666"&gt;DateTime instead of Task&amp;lt;DateTime&amp;gt;&lt;/font&gt;) &lt;/p&gt;  &lt;h5&gt;Which thread is running?&lt;/h5&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/async1_0DF4904D.png"&gt;&lt;img style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" title="async, await, .NET 4.5, C#5, continuation" border="0" alt="async, await, .NET 4.5, C#5, continuation" src="http://blogs.microsoft.co.il/blogs/bnaya/async1_thumb_57C2F230.png" width="583" height="252" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;normally when the method doesn&amp;#39;t invoke from the UI thread, everything &lt;strong&gt;before the await&lt;/strong&gt; line will run &lt;strong&gt;synchronously&lt;/strong&gt; on the caller thread.     &lt;br /&gt;the &lt;strong&gt;Task.Run&lt;/strong&gt; naturally will be &lt;strong&gt;schedule&lt;/strong&gt; on a &lt;strong&gt;different thread &lt;/strong&gt;and everything &lt;strong&gt;under the await&lt;/strong&gt; will be &lt;strong&gt;schedule&lt;/strong&gt; on &lt;strong&gt;different thread&lt;/strong&gt; then the caller thread, it may be the same thread of the Task.Run or any other ThreadPool thread (&lt;font color="#666666"&gt;when there is only single continuation it will probably be the same thread as Task.Run&lt;/font&gt;) &lt;/p&gt;  &lt;h5&gt;Async and UI&lt;/h5&gt;  &lt;p&gt;whenever the async method invocation is coming from &lt;strong&gt;UI thread&lt;/strong&gt; (or to be more precise from thread under synchronization context) the continuation return back to the &lt;strong&gt;synchronization context thread&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;this is quit similar to the following TPL code (.NET 4):&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:42302151-dba0-486a-9c98-ef565065f3d1" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#2b91af;"&gt;TaskScheduler&lt;/span&gt; scheduler = &lt;span style="color:#2b91af;"&gt;TaskScheduler&lt;/span&gt;.FromCurrentSynchronizationContext();&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#2b91af;"&gt;Task&lt;/span&gt; t = &lt;span style="color:#2b91af;"&gt;Task&lt;/span&gt;.Factory.StartNew(() =&amp;gt; &lt;span style="color:#2b91af;"&gt;Trace&lt;/span&gt;.WriteLine(&lt;span style="color:#a31515;"&gt;&amp;quot;in parallel&amp;quot;&lt;/span&gt;));&lt;/li&gt; &lt;li&gt;t.ContinueWith(tsk =&amp;gt; &lt;span style="color:#2b91af;"&gt;Trace&lt;/span&gt;.WriteLine(&lt;span style="color:#a31515;"&gt;&amp;quot;UI thread&amp;quot;&lt;/span&gt;), scheduler);&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;or to more legacy code which is using the synchronization context directly:&lt;/p&gt;    &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:be8d81d0-b478-4c96-a1c4-f1748185dbdb" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#2b91af;"&gt;Action&lt;/span&gt; a = () =&amp;gt; &lt;span style="color:#2b91af;"&gt;Trace&lt;/span&gt;.WriteLine(&lt;span style="color:#a31515;"&gt;&amp;quot;in parallel&amp;quot;&lt;/span&gt;);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#2b91af;"&gt;SynchronizationContext&lt;/span&gt; sc = &lt;span style="color:#2b91af;"&gt;SynchronizationContext&lt;/span&gt;.Current;&lt;/li&gt; &lt;li&gt;a.BeginInvoke(ar =&amp;gt; &lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    {&lt;/li&gt; &lt;li&gt;        sc.Post(state =&amp;gt; &lt;span style="color:#2b91af;"&gt;Trace&lt;/span&gt;.WriteLine(&lt;span style="color:#a31515;"&gt;&amp;quot;UI thread&amp;quot;&lt;/span&gt;), &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    }, &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;);&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;    &lt;p&gt;&lt;strong&gt;async / await&lt;/strong&gt; is aware of the &lt;strong&gt;synchronization context&lt;/strong&gt; of the caller and if any it &lt;strong&gt;schedule&lt;/strong&gt; the &lt;strong&gt;await&lt;/strong&gt; &lt;strong&gt;callback&lt;/strong&gt; on this context.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/async2_3F03A520.png"&gt;&lt;img style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" title="async, await, .NET 4.5, C#5, continuation" border="0" alt="async, await, .NET 4.5, C#5, continuation" src="http://blogs.microsoft.co.il/blogs/bnaya/async2_thumb_41E8E406.png" width="575" height="212" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;h5&gt;Summary&lt;/h5&gt;  &lt;p&gt;the &lt;strong&gt;syntactic compiler translate&lt;/strong&gt; the&lt;strong&gt; async / await&lt;/strong&gt; syntax into state machine which handle the continuation flow after parallel operation.&lt;/p&gt;  &lt;p&gt;there is much more for that and I will discuss it in future posts.&lt;/p&gt;  &lt;p&gt;you can see it performance characteristic on &lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2011/12/15/what-is-the-cost-of-async-await.aspx" target="_blank"&gt;this&lt;/a&gt; post.&lt;/p&gt;  &lt;br /&gt;&lt;a href="http://dotnetshoutout.com/the-concept-of-async-await-Bnaya-Eshet"&gt;&lt;img style="border-right-width:0px;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" alt="Shout it" src="http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fblogs.microsoft.co.il%2Fblogs%2Fbnaya%2Farchive%2F2011%2F12%2F29%2Fthe-concept-of-async-await.aspx" /&gt;&lt;/a&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=972828" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/DEV/default.aspx">DEV</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Parallel/default.aspx">Parallel</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Performance/default.aspx">Performance</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Task/default.aspx">Task</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Thread/default.aspx">Thread</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/TPL/default.aspx">TPL</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/async/default.aspx">async</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/await/default.aspx">await</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/continuewith/default.aspx">continuewith</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/TAP/default.aspx">TAP</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/continuation/default.aspx">continuation</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/.NET+4.5/default.aspx">.NET 4.5</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/C_2300_5/default.aspx">C#5</category></item></channel></rss>