<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/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>Lucian's VBlog : async</title><link>http://blogs.msdn.com/b/lucian/archive/tags/async/</link><description>Tags: async</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>MVVM with Async - sample code</title><link>http://blogs.msdn.com/b/lucian/archive/2013/02/26/mvvm-with-async-sample-code.aspx</link><pubDate>Tue, 26 Feb 2013 20:53:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10397333</guid><dc:creator>Lucian Wischik, MSFT</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/lucian/rsscomments.aspx?WeblogPostID=10397333</wfw:commentRss><comments>http://blogs.msdn.com/b/lucian/archive/2013/02/26/mvvm-with-async-sample-code.aspx#comments</comments><description>&lt;p&gt;How do you combine MVVM with async? -- Well, really, there's not much to it. Just the same as always. Here's an example Silverlight MVVM project. It first shipped with the Async CTP, and I've updated it to use VS2012 and Microsoft.Bcl.Async.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Download &lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06/6523.AsyncMVVM_2D00_Silverlight_2D00_VB.zip"&gt;AsyncMVVM-Silverlight-VB.zip&lt;/a&gt; [2mb, requires VS2012]&lt;/li&gt;
&lt;li&gt;Download &lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06/4034.AsyncMVVM_2D00_Silverlight_2D00_CS.zip"&gt;AsyncMVVM-Silverlight-CS.zip&lt;/a&gt; [2mb, requires VS2012]&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;One thing that makes it easier with async/await is that these keywords don't introduce new threads. This makes things a lot easier. It means that all your work can be done on the &amp;nbsp;UI thread: even when you update databound properties, or add elements to a databound observable collection, it's all on the UI thread and so works fine.&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;I just want to call out ICommand.Execute. We all know that async voids should only be used for top-level event-handlers, or event-like things. Well, ICommand.Execute is an top-level event-like thing, so it's okay for it to be async void. Just remember that all exceptions that come out of an async void will be posted straight to the UI thread.&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Public&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Class&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;FetchCommand&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Implements&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;ICommand&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Friend&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; vm &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;MainPageViewModel&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Friend&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; Invalidate()&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;RaiseEvent&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; CanExecuteChanged(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Me&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;, &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;EventArgs&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;.Empty)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Public&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; CanExecute(p &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Object&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;) &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Boolean&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Implements&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;ICommand&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;.CanExecute&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Return&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Not&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; vm.Fetching&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Public&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Event&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; CanExecuteChanged(s &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Object&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;, e &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;EventArgs&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;) &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Implements&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;ICommand&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;.CanExecuteChanged&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Public&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; Execute(parameter &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Object&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;) &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Implements&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;ICommand&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;.Execute&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; vm.DoFetch() &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: green; background: white;"&gt;' DoFetch is an Async Sub (i.e. void-returning async)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Class&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10397333" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/lucian/archive/tags/async/">async</category><category domain="http://blogs.msdn.com/b/lucian/archive/tags/mvvm/">mvvm</category></item><item><title>Talk: The New Async Design Patterns</title><link>http://blogs.msdn.com/b/lucian/archive/2013/02/18/talk-the-new-async-design-patterns.aspx</link><pubDate>Mon, 18 Feb 2013 06:02:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10394745</guid><dc:creator>Lucian Wischik, MSFT</dc:creator><slash:comments>5</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/lucian/rsscomments.aspx?WeblogPostID=10394745</wfw:commentRss><comments>http://blogs.msdn.com/b/lucian/archive/2013/02/18/talk-the-new-async-design-patterns.aspx#comments</comments><description>&lt;h1&gt;Talk: The New Async Design Patterns&lt;/h1&gt;
&lt;p&gt;Async involves some new concepts. They're not difficult; just unfamiliar. Over the past year I've been watching how people use it. This talk distils out the three top async patterns and anti-patterns.&lt;/p&gt;
&lt;div&gt;&lt;ol&gt;
&lt;li&gt;Async void is for top-level event-handlers only, and event-like things. Don't use it elsewhere in your code.&lt;/li&gt;
&lt;li&gt;You can wrap events up in Task-returning APIs and await them. This can dramatically simplify code.&lt;/li&gt;
&lt;li&gt;It's crucial to distinguish CPU-bound work (should be done on threadpool) from IO-bound work (which needn't).&lt;/li&gt;
&lt;/ol&gt;
&lt;div&gt;If you understand the basic flow of control in an async method, then those three points all fall naturally into place.&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;I gave this talk at Techready16 in Seattle, USA on 2013.02.05&lt;/strong&gt;&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;Watch videos online: &lt;a href="http://channel9.msdn.com/Series/Three-Essential-Tips-for-Async/Three-Essential-Tips-For-Async-Introduction"&gt;Intro&lt;/a&gt;&amp;nbsp;[5mins], Tip1: &lt;a href="http://channel9.msdn.com/Series/Three-Essential-Tips-for-Async/Tip-1-Async-void-is-for-top-level-event-handlers-only"&gt;async void&lt;/a&gt; [20mins], Tip2: &lt;a href="http://channel9.msdn.com/Series/Three-Essential-Tips-for-Async/Tip-2-Distinguish-CPU-Bound-work-from-IO-bound-work"&gt;async and threads&lt;/a&gt; [16mins], Tip3: &lt;a href="http://channel9.msdn.com/Series/Three-Essential-Tips-for-Async/Lucian03-TipsForAsyncThreadsAndDatabinding"&gt;async-over-events&lt;/a&gt; [15mins]&lt;/li&gt;
&lt;li&gt;Download &lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkAsync2013/4762.Async-Design-Patterns-_5B00_slides_5D00_-_2D00_-Lucian-Wischik-_2D00_-TechReady16-Seattle-2013.02.05.pptx"&gt;slides.pptx&lt;/a&gt; [6.3mb]&lt;/li&gt;
&lt;li&gt;Download &lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkAsync2013/2656.Async-Design-Patterns-_5B00_script_5D00_-_2D00_-Lucian-Wischik-_2D00_-TechReady16-Seattle-2013.02.05.docx"&gt;script.docx&lt;/a&gt; [35k]&lt;/li&gt;
&lt;li&gt;Download VB &lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkAsync2013/6283.Async-Design-Patterns-_5B00_code_5D00_-_2D00_-Lucian-Wischik-_2D00_-TechReady16-Seattle-2013.02.05.zip"&gt;source_code.zip&lt;/a&gt; [460k, requires VS2012 and Win8]&lt;/li&gt;
&lt;/ul&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkAsync2013/3872.asyncdesign1.jpg"&gt;&lt;img src="http://blogs.msdn.com/resized-image.ashx/__size/400x225/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkAsync2013/3872.asyncdesign1.jpg" alt="" border="0" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkAsync2013/7723.asyncdesign2.jpg"&gt;&lt;img src="http://blogs.msdn.com/resized-image.ashx/__size/400x225/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkAsync2013/7723.asyncdesign2.jpg" alt="" border="0" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkAsync2013/2480.asyncdesign3.jpg"&gt;&lt;img src="http://blogs.msdn.com/resized-image.ashx/__size/400x225/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkAsync2013/2480.asyncdesign3.jpg" alt="" border="0" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkAsync2013/3122.asyncdesign4.jpg"&gt;&lt;img src="http://blogs.msdn.com/resized-image.ashx/__size/400x225/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkAsync2013/3122.asyncdesign4.jpg" alt="" border="0" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkAsync2013/5722.asyncdesign5.jpg"&gt;&lt;img src="http://blogs.msdn.com/resized-image.ashx/__size/400x225/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkAsync2013/5722.asyncdesign5.jpg" alt="" border="0" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;PS. The first anti-pattern is "async void". People of course ask: "If it's so wrong, then why did you allow it?" That's a good question. I'll write a blog post solely on this question next week.&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10394745" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/lucian/archive/tags/async/">async</category><category domain="http://blogs.msdn.com/b/lucian/archive/tags/talk/">talk</category></item><item><title>Talk: Async Part 2 - for architects, under the hood</title><link>http://blogs.msdn.com/b/lucian/archive/2013/02/17/talk-async-part-2-for-architects-under-the-hood.aspx</link><pubDate>Sun, 17 Feb 2013 19:01:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10394661</guid><dc:creator>Lucian Wischik, MSFT</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/lucian/rsscomments.aspx?WeblogPostID=10394661</wfw:commentRss><comments>http://blogs.msdn.com/b/lucian/archive/2013/02/17/talk-async-part-2-for-architects-under-the-hood.aspx#comments</comments><description>&lt;h1&gt;Talk: Async Part 2 - for architects, under the hood&lt;/h1&gt;
&lt;p&gt;This talk tackles some more advanced async topics.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;em&gt;&lt;strong&gt;Async idioms&lt;/strong&gt; - the "TAP" Task Asynchronous Pattern, when and when not to expose async APIs, the unexpected trouble with Task.Yield&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;em&gt;Integrating async into existing code&lt;/em&gt;&lt;/strong&gt;&lt;em&gt;&amp;nbsp;- call sync from async and vice versa, user-defined awaitables&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;em&gt;Async codegen and perf&lt;/em&gt;&lt;/strong&gt;&lt;em&gt;&amp;nbsp;- some small tweaks you can make, if performance analysis shows that an async method is in your critical path&lt;/em&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;For an architect or a library-author, I think you need to know a little more about the conventions and what's going on under the hood&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;I gave this talk at NDC2012 in Oslo, Norway on 2012.06.06&lt;/strong&gt;&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="http://ndc2012.oktaset.com/t-4926"&gt;Watch video&lt;/a&gt; online&lt;/li&gt;
&lt;li&gt;Download&amp;nbsp;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkAsync2012/2046.Async2-for-architects-_5B00_slides_5D00_-_2D00_-Lucian-Wischik-_2D00_-NDC2012-Oslo-2012.06.08.pptx"&gt;slides.pptx&lt;/a&gt;&amp;nbsp;[190k]&lt;/li&gt;
&lt;li&gt;Download&amp;nbsp;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkAsync2012/3113.Async2-for-architects-_5B00_script_5D00_-_2D00_-Lucian-Wischik-_2D00_-NDC2012-Oslo-2012.06.08.docx"&gt;script.docx&lt;/a&gt;&amp;nbsp;[21k]&lt;/li&gt;
&lt;li&gt;Download&amp;nbsp;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkAsync2012/1145.Async2-for-architects-_5B00_code_5D00_-_2D00_-Lucian-Wischik-_2D00_-NDC2012-Oslo-2012.06.08.zip"&gt;source_code.zip&lt;/a&gt;&amp;nbsp;[10k, requires VS2012 and Win8]&lt;br /&gt;&amp;nbsp;&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;&lt;strong&gt;Similar material elsewhere&lt;/strong&gt;&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;Stephen Toub blogged about "&lt;a href="http://blogs.msdn.com/b/pfxteam/archive/2012/04/13/10293638.aspx"&gt;Should I expose asynchronous wrappers for synchronous methods&lt;/a&gt;"&lt;/li&gt;
&lt;li&gt;He also blogged about the converse "&lt;a href="http://blogs.msdn.com/b/pfxteam/archive/2012/04/13/10293638.aspx"&gt;Should I expose SYNC wrappers for ASYNC methods&lt;/a&gt;"&lt;/li&gt;
&lt;li&gt;Together, these topics get to the heart of the interplay between sync and async in your code&lt;/li&gt;
&lt;/ul&gt;
&lt;/ul&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkAsync2012/0508.async21.jpg"&gt;&lt;img src="http://blogs.msdn.com/resized-image.ashx/__size/400x300/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkAsync2012/0508.async21.jpg" alt="" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkAsync2012/4380.async22.jpg"&gt;&lt;img src="http://blogs.msdn.com/resized-image.ashx/__size/400x300/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkAsync2012/4380.async22.jpg" alt="" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkAsync2012/3716.async23.jpg"&gt;&lt;img src="http://blogs.msdn.com/resized-image.ashx/__size/400x300/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkAsync2012/3716.async23.jpg" alt="" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkAsync2012/0488.async24.jpg"&gt;&lt;img src="http://blogs.msdn.com/resized-image.ashx/__size/400x300/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkAsync2012/0488.async24.jpg" alt="" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkAsync2012/4377.async25.jpg"&gt;&lt;img src="http://blogs.msdn.com/resized-image.ashx/__size/400x300/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkAsync2012/4377.async25.jpg" alt="" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10394661" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/lucian/archive/tags/async/">async</category><category domain="http://blogs.msdn.com/b/lucian/archive/tags/talk/">talk</category></item><item><title>How to write a custom awaiter</title><link>http://blogs.msdn.com/b/lucian/archive/2012/12/12/how-to-write-a-custom-awaiter.aspx</link><pubDate>Wed, 12 Dec 2012 01:45:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10376658</guid><dc:creator>Lucian Wischik, MSFT</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/lucian/rsscomments.aspx?WeblogPostID=10376658</wfw:commentRss><comments>http://blogs.msdn.com/b/lucian/archive/2012/12/12/how-to-write-a-custom-awaiter.aspx#comments</comments><description>&lt;p&gt;[This post is part of a series&amp;nbsp;&lt;a href="http://blogs.msdn.com/b/lucian/archive/2012/11/28/how-to-await-a-storyboard-and-other-things.aspx"&gt;How to await a storyboard, and other things&lt;/a&gt;]&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The normal behavior of the "await" operator on a task is to suspend execution of the method; then, &lt;em&gt;when the task operand has finished, to resume execution on the same SynchronizationContext&lt;/em&gt; and with the same ExecutionContext.&lt;/p&gt;
&lt;p&gt;You can replace the part in italics with your own logic by awaiting an operand of your own type (not a task) and writing a &lt;em&gt;custom awaiter&lt;/em&gt; for it. It's not a common need. Stephen Toub has already written an excellent article that covers this topic ("&lt;a href="http://blogs.msdn.com/b/pfxteam/archive/2011/01/13/10115642.aspx"&gt;Await anything&lt;/a&gt;"). He explains that a custom awaiter is for when "you need full control over how (rather than when) the method resumes". I wanted to build on his article from a compiler-writer's perspective and with some more example code. Here are some examples I've seen of custom awaiters.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.task.configureawait.aspx"&gt;&lt;strong&gt;Await task1.ConfigureAwait(false)&lt;/strong&gt;&lt;/a&gt;. Here execution doesn't make the effort to go back to the prior synchronization context, and often resumes on whatever thread the task happened to signal its completion upon. This is an efficiency boost, since it's faster than having to post back onto the prior SynchronizationContext.&lt;/li&gt;
&lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.task.yield.aspx"&gt;&lt;strong&gt;Await Task.Yield()&lt;/strong&gt;&lt;/a&gt;. Here there isn't an operand that finishes, as such. Instead this awaiter schedules execution to resume shortly on the same SynchronizationContext for some vague meaning of "shortly"... (Actually, the meaning of "shortly" is so vague that the only predictable use of this API is to force a suspension of an async method).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The above two scenarios were thought common enough that they're provided in the .NET45 framework. The rest aren't...&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="http://blogs.msdn.com/b/lucian/archive/2012/11/24/how-to-hibernate-async-methods-how-to-serialize-task.aspx"&gt;&lt;strong&gt;Await HibernateAsync("a.hib")&lt;/strong&gt;&lt;/a&gt;. I wrote an awaiter like this which didn't have any useful resumption. Instead it used the fact that a custom awaiter can (undocumentedly) use reflection to get at the current state of the async method, and thereby serialize it to disk.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Await task1.OnCoroutine(crm1)&lt;/strong&gt;. I saw some code which bypassed the SynchronizationContext mechanism completely. Instead it used its own co-routine managers: a set of lists-of-actions, which they executed in round-robin fashion.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Await SwitchToThreadpool() / &lt;/strong&gt;SwitchToDispatcher(dispatcher). I saw some code which used these two primitives to switch between threadpool and UI thread, as an alternative to the more common "Await Task.Run(...)" and "Await Dispatcher.RunAsync(...)" which both involve lambdas. It came from a wish to expunge all lambdas from a user's code.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Await SwitchPriority(High)&lt;/strong&gt;. Similar to the above example, it might be useful to start executing at a higher priority.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dim x = Await thread1.Enter() : ... : Await x.Leave()&lt;/strong&gt;. I thought of this as a synchronization primitive. The idea is that most of my code runs happily on multiple threads, but I block off certain critical sections which must be executed &lt;em&gt;on a particular thread&lt;/em&gt; maybe because they use thread-local-storage.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Await task1.Log()&lt;/strong&gt;. Here I was trying to track down a bug where Await wasn't resuming. I wrote a custom awaiter which wrote to a debug log at every stage along the way of suspending and resuming an async method.&lt;/li&gt;
&lt;li&gt;&lt;a href="http://blogs.msdn.com/b/pfxteam/archive/2011/01/13/10115642.aspx"&gt;&lt;strong&gt;Await task1.WithCulture()&lt;/strong&gt;&lt;/a&gt;. Normally, CurrentThread.CurrentCulture doesn't flow across await points. You can write a custom awaiter which does make it flow.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Await task1.TemporarilyConfigureAwait(false)&lt;/strong&gt;. Maybe you might want the efficiency-boost of task.ConfigureAwait(false), but you still want it to restore SynchronizationContext.Current so that &lt;em&gt;subsequent&lt;/em&gt; awaits will return to it.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Here's some background reading to explain the concepts...&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="http://blogs.msdn.com/b/pfxteam/archive/2012/06/15/executioncontext-vs-synchronizationcontext.aspx"&gt;ExecutionContext vs SynchronizationContext&lt;/a&gt; &amp;ndash; blog article by Stephen Toub&lt;/li&gt;
&lt;li&gt;&lt;a href="http://blogs.msdn.com/b/pfxteam/archive/2011/01/13/10115642.aspx"&gt;Await anything&lt;/a&gt; &amp;ndash; blog article by Stephen Toub, including one custom-awaiter that flows culture, one that switches to the threadpool, and one that switches to the UI&lt;/li&gt;
&lt;li&gt;&lt;a href="http://blogs.msdn.com/b/pfxteam/archive/2012/02/29/10274035.aspx"&gt;Await pattern&lt;/a&gt; &amp;ndash; blog article by Stephen Toub that describes the signature of a custom awaiter.&lt;/li&gt;
&lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.inotifycompletion.aspx"&gt;INotifyCompletion&lt;/a&gt; and &lt;a href="http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.icriticalnotifycompletion.aspx"&gt;ICriticalNotifyCompletion&lt;/a&gt; &amp;ndash; MSDN documentation about these two interfaces that a custom awaiter will have to implement.&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;/li&gt;
&lt;/ul&gt;
&lt;h1&gt;&lt;span style="background: white;"&gt;What the compiler requires from a custom awaiter&lt;/span&gt;&lt;/h1&gt;
&lt;p&gt;&lt;span style="background: white;"&gt;When you use the Await operator, the compiler expands it out into something a bit like this:&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: green; background: white;"&gt;' ORIGINAL CODE&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; r = &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Await&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; t&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: green; background: white;"&gt;' APPROXIMATELY WHAT THE COMPILER GENERATES&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; tmp = t.GetAwaiter()&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;If&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Not&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; tmp.IsCompleted &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Then&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;em&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/em&gt;&lt;em&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;/em&gt;&lt;em&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; ec = &lt;/span&gt;&lt;/em&gt;&lt;em&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;ExecutionContext&lt;/span&gt;&lt;/em&gt;&lt;em&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;.Capture()&lt;/span&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;em&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; CType(tmp, INotifyCompletion).OnCompleted(&lt;/span&gt;&lt;/em&gt;&lt;em&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;/em&gt;&lt;em&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;() &lt;/span&gt;&lt;/em&gt;&lt;em&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;ExecutionContext&lt;/span&gt;&lt;/em&gt;&lt;em&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;.Run(ec, &lt;/span&gt;&lt;/em&gt;&lt;em&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white;"&gt;K1&lt;/span&gt;&lt;/em&gt;&lt;em&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;))&lt;/span&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Return&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;If&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white;"&gt;K1:&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; r = tmp.GetResult()&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: green; background: white;"&gt;' Some things not shown in the above simplification: &lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: green; background: white;"&gt;' The two lines in italics are executed from within a routine inside mscorlib&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: green; background: white;"&gt;' The argument "K1" represents a delegate that, when executed, resumes execution below.&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: green; background: white;"&gt;' If "tmp" is a struct, any mutations after IsCompleted before GetResult may be lost.&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: green; background: white;"&gt;' If tmp implements ICriticalNotifyCompletion, then it calls tmp.UnsafeOnCompleted instead.&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: green; background: white;"&gt;' The variable "ec" gets disposed at the right time inside the Sub()&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: green; background: white;"&gt;' If ec is null, then it invokes K1 directly instead of within ExecutionContext.Run&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: green; background: white;"&gt;' There are optimizations for the case where ExecutionContext is unmodified.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: white;"&gt;From the compiler perspective, here's the bare minimum that you need to implement to be able to compile "Await t":&lt;/span&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong style="text-indent: -0.25in;"&gt;&lt;span style="background: white;"&gt;Dim tmp = t.GetAwaiter()&lt;/span&gt;&lt;/strong&gt;&lt;span style="background: white;"&gt; must compile, and GetAwaiter must be either an instance method which takes no parameters, or an extension method which takes just one "this" parameter. (From this you can deduce that GetAwaiter cannot take optional parameters, and can take generic type parameters only if it's an extension method).&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong style="text-indent: -0.25in;"&gt;&lt;span style="background: white;"&gt;Dim b = tmp.IsCompleted&lt;/span&gt;&lt;/strong&gt;&lt;span style="background: white;"&gt; must compile, and IsCompleted must be a property which takes no parameters and returns a Boolean. (From this you can deduce that IsCompleted must be an instance property).&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong style="text-indent: -0.25in;"&gt;&lt;span style="background: white;"&gt;tmp must implement INotifyCompletion&lt;/span&gt;&lt;/strong&gt;&lt;span style="background: white;"&gt;. It can optionally also implement ICriticalNotifyCompletion. These must be "CLR implements" &amp;ndash; the compiler will not use dynamic casts or user-defined conversions. If the delegate passed on OnCompleted/UnsafeOnCompleted is executed zero times then the async method will never resume. If it is executed more than once, then behavior of the async method is unspecified.&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong style="text-indent: -0.25in;"&gt;&lt;span style="background: white;"&gt;tmp.GetResult()&lt;/span&gt;&lt;/strong&gt;&lt;span style="background: white;"&gt; must compile, and GetResult must be an instance method which takes no parameters. If GetResult is a function, then this function's return-value is used as the result of the Await operator. If GetResult is a sub (i.e. void-returning), then the Await operator returns void.&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;If you do a late-bound await then the rules are a little different. A late-bound await is "await t" where t has type dynamic (C#) or Object (VB with Option Strict Off).&lt;/p&gt;
&lt;p class="MsoListParagraphCxSpFirst" style="margin-left: 38.5pt; text-indent: -.25in;"&gt;&lt;span style="background: white;"&gt;1.&lt;span style="font: 7.0pt 'Times New Roman';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;strong&gt;&lt;span style="background: white;"&gt;Dim tmp = t.GetAwaiter()&lt;/span&gt;&lt;/strong&gt;&lt;span style="background: white;"&gt; must compile as a late-bound invocation, and it will use the normal late-binder rules: it works with optional parameters, and IDynamicMetaObjectProvider, and even with a delegate field named GetAwaiter.&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoListParagraphCxSpMiddle" style="margin-left: 38.5pt; text-indent: -.25in;"&gt;&lt;span style="background: white;"&gt;2.&lt;span style="font: 7.0pt 'Times New Roman';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;strong&gt;&lt;span style="background: white;"&gt;Dim b = tmp.IsCompleted&lt;/span&gt;&lt;/strong&gt;&lt;span style="background: white;"&gt; must compile as a normal late-bound property access.&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoListParagraphCxSpMiddle" style="margin-left: 38.5pt; text-indent: -.25in;"&gt;&lt;span style="background: white;"&gt;3.&lt;span style="font: 7.0pt 'Times New Roman';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;strong&gt;&lt;span style="background: white;"&gt;tmp&lt;/span&gt;&lt;/strong&gt;&lt;span style="background: white;"&gt;&amp;nbsp;&lt;strong&gt;must implement INotifyCompletion&lt;/strong&gt; or ICriticalNotification. As before, it must implement them through CLR interface implementation; not through dynamic casts.&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoListParagraphCxSpLast" style="margin-left: 38.5pt; text-indent: -.25in;"&gt;&lt;span style="background: white;"&gt;4.&lt;span style="font: 7.0pt 'Times New Roman';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;strong&gt;&lt;span style="background: white;"&gt;tmp.GetResult()&lt;/span&gt;&lt;/strong&gt;&lt;span style="background: white;"&gt; must compile as a normal late-bound invocation, and the result of the late-bound invocation is the result of the late-bound Await operator.&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: white;"&gt;These are only the bare-minimum requirements for the code to compile and have kind of specified behavior at runtime. The rest of it, of how you implement GetAwaiter/IsCompleted/OnCompleted/GetResult, is entirely up to you and convention.&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;h1&gt;What conventions the user expects from a custom awaiter&lt;/h1&gt;
&lt;p&gt;Here is code for a custom awaiter that satisfies all the compiler's requirements, and behaves approximately similarly to how the default TaskAwaiter behaves ("suspend execution of the method; then, &lt;em&gt;when the operand has finished, resume execution on the same SynchronizationContext &lt;/em&gt;and with the same ExecutionContext").&lt;/p&gt;
&lt;p&gt;This code is pointless! The only reason you'd write a custom awaiter is because you want something &lt;em&gt;different&lt;/em&gt; from the default TaskAwaiter. (After all, if you were happy with the default TaskAwaiter behavior, then it'd be more efficient and more robust to just create one via TaskCompletionSource.Task.GetAwaiter). I'm putting this code here just as a starting point, to diverge from it. I've also made fields public to keep the code simple.&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Public&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Class&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;MyTask&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Of&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;T&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Public&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; result &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;T&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Public&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; isCompleted &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Boolean&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Public&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; continuations &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;New&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;List&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Of&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Action&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Public&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; mutex &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;New&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Object&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Public&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; GetAwaiter() &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;MyTaskAwaiter&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Of&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;T&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Return&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;New&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;MyTaskAwaiter&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Of&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;T&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;) &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;With&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; {.task = &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Me&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;}&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Public&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; SetResult(value &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;T&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; cc &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;List&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Of&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Action&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;SyncLock&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; mutex&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; result = value&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; isCompleted = &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;True&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cc = continuations&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; continuations = &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Nothing&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;SyncLock&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;For&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Each&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; c &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;In&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; cc : c() : &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Next&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Public&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; AddContinuation(continuation &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;action&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;SyncLock&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; mutex&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;If&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Not&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; isCompleted &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Then&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; continuations.Add(continuation) : &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Return&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;SyncLock&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Task&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;.Run(continuation)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Class&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Public&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Class&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;MyTaskAwaiter&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Of&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;T&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;)&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Implements&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; System.Runtime.CompilerServices.&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;INotifyCompletion&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Public&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; task &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;MyTask&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Of&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;T&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Public&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;ReadOnly&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Property&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; IsCompleted &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Boolean&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Get&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;SyncLock&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; task.mutex&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Return&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; task.isCompleted&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;SyncLock&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Get&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Property&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Public&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; GetResult() &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;T&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Return&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; task.result&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Public&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; OnCompleted(continuation &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Action&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;) _&lt;br /&gt; &amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Implements&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;INotifyCompletion&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;.OnCompleted&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; sc = &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;If&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;SynchronizationContext&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;.Current, &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;New&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;SynchronizationContext&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; task.AddContinuation(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;() sc.Post(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;() continuation(), &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Nothing&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;))&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Class&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Alternatively, if your code allows partially trusted callers, then the above code would be a security hole. Here's how we could write MyTaskAwaiter instead for this case:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: black; background: white;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: blue; background: white;"&gt;Assembly&lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: black; background: white;"&gt;: Security.&lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: #2b91af; background: white;"&gt;AllowPartiallyTrustedCallers&lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: black; background: white;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Public&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Class&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;MyTaskAwaiter&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Of&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;T&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;) : &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Implements&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;ICriticalNotifyCompletion&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Public&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; task &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;MyTask&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Of&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;T&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Public&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;ReadOnly&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Property&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; IsCompleted &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Boolean&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Get&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;SyncLock&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; task.mutex&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Return&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; task.isCompleted&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;SyncLock&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Get&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Property&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Public&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; GetResult() &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;T&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Return&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; task.result&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;Security.&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;SecurityCritical&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Public&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; UnsafeOnCompleted(continuation &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Action&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;) _&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Implements&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;ICriticalNotifyCompletion&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;.UnsafeOnCompleted&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; sc = &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;If&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;SynchronizationContext&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;.Current, &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;New&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;SynchronizationContext&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; task.AddContinuation(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;() sc.Post(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;() continuation(), &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Nothing&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;))&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Public&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; OnCompleted(continuation &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Action&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;) _&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Implements&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;INotifyCompletion&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;.OnCompleted&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Throw&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;New&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;NotSupportedException&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;()&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Class&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;span style="background: white;"&gt;AllowPartiallyTrustedCallers.&lt;/span&gt;&lt;/strong&gt;&lt;span style="background: white;"&gt; Part of the .NET security model is that, if a method in your assembly can be invoked from a partially trusted caller, then none of your APIs can allow that caller to break free of the ExecutionContext they started in. A partially-trusted attacker might call the first MyTaskAwaiter.OnCompleted() directly (i.e. bypassing the Await operator) and hence execute an arbitrary lambda in the ExecutionContext of whoever invoked MyTask.SetResult(). The second version works around this by implementing ICriticalNotifyCompletion and putting &amp;lt;SecurityCritical&amp;gt; on the UnsafeOnCompleted method to prevent partially-trusted callers from invoking it. We already saw that the compiler prefers to use the ICriticalNotifyCompletion interface if it's available, over INotifyCompletion. We also saw that the generated code's call to UnsafeOnCompleted is made indirectly via a routine in mscorlib, which is why it's able to invoke &amp;lt;SecurityCritical&amp;gt; code even if your assembly is only partially trusted. (The code for MyTask would also have to be tightened up, so that continuations/AddContinuation can't be touched by partially trusted callers).&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;span style="background: white;"&gt;Race conditions&lt;/span&gt;&lt;/strong&gt;&lt;span style="background: white;"&gt;. I believe the above code is free of race conditions and other concurrency bugs but I'm not sure. My first version suffered from several concurrency bugs. First bug: I forgot the lock on MyTask.AddContinuation(), allowing this interleaving:&lt;/span&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;span style="background: white;"&gt;Thread1 invokes MyTask.AddContinuation()&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="background: white;"&gt;Thread1 passes the "Not isCompleted" test&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="background: white;"&gt;Thread2 invokes MyTask.SetResult()&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="background: white;"&gt;Thread2 sets isCompleted to true and executes all delegates in "continuations"&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="background: white;"&gt;Thread1 adds its continuation to "continuations"&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="background: white;"&gt;RESULT: the continuation from Thread1 never gets executed.&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;span style="background: white;"&gt;Second bug: I forgot the lock on MyTaskAwaiter.IsCompleted, so the code was vulnerable to &lt;/span&gt;&lt;a href="http://msdn.microsoft.com/en-gb/magazine/cc163715.aspx"&gt;&lt;span style="background: white;"&gt;memory reordering&lt;/span&gt;&lt;/a&gt;&lt;span style="background: white;"&gt;: &lt;/span&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;span style="background: white;"&gt;&lt;span style="font: 7.0pt 'Times New Roman';"&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;span style="background: white;"&gt;Thread1 invokes SetResult&lt;br /&gt;&lt;span style="text-indent: -0.25in;"&gt;&amp;nbsp; &amp;nbsp; WRITE:result&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="text-indent: -0.25in;"&gt;&amp;nbsp; &amp;nbsp; WRITE:isCompleted = true&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="background: white;"&gt;Thread2 invokes IsCompleted followed by GetResult&lt;br /&gt;&lt;span style="text-indent: -0.25in;"&gt;&amp;nbsp; &amp;nbsp; READ:isCompleted returns true&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="text-indent: -0.25in;"&gt;&amp;nbsp; &amp;nbsp; READ:result&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="background: white;"&gt;If the hardware re-ordered the two WRITE operations (allowed by ECMA but not CLR), then we might get this interleaving:&lt;br /&gt;&lt;/span&gt;&lt;span style="text-indent: -0.25in;"&gt;&amp;nbsp; &amp;nbsp; WRITE:isCompleted = true&lt;br /&gt;&lt;/span&gt;&lt;span style="text-indent: -0.25in;"&gt;&amp;nbsp; &amp;nbsp; READ:isCompleted returns true&lt;br /&gt;&lt;/span&gt;&lt;span style="text-indent: -0.25in;"&gt;&amp;nbsp; &amp;nbsp; READ:result returns an invalid value&lt;br /&gt;&lt;/span&gt;&lt;span style="text-indent: -0.25in;"&gt;&amp;nbsp; &amp;nbsp; WRITE:result&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="text-indent: -0.25in;"&gt;If the hardware did speculative execution and re-ordered the two READ operations (I don't know if this is even allowed!) then we might get this interleaving:&lt;br /&gt;&lt;/span&gt;&lt;span style="text-indent: -0.25in;"&gt;&amp;nbsp; &amp;nbsp; READ:result returns an invalid value&lt;br /&gt;&lt;/span&gt;&lt;span style="text-indent: -0.25in;"&gt;&amp;nbsp; &amp;nbsp; WRITE:result&lt;br /&gt;&lt;/span&gt;&lt;span style="text-indent: -0.25in;"&gt;&amp;nbsp; &amp;nbsp; WRITE:isCompleted = true&lt;br /&gt;&lt;/span&gt;&lt;span style="text-indent: -0.25in;"&gt;&amp;nbsp; &amp;nbsp; READ:isCompleted returns true&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="text-indent: -0.25in;"&gt;RESULT: in both cases the GetResult() call retrieved an invalid value&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Third bug: In MyTask.SetResult(), the first version of my code looped over the continuations and executed them from inside the SyncLock. But this would have led to deadlock if any of the continuations themselves tried to await mytask from a different thread. The solution was to execute the continuations outside the SyncLock.&lt;/p&gt;
&lt;p&gt;Fourth bug: In AddContinuation, the first version of my code executed the continuation directly. But this would have blown the stack if I had a loop which awaited a MyTask that had already completed. The solution was to schedule the continuation for later execution with Task.Run(continuation).&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;span style="background: white;"&gt;NotSupportedException&lt;/span&gt;&lt;/strong&gt;&lt;span style="background: white;"&gt;. The AllowPartiallyTrustedCallers version of the code throws a NotSupportedException for anyone who calls its implementation of INotifyCompletion.OnCompleted. I figured there was no need, and it's hard to get it right so I'm better off just throwing the exception. After all, I'm only doing my custom awaiter to support the Await operator, and the compiler specs spell out the fact that the compiler won't invoke INotifyCompletion.OnCompleted on an awaiter that implements ICriticalNotifyCompletion.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;strong&gt;&lt;span style="background: white;"&gt;What to do if SynchronizationContext.Current is null?&lt;/span&gt;&lt;/strong&gt;&lt;span style="background: white;"&gt; My custom awaiter uses this code:&lt;br /&gt; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Dim&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; sc = &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;If&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;SynchronizationContext&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;.Current, &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;New&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;SynchronizationContext&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;)&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: green; background: white;"&gt;' &amp;nbsp;VB&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: blue; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; var&lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: black; background: white;"&gt; sc = &lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: #2b91af; background: white;"&gt;SynchronizationContext&lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: black; background: white;"&gt;.Current ?? &lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: blue; background: white;"&gt;new&lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: #2b91af; background: white;"&gt;SynchronizationContext&lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: black; background: white;"&gt;();&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: green; background: white;"&gt;// C#&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: white;"&gt;I did as part of implementing the common convention, that the "Await" operator will resume execution on whatever SynchronizationContext was current before it started. The question arises of what to do if Synchronization.Current was null, as is the case in a Console application or a unit test, or in the body of Task.Run(): where should the continuation be posted? In my code, the new synchronization context will post the continuation-delegate to the threadpool, which is decent-enough behavior.&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;h1&gt;&lt;span style="background: white;"&gt;Conclusions&lt;/span&gt;&lt;/h1&gt;
&lt;p&gt;&lt;span style="background: white;"&gt;The custom awaiter code is difficult &amp;ndash; difficult to follow the conventions, and difficult to write without concurrency bugs. If at all possible it's better to stick to the standard TaskAwaiter, or at least to build your own custom awaiter as a wrapper around TaskAwaiter.&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10376658" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/lucian/archive/tags/async/">async</category></item><item><title>How to await a command-line process, and capture its output</title><link>http://blogs.msdn.com/b/lucian/archive/2012/12/08/how-to-await-a-system-diagnostics-process.aspx</link><pubDate>Sat, 08 Dec 2012 03:27:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10375730</guid><dc:creator>Lucian Wischik, MSFT</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/lucian/rsscomments.aspx?WeblogPostID=10375730</wfw:commentRss><comments>http://blogs.msdn.com/b/lucian/archive/2012/12/08/how-to-await-a-system-diagnostics-process.aspx#comments</comments><description>&lt;p&gt;[This post is part of a series&amp;nbsp;&lt;a href="http://blogs.msdn.com/b/lucian/archive/2012/11/28/how-to-await-a-storyboard-and-other-things.aspx"&gt;How to await a storyboard, and other things&lt;/a&gt;]&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I want to invoke an external executable, and await until it&amp;rsquo;s finished, and get back its output. (In my case, the external executable is called &amp;ldquo;&lt;a href="http://tidy.sourceforge.net/"&gt;tidy.exe&lt;/a&gt;&amp;rdquo; &amp;ndash; an excellent open-source utility for turning HTML into clean parseable XML.)&amp;nbsp;To cut a long story short, here&amp;rsquo;s my final code. The rest of the article has explanations of how it works and why.&amp;nbsp;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Async&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; Attempt4Async() &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Task&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; client &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;New&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;HttpClient&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; html = &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Await&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; client.GetStringAsync(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white;"&gt;"http://blogs.msdn.com/lucian"&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; tidy = &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Await&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; RunCommandLineAsync(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white;"&gt;"tidy.exe"&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;,&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white;"&gt;"-asxml -numeric -quiet --doctype omit"&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;html)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; xml = &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;XDocument&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;.Parse(tidy)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Async&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; RunCommandLineAsync(cmd &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;String&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;, args &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;String&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;, stdin &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;String&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;,&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Optional&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; cancel &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;CancellationToken&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; = &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Nothing&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;) &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Task&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Of&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;String&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Using&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; p &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;New&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Process&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; p.StartInfo.FileName = cmd&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; p.StartInfo.Arguments = args&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; p.StartInfo.UseShellExecute = &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;False&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; p.StartInfo.RedirectStandardInput = &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;True&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; p.StartInfo.RedirectStandardOutput = &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;True&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; p.StartInfo.RedirectStandardError = &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;True&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;If&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Not&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; p.Start() &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Then&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Throw&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;New&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;InvalidOperationException&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white;"&gt;"no tidy.exe"&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Using&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; cancelReg = cancel.Register(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;() p.Kill())&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; tin = &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Async&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;()&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Await&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; p.StandardInput.WriteAsync(stdin)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; p.StandardInput.Close()&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;()&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; tout = p.StandardOutput.ReadToEndAsync()&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; terr = p.StandardError.ReadToEndAsync()&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Await&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Task&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;.WhenAll(tin, tout, terr)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; p.StandardOutput.Close()&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; p.StandardError.Close()&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Await&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Task&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;.Run(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;AddressOf&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; p.WaitForExit)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; stdout = &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Await&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; tout&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; stderr = &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Await&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; terr&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;If&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; cancel.IsCancellationRequested &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Then&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Throw&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;New&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;OperationCanceledException&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;(stderr)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;ElseIf&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;String&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;.IsNullOrEmpty(stdout) &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Then&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Throw&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;New&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Exception&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;(stderr)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Else&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Return&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; stdout&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;If&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Using&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Using&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h1&gt;Process.Exited event&lt;/h1&gt;
&lt;p&gt;Stephen Toub wrote a very useful article &amp;ldquo;&lt;a href="http://blogs.msdn.com/b/pfxteam/archive/2011/01/13/10115642.aspx"&gt;Await Anything&lt;/a&gt;&amp;rdquo; that I always use as my starting point. He included an example of awaiting for a process. That article used the Process.Exited event, and lead me to this code:&amp;nbsp;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Async&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; Attempt1Async() &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Task&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; p &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;New&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Process&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; p.StartInfo.FileName = &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white;"&gt;"notepad.exe"&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; p.Start()&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; tcs &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;New&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;TaskCompletionSource&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Of&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Integer&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; p.EnableRaisingEvents = &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;True&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;AddHandler&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; p.Exited, &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;() tcs.TrySetResult(p.ExitCode)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;If&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; p.HasExited &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Then&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; tcs.TrySetResult(p.ExitCode)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Await&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; tcs.Task&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Note the check for p.HasExited comes after adding the handler. If we didn&amp;rsquo;t do that, and if the process exited before adding the handler, then we&amp;rsquo;d never hear about it.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The code works great as is. But once I added in code to capture the output then I uncovered a race condition inside the Process.Exited event &amp;ndash; it seems that the triple combination of ReadToEnd(), Dispose() and HasExited can cause the Exited event to fire more than once!&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Repro: The following code will non-deterministically throw an exception because the process finishes and fires an event, then &amp;ldquo;await tcs.Task&amp;rdquo; finishes, then the event is fired a second time, and its attempt to read &amp;ldquo;p.ExitCode&amp;rdquo; is now invalid. The exception also seems to happen even if I RemoveHandler before EndUsing.&amp;nbsp;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Async&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; Attempt2Async() &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Task&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;For&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; i = 0 &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;To&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; 1000&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Using&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; p &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;New&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Process&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; p.StartInfo.FileName = &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white;"&gt;"cmd.exe"&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; p.StartInfo.Arguments = &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white;"&gt;"/c dir c:\windows /b"&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; p.StartInfo.UseShellExecute = &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;False&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; p.StartInfo.RedirectStandardOutput = &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;True&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; p.Start()&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; stdout = &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Await&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; p.StandardOutput.ReadToEndAsync()&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: green; background: white;"&gt;'&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; tcs &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;New&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;TaskCompletionSource&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Of&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Integer&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; p.EnableRaisingEvents = &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;True&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;AddHandler&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; p.Exited, &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;() tcs.TrySetResult(p.ExitCode)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;If&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; p.HasExited &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Then&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; tcs.TrySetResult(p.ExitCode)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Await&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; tcs.Task&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Using&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Next&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; i&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h1&gt;If the mechanism of communication is a wait-handle, then you need to block some thread&lt;/h1&gt;
&lt;p&gt;There might be clever ways to make Process.Exited work, but I didn&amp;rsquo;t care to risk them. So here&amp;rsquo;s my next attempt at awaiting until the process has finished. I tested it inside a while loop, and it doesn&amp;rsquo;t suffer from the same race condition as before.&amp;nbsp;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Async&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; Attempt3Async() &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Task&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; p &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;New&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Process&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; p.StartInfo.FileName = &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white;"&gt;"notepad.exe"&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; p.Start()&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Await&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; p&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&amp;nbsp;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Extension&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;gt; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; GetAwaiter(p &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Process&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;) &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;TaskAwaiter&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; t &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Task&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; = &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Task&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;.Run(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;AddressOf&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; p.WaitForExit)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Return&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; t.GetAwaiter()&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Here&amp;rsquo;s an explanation for why the GetAwaiter() is written as it is. Process has a method p.WaitForExit() which blocks a thread until the process has finished. It also exposes an IntPtr property &amp;ldquo;Handle&amp;rdquo;, which is the raw win32 handle that will be signalled once the process has completed. I want to await until the process has finished, or equivalently until the handle is signaled. &lt;em&gt;If the mechanism of communication is a wait handle, then I need to block some thread.&lt;/em&gt; Since I have to block some thread anyway, I might as well use WaitForExit(), which is slightly easier than the handle.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h1&gt;RunCommandLineAsync&lt;/h1&gt;
&lt;p&gt;I already started this article with the finished code, &amp;ldquo;RunCommandLineAsync&amp;rdquo;. Its code is subtle. Here&amp;rsquo;s an explanation.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Cancellation&lt;/strong&gt;. If cancellation is requested, then I need to be able to terminate the process. I do that with &amp;ldquo;cancel.Register(Sub() p.Kill())&amp;rdquo;. As soon as cancellation is requested, this will invoke the lambda, which will kill the process. This will terminate the process abruptly, closing the handles cleanly (i.e. without exception). In this state I have no guarantee that stdout is complete. Therefore, I throw an OperationCancelledException. Note that even if cancellation is requested and the process gets killed, I still await until the process has completely finished before returning (rather than returning immediately and letting it clean up asynchronously in the background). I figure this is a more hygienic design.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Tin, Tout, Terr&lt;/strong&gt;. The precise way that &amp;ldquo;RedirectStandardInput/Output/Error&amp;rdquo; will work depends on the internal details of the process we&amp;rsquo;re launching. All we can say with any certainty is that (1) the process might not finish until we close its StandardInput; (2) we might have to read data from StandardOutput/StandardError before we can write any more data into StandardInput; (3) we might have to write more data into StandardInput before we can read more data from StandardOutput/StandardError. Those constraints imply that the three tasks &amp;ldquo;tin/tout/terr&amp;rdquo; must all be launched concurrently, and then awaited using Task.WhenAll().&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Await tout&lt;/strong&gt;. The code does &amp;ldquo;Dim stdout = Await tout&amp;rdquo; even after we know that tout has finished (because we awaited Task.WhenAll). That&amp;rsquo;s fine. It merely means that the call to &amp;ldquo;Await tout&amp;rdquo; will complete immediately.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Async vs threads&lt;/strong&gt;. A few years ago, I wrote &lt;a href="http://blogs.msdn.com/b/lucian/archive/2009/02/21/web-scraping-with-vb-s-xml-support.aspx"&gt;similar code using threads&lt;/a&gt; instead of async. I'm happy that the async version is cleaner and more readable.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h1&gt;Wrapping it up&lt;/h1&gt;
&lt;p&gt;My ultimate goal behind this project was to be able to scrape web-pages. And I wanted to use VB&amp;rsquo;s XML-literals to make that easy. Here&amp;rsquo;s how I wrapped it up, to scrape all img tags from a page:&amp;nbsp;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Async&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; Attempt5Async(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Optional&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; cancel &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;CancellationToken&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; = &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Nothing&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;) &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Task&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; client &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;New&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;HttpClient&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; xml = &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Await&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; client.GetXmlAsync(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white;"&gt;"http://blogs.msdn.com/lucian"&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;, cancel)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;For&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Each&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; img &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;In&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; (&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;From&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; i &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;In&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; xml&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #6464b9; background: white;"&gt;...&amp;lt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;img&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #6464b9; background: white;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Select&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; i&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #6464b9; background: white;"&gt;.@&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;src &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Distinct&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Console&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;.WriteLine(img)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Next&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Extension&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Async&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; GetXmlAsync(client &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;HttpClient&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;, uri &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;String&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;,&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Optional&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; cancel &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;CancellationToken&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; = &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Nothing&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;) &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Task&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Of&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;XDocument&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Using&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; response = &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Await&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; client.GetAsync(uri, cancel)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; html = &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Await&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; response.Content.ReadAsStringAsync()&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; tidy = &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Await&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; RunCommandLineAsync(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white;"&gt;"tidy.exe"&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;,&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white;"&gt;"-asxml -numeric -quiet --doctype omit"&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;, html, cancel)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Return&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;XDocument&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;.Parse(tidy)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Using&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10375730" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/lucian/archive/tags/async/">async</category></item><item><title>Await HttpClient.GetStringAsync() and cancellation</title><link>http://blogs.msdn.com/b/lucian/archive/2012/12/08/await-httpclient-getstringasync-and-cancellation.aspx</link><pubDate>Sat, 08 Dec 2012 02:39:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10375728</guid><dc:creator>Lucian Wischik, MSFT</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/lucian/rsscomments.aspx?WeblogPostID=10375728</wfw:commentRss><comments>http://blogs.msdn.com/b/lucian/archive/2012/12/08/await-httpclient-getstringasync-and-cancellation.aspx#comments</comments><description>&lt;p&gt;I&amp;rsquo;m a big fan of supporting cancellation on async methods. I was recently wondering how to do this for HttpClient.GetStringAsync(). This post has some answers.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Async&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; TestAsync(uri &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Uri&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;, &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Optional&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; cancel &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;CancellationToken&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; = &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Nothing&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;) &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Task&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Of&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;String&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; client &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;New&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;HttpClient&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Return&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Await&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; client.GetStringAsync(uri, &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: yellow;"&gt;cancel&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;)&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: green; background: white;"&gt;' error: no overload of GetStringAsync takes a cancellation token&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;My first attempt is above. But it doesn&amp;rsquo;t compile, because HttpClient.GetStringAsync doesn&amp;rsquo;t have any overloads that take a CancellationToken.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;The answer? Instead of using GetStringAsync, we have to use GetAsync, which does take a cancellation-token parameter:&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; client &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;New&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;HttpClient&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Using&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; response = &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Await&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; client.GetAsync(uri,&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;HttpCompletionOption&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;.ResponseContentRead,&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cancel)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Return&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Await&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; response.Content.ReadAsStringAsync()&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Using&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&lt;br /&gt; &lt;br /&gt; &lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;The &amp;ldquo;ResponseContentRead&amp;rdquo; option can be omitted, since it is the default for calls to HttpClient.GetAsync. I included it just for clarity. What it means is that the entire body of the response is read by the call to HttpClient.GetAsync(), and so is subject to the cancellation token. Therefore the next call to ReadAsStringAsync() will complete immediately and needn&amp;rsquo;t worry about cancellation.&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;If you wanted to use the option &amp;ldquo;ResponseHeaderRead&amp;rdquo; instead, then cancellation looks different:&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Try&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Using&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; response = &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Await&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; client.GetAsync(uri,&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;HttpCompletionOption&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;.ResponseHeadersRead,&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cancel)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Using&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; cancelreg = cancel.Register(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;() response.Dispose())&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Return&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Await&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; response.Content.ReadAsStringAsync()&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Using&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Using&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Catch&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; ex &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;ObjectDisposedException&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;If&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; cancel.IsCancellationRequested &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Then&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Throw&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;New&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;OperationCanceledException&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Throw&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Try&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;Explanation: in this case, only the headers of the response are read by HttpClient.GetAsync(), and so only they are subject to cancellation by the cancellation token. When we next invoke ReadAsStringAsync(), this operation might take a long time, and so we need to figure out how to cancel it. No overload of ReadAsStringAsync() takes a cancellation token. The best we can do is &lt;em&gt;dispose&lt;/em&gt; of the HttpResponseMessage. This will cause the ReadAsStringAsync() method to terminate abruptly with an ObjectDisposedException.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10375728" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/lucian/archive/tags/async/">async</category></item><item><title>How to await a button click</title><link>http://blogs.msdn.com/b/lucian/archive/2012/11/28/how-to-await-a-button-click.aspx</link><pubDate>Wed, 28 Nov 2012 17:07:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10372744</guid><dc:creator>Lucian Wischik, MSFT</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/lucian/rsscomments.aspx?WeblogPostID=10372744</wfw:commentRss><comments>http://blogs.msdn.com/b/lucian/archive/2012/11/28/how-to-await-a-button-click.aspx#comments</comments><description>&lt;p class="MsoNormal"&gt;[This post is part of a series&amp;nbsp;&lt;a href="http://blogs.msdn.com/b/lucian/archive/2012/11/28/how-to-await-a-storyboard-and-other-things.aspx"&gt;How to await a storyboard, and other things&lt;/a&gt;]&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;Sometimes we want to await until a button has been clicked. Once place where this is useful is, for instance, when displaying the message &amp;ldquo;Click button to continue&amp;rdquo;.&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Await&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;button1.WhenClicked()&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;Why do it this way? Well, if you did it normally by writing top-level click handler event, then the event would be "always on". You'd need to enforce the invariant that the button was invisible when you didn't need it. Or, if the button were shared for different tasks at different times, then you'd have needed a top-level state field to record which of those tasks it's currently in use for. Or maybe you'd switch around which delegate was assigned to its Completed handler. Those approaches are all valid, but yhis "Await" approach works out nicer in some cases.&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;Here&amp;rsquo;s some code to support the above syntax. It's for win8, using RoutedEventHandler, but is easy to adapt to other platforms:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Extension&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;gt; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; WhenClicked(btn &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Button&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;) &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Task&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; tcs &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;New&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;TaskCompletionSource&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Of&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Object&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; lambda &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;RoutedEventHandler&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; = &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;(s, e)&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;RemoveHandler&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; btn.Click, lambda&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; tcs.SetResult(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Nothing&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;AddHandler&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; btn.Click, lambda&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Return&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; tcs.Task&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Note on naming convention:&lt;/strong&gt;&amp;nbsp;The .NET async guidelines say that async methods should end in the suffix "Async". I've violated that guideline here. When we made the guidelines, I don't think we thought through the cases where you use async for events. This alternative name is closer in spirit to Task.WhenAll and Task.WhenAny. Thanks to Quartermeister for the suggestion.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10372744" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/lucian/archive/tags/async/">async</category><category domain="http://blogs.msdn.com/b/lucian/archive/tags/win8/">win8</category></item><item><title>How to await a drag operation</title><link>http://blogs.msdn.com/b/lucian/archive/2012/11/28/how-to-await-a-drag-operation.aspx</link><pubDate>Wed, 28 Nov 2012 17:03:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10372743</guid><dc:creator>Lucian Wischik, MSFT</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/lucian/rsscomments.aspx?WeblogPostID=10372743</wfw:commentRss><comments>http://blogs.msdn.com/b/lucian/archive/2012/11/28/how-to-await-a-drag-operation.aspx#comments</comments><description>&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;[This post is part of a series&amp;nbsp;&lt;a href="http://blogs.msdn.com/b/lucian/archive/2012/11/28/how-to-await-a-storyboard-and-other-things.aspx"&gt;How to await a storyboard, and other things&lt;/a&gt;]&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;In my app, I want to await a drag operation. Here&amp;rsquo;s how I&amp;rsquo;d like to await it:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&amp;nbsp;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Private&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Async&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; rectangle1_PointerPressed(sender &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Object&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;,&lt;br /&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; e &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;PointerRoutedEventArgs&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;) &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Handles&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; rectangle1.PointerPressed&lt;br /&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Await&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; DragAsync(rectangle1, e)&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;Here below is my implementation for win8, for dragging a UIElement on a canvas. I didn't bother supporting a cancellation-token because it's easy for the user to cancel: just by letting go!&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; DragAsync(shape &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;UIElement&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;, origE &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;PointerRoutedEventArgs&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;,&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Optional&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; progress &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;IProgress&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Of&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Point&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;) = &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Nothing&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;) &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Task&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; tcs &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;New&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;TaskCompletionSource&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Of&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Point&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; origPos = &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;New&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Point&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Canvas&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;.GetLeft(shape), &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Canvas&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;.GetTop(shape))&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; origPt = origE.GetCurrentPoint(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Nothing&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;).Position&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; lambdaMoved &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;PointerEventHandler&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; = &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Nothing&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; lambdaReleased &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;PointerEventHandler&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; = &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Nothing&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lambdaMoved = &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;(s, e)&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; pt = e.GetCurrentPoint(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Nothing&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;).Position&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;If&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Not&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; progress &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Is&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Nothing&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Then&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; progress.Report(pt)&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Canvas&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;.SetLeft(shape, origPos.X + pt.X - origPt.X)&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Canvas&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;.SetTop(shape, origPos.Y + pt.Y - origPt.Y)&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lambdaReleased = &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;(s, e)&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;RemoveHandler&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; shape.PointerMoved, lambdaMoved&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;RemoveHandler&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; shape.PointerReleased, lambdaReleased&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; shape.ReleasePointerCapture(origE.Pointer)&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; tcs.SetResult(e.GetCurrentPoint(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Nothing&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;).Position)&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; shape.CapturePointer(origE.Pointer)&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;AddHandler&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; shape.PointerMoved, lambdaMoved&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;AddHandler&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; shape.PointerReleased, lambdaReleased&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Return&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; tcs.Task&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;Actually, let&amp;rsquo;s make it fancy with a rubber-band for the drag operation, relying on the fact that we wrote DragAsync to report back its progress:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Private&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Async&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; rectangle1_PointerPressed(sender &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Object&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;,&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;e &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;PointerRoutedEventArgs&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;) &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Handles&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; rectangle1.PointerPressed&lt;br /&gt; &lt;br /&gt; &lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; origPt = e.GetCurrentPoint(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Nothing&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;).Position&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; rubber &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;New&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; Shapes.&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Line&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;With&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {.X1 = origPt.X, .Y1 = origPt.Y,&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;.Stroke = &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;New&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;SolidColorBrush&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Colors&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;.Red),&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Visibility = Xaml.&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Visibility&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;.Collapsed}&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; canvas1.Children.Add(rubber)&lt;br /&gt; &lt;br /&gt; &lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; p &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;New&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Progress&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Of&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Point&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;)(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;(pt)&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;rubber.X2 = pt.X : rubber.Y2 = pt.Y&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rubber.Visibility = &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Visibility&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;.Visible&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;)&lt;br /&gt; &lt;br /&gt; &lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Await&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; DragAsync(rectangle1, e, p)&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; canvas1.Children.Remove(rubber)&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10372743" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/lucian/archive/tags/async/">async</category><category domain="http://blogs.msdn.com/b/lucian/archive/tags/win8/">win8</category></item><item><title>How to await a MediaElement (PlaySound in Windows 8)</title><link>http://blogs.msdn.com/b/lucian/archive/2012/11/28/how-to-await-a-mediaelement.aspx</link><pubDate>Wed, 28 Nov 2012 17:02:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10372741</guid><dc:creator>Lucian Wischik, MSFT</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/lucian/rsscomments.aspx?WeblogPostID=10372741</wfw:commentRss><comments>http://blogs.msdn.com/b/lucian/archive/2012/11/28/how-to-await-a-mediaelement.aspx#comments</comments><description>&lt;p class="MsoNormal"&gt;[This post is part of a series&amp;nbsp;&lt;a href="http://blogs.msdn.com/b/lucian/archive/2012/11/28/how-to-await-a-storyboard-and-other-things.aspx"&gt;How to await a storyboard, and other things&lt;/a&gt;]&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;Let&amp;rsquo;s look at making MediaElement awaitable. This is the kind of idiom I&amp;rsquo;d like to use:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Try&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Await&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; mediaElement1.OpenAsync(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;New&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Uri&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white;"&gt;"ms-appx:///Assets/boooo.mp3"&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;))&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Await&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; mediaElement1.PlayAsync()&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Catch&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; ex &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Exception&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: green; background: white;"&gt;...&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: blue; background: white;"&gt;Try&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;strong&gt;&lt;em&gt;Q. Why do I want to await it?&lt;/em&gt;&lt;/strong&gt; Why not just set its AutoPlay property to True, then set its Source property, and let it go?&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;A. Well, AutoPlay is fine for simple scenarios. But it doesn&amp;rsquo;t &lt;em&gt;compose&lt;/em&gt; in the nice way that await does. It doesn&amp;rsquo;t let me use my familiar exception handling blocks - inside I&amp;rsquo;d have to sign up to a MediaFailed event handler. And it doesn&amp;rsquo;t let me compose in other interesting ways, e.g.&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Await&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Task&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;.WhenAll(storyboard1.PlayAsync(), mediaElement1.Play())&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;strong&gt;ALERT. &lt;/strong&gt;MediaElement must be in the visual tree. Typically that means you create it as a child of your current Xaml page. If you want it to play globally, i.e. independent of which page is being shown, read this thread:&amp;nbsp;&lt;a href="http://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/241ba3b4-3e2a-4f9b-a704-87c7b1be7988/"&gt;http://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/241ba3b4-3e2a-4f9b-a704-87c7b1be7988/&lt;/a&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;Jumping straight to the answer, here&amp;rsquo;s my attempt at code that supports awaitability. There may be bugs in it: it&amp;rsquo;s complex, and this is my first attempt. &lt;em&gt;Requirements: neither of these operations is re-entrant: i.e. if other operations are performed on a given media element while either one is in progress, it might go awry.&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;/em&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Extension&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;gt; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; OpenAsync(media &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;MediaElement&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;, uri &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Uri&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;,&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Optional&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; cancel &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;CancellationToken&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; = &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Nothing&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;) &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Task&lt;br /&gt; &lt;br /&gt; &lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; tcs &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;New&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;TaskCompletionSource&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;(&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Of&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Object&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;If&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; cancel.IsCancellationRequested &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Then&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; tcs.SetCanceled()&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Return&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; tcs.Task&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;If&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;If&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; media.CurrentState = &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;MediaElementState&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;.Buffering &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;OrElse&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;media.CurrentState = &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;MediaElementState&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;.Opening &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;OrElse&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;media.CurrentState = &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;MediaElementState&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;.Playing &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Then&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; tcs.SetException(&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;New&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Exception&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;(&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #a31515; background: white;"&gt;"MediaElement not ready to open"&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;))&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Return&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; tcs.Task&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;If&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; lambdaOpened &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;RoutedEventHandler&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; = &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Nothing&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; lambdaChanged &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;RoutedEventHandler&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; = &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Nothing&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; lambdaFailed &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;ExceptionRoutedEventHandler&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; = &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Nothing&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; cancelReg &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;CancellationTokenRegistration&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;? = &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Nothing&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; removeLambdas &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Action&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; = &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;()&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;RemoveHandler&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; media.MediaOpened, lambdaOpened&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;RemoveHandler&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; media.MediaFailed, lambdaFailed&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;RemoveHandler&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; media.CurrentStateChanged, lambdaChanged&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;If&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; cancelReg.HasValue &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Then&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; cancelReg.Value.Dispose()&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; removeLambdas = &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;() &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Return&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: green; background: white;"&gt;' in case two lambdas get fired one after the other&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; lambdaOpened = &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;()&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;removeLambdas()&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; tcs.TrySetResult(&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Nothing&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; lambdaFailed = &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;(s, e)&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; removeLambdas()&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; tcs.TrySetException(&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;New&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Exception&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;(e.ErrorMessage))&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; lambdaChanged = &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;()&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;If&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; media.CurrentState &amp;lt;&amp;gt; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;MediaElementState&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;.Closed &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Then&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Return&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; removeLambdas()&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; tcs.TrySetCanceled()&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;AddHandler&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; media.MediaOpened, lambdaOpened&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;AddHandler&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; media.MediaFailed, lambdaFailed&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;AddHandler&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; media.CurrentStateChanged, lambdaChanged&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; media.Source = uri&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;If&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Not&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; tcs.Task.IsCompleted &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Then&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: green; background: white;"&gt;' The above condition guards against lambas being invoked by Source assignment&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cancelReg = cancel.Register(&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;()&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; dummy = media.Dispatcher.RunAsync(Core.&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;CoreDispatcherPriority&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;.Normal,&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;()&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; media.ClearValue(&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;MediaElement&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;.SourceProperty)&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;If&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Return&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; tcs.Task&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Extension&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;gt; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; PlayAsync(media &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;MediaElement&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;,&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Optional&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; cancel &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;CancellationToken&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; = &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Nothing&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;) &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Task&lt;br /&gt; &lt;br /&gt; &lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; tcs &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;New&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;TaskCompletionSource&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;(&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Of&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Object&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;If&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; cancel.IsCancellationRequested &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Then&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; tcs.SetCanceled()&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Return&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; tcs.Task&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;If&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;If&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; media.CurrentState &amp;lt;&amp;gt; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;MediaElementState&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;.Paused &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Then&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; tcs.SetException(&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;New&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Exception&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;(&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #a31515; background: white;"&gt;"MediaElement not ready to play"&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;))&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Return&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; tcs.Task&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;If&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; lambdaEnded &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;RoutedEventHandler&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; = &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Nothing&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; lambdaChanged &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;RoutedEventHandler&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; = &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Nothing&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; cancelReg &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;CancellationTokenRegistration&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;? = &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Nothing&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; removeLambdas &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Action&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; = &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;()&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;RemoveHandler&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; media.MediaEnded, lambdaEnded&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;RemoveHandler&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; media.CurrentStateChanged, lambdaChanged&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;If&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; cancelReg.HasValue &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Then&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; cancelReg.Value.Dispose()&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; removeLambdas = &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;() &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Return&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; lambdaEnded = &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;()&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; removeLambdas()&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; tcs.TrySetResult(&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Nothing&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; lambdaChanged = &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;()&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;If&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; media.CurrentState &amp;lt;&amp;gt; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;MediaElementState&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;.Stopped &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Then&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Return&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; removeLambdas()&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; tcs.TrySetCanceled()&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;AddHandler&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; media.MediaEnded, lambdaEnded&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;AddHandler&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; media.CurrentStateChanged, lambdaChanged&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; media.Play()&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;If&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Not&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; tcs.Task.IsCompleted &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Then&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cancelReg = cancel.Register(&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;()&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; dummy = media.Dispatcher.RunAsync(Core.&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;CoreDispatcherPriority&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;.Normal,&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;()&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; media.Stop()&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;If&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Return&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; tcs.Task&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;
&lt;h1&gt;What is the interactive behavior of MediaElement?&lt;/h1&gt;
&lt;p class="MsoNormal"&gt;There&amp;rsquo;s no way I could make MediaElement awaitable without knowing the details of how it behaves. I did my PhD on the &amp;ldquo;pi calculus&amp;rdquo;, a sort of mathematical algebra for describing and specifying interactive behaviors. Now I&amp;rsquo;m not going to delve into mathematics here, but I believe that software engineering requires the same careful approach when you get down to the edge cases of async. Turning a complex event-based thing into a Task-based thing is one of those edge cases.&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;em&gt;When studying the interactive behavior of a thing, then you HAVE to understand its complete state diagram. You have to understand in which states you&amp;rsquo;re allowed to trigger which stimuli (e.g. invoking the Play() method), and what their effect will be. You have to understand in which states the thing might have its own internal stimuli (e.g. reaching the end of a piece of media) and what effects these will have. If the documentation fails to specify all this, then you'll have to discover it by yourself.&lt;/em&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;em&gt;&lt;br /&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;I&amp;rsquo;m not a XAML expert. I don&amp;rsquo;t know the full details of how MediaElement behaves. But from experiment, this is what I believe is its state diagram:&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;
&lt;table class="MsoTableGrid" style="border-collapse: collapse; border: none;" border="1" cellspacing="0" cellpadding="0"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style="width: 79.05pt; border: solid windowtext 1.0pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top" width="105"&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal;"&gt;&lt;em&gt;State&lt;/em&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;td style="width: 89.3pt; border: solid windowtext 1.0pt; border-left: none; padding: 0in 5.4pt 0in 5.4pt;" valign="top" width="119"&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal;"&gt;&lt;em&gt;Possible internal transitions&lt;/em&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;td style="width: 77.8pt; border: solid windowtext 1.0pt; border-left: none; padding: 0in 5.4pt 0in 5.4pt;" valign="top" width="104"&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal;"&gt;&lt;em&gt;Upon setting Source&lt;/em&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;td style="width: 64.65pt; border: solid windowtext 1.0pt; border-left: none; padding: 0in 5.4pt 0in 5.4pt;" valign="top" width="86"&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal;"&gt;&lt;em&gt;Upon reset of Source&lt;/em&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;td style="width: 79.0pt; border: solid windowtext 1.0pt; border-left: none; padding: 0in 5.4pt 0in 5.4pt;" valign="top" width="105"&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal;"&gt;&lt;em&gt;Upon invoking Play()&lt;/em&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;td style="width: 77.7pt; border: solid windowtext 1.0pt; border-left: none; padding: 0in 5.4pt 0in 5.4pt;" valign="top" width="104"&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal;"&gt;&lt;em&gt;Upon invoking Stop()&lt;/em&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style="width: 79.05pt; border: solid windowtext 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt;" valign="top" width="105"&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal;"&gt;Closed&lt;/p&gt;
&lt;/td&gt;
&lt;td style="width: 89.3pt; border-top: none; border-left: none; border-bottom: solid windowtext 1.0pt; border-right: solid windowtext 1.0pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top" width="119"&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;/td&gt;
&lt;td style="width: 77.8pt; border-top: none; border-left: none; border-bottom: solid windowtext 1.0pt; border-right: solid windowtext 1.0pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top" width="104"&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal;"&gt;-&amp;gt; Opening&lt;/p&gt;
&lt;/td&gt;
&lt;td style="width: 64.65pt; border-top: none; border-left: none; border-bottom: solid windowtext 1.0pt; border-right: solid windowtext 1.0pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top" width="86"&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;/td&gt;
&lt;td style="width: 79.0pt; border-top: none; border-left: none; border-bottom: solid windowtext 1.0pt; border-right: solid windowtext 1.0pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top" width="105"&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;/td&gt;
&lt;td style="width: 77.7pt; border-top: none; border-left: none; border-bottom: solid windowtext 1.0pt; border-right: solid windowtext 1.0pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top" width="104"&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style="width: 79.05pt; border: solid windowtext 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt;" valign="top" width="105"&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal;"&gt;Opening&lt;/p&gt;
&lt;/td&gt;
&lt;td style="width: 89.3pt; border-top: none; border-left: none; border-bottom: solid windowtext 1.0pt; border-right: solid windowtext 1.0pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top" width="119"&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal;"&gt;-&amp;gt; Paused (MediaOpened)&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal;"&gt;-&amp;gt; Closed (MediaFailed)&lt;/p&gt;
&lt;/td&gt;
&lt;td style="width: 77.8pt; border-top: none; border-left: none; border-bottom: solid windowtext 1.0pt; border-right: solid windowtext 1.0pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top" width="104"&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal;"&gt;?&lt;/p&gt;
&lt;/td&gt;
&lt;td style="width: 64.65pt; border-top: none; border-left: none; border-bottom: solid windowtext 1.0pt; border-right: solid windowtext 1.0pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top" width="86"&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal;"&gt;-&amp;gt; Closed&lt;/p&gt;
&lt;/td&gt;
&lt;td style="width: 79.0pt; border-top: none; border-left: none; border-bottom: solid windowtext 1.0pt; border-right: solid windowtext 1.0pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top" width="105"&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;/td&gt;
&lt;td style="width: 77.7pt; border-top: none; border-left: none; border-bottom: solid windowtext 1.0pt; border-right: solid windowtext 1.0pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top" width="104"&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style="width: 79.05pt; border: solid windowtext 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt;" valign="top" width="105"&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal;"&gt;Paused&lt;/p&gt;
&lt;/td&gt;
&lt;td style="width: 89.3pt; border-top: none; border-left: none; border-bottom: solid windowtext 1.0pt; border-right: solid windowtext 1.0pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top" width="119"&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;/td&gt;
&lt;td style="width: 77.8pt; border-top: none; border-left: none; border-bottom: solid windowtext 1.0pt; border-right: solid windowtext 1.0pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top" width="104"&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal;"&gt;-&amp;gt; Opening&lt;/p&gt;
&lt;/td&gt;
&lt;td style="width: 64.65pt; border-top: none; border-left: none; border-bottom: solid windowtext 1.0pt; border-right: solid windowtext 1.0pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top" width="86"&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal;"&gt;-&amp;gt; Closed&lt;/p&gt;
&lt;/td&gt;
&lt;td style="width: 79.0pt; border-top: none; border-left: none; border-bottom: solid windowtext 1.0pt; border-right: solid windowtext 1.0pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top" width="105"&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal;"&gt;-&amp;gt; Buffering&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal;"&gt;-&amp;gt; Playing&lt;/p&gt;
&lt;/td&gt;
&lt;td style="width: 77.7pt; border-top: none; border-left: none; border-bottom: solid windowtext 1.0pt; border-right: solid windowtext 1.0pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top" width="104"&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal;"&gt;-&amp;gt; Stopped&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style="width: 79.05pt; border: solid windowtext 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt;" valign="top" width="105"&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal;"&gt;Buffering&lt;/p&gt;
&lt;/td&gt;
&lt;td style="width: 89.3pt; border-top: none; border-left: none; border-bottom: solid windowtext 1.0pt; border-right: solid windowtext 1.0pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top" width="119"&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal;"&gt;-&amp;gt; Playing&lt;/p&gt;
&lt;/td&gt;
&lt;td style="width: 77.8pt; border-top: none; border-left: none; border-bottom: solid windowtext 1.0pt; border-right: solid windowtext 1.0pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top" width="104"&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal;"&gt;?&lt;/p&gt;
&lt;/td&gt;
&lt;td style="width: 64.65pt; border-top: none; border-left: none; border-bottom: solid windowtext 1.0pt; border-right: solid windowtext 1.0pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top" width="86"&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal;"&gt;?&lt;/p&gt;
&lt;/td&gt;
&lt;td style="width: 79.0pt; border-top: none; border-left: none; border-bottom: solid windowtext 1.0pt; border-right: solid windowtext 1.0pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top" width="105"&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;/td&gt;
&lt;td style="width: 77.7pt; border-top: none; border-left: none; border-bottom: solid windowtext 1.0pt; border-right: solid windowtext 1.0pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top" width="104"&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal;"&gt;-&amp;gt; Stopped&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style="width: 79.05pt; border: solid windowtext 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt;" valign="top" width="105"&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal;"&gt;Playing&lt;/p&gt;
&lt;/td&gt;
&lt;td style="width: 89.3pt; border-top: none; border-left: none; border-bottom: solid windowtext 1.0pt; border-right: solid windowtext 1.0pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top" width="119"&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal;"&gt;-&amp;gt; Buffering&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal;"&gt;-&amp;gt; Paused (MediaEnded)&lt;/p&gt;
&lt;/td&gt;
&lt;td style="width: 77.8pt; border-top: none; border-left: none; border-bottom: solid windowtext 1.0pt; border-right: solid windowtext 1.0pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top" width="104"&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal;"&gt;?&lt;/p&gt;
&lt;/td&gt;
&lt;td style="width: 64.65pt; border-top: none; border-left: none; border-bottom: solid windowtext 1.0pt; border-right: solid windowtext 1.0pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top" width="86"&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal;"&gt;?&lt;/p&gt;
&lt;/td&gt;
&lt;td style="width: 79.0pt; border-top: none; border-left: none; border-bottom: solid windowtext 1.0pt; border-right: solid windowtext 1.0pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top" width="105"&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;/td&gt;
&lt;td style="width: 77.7pt; border-top: none; border-left: none; border-bottom: solid windowtext 1.0pt; border-right: solid windowtext 1.0pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top" width="104"&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal;"&gt;-&amp;gt; Stopped&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style="width: 79.05pt; border: solid windowtext 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt;" valign="top" width="105"&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal;"&gt;Stopped&lt;/p&gt;
&lt;/td&gt;
&lt;td style="width: 89.3pt; border-top: none; border-left: none; border-bottom: solid windowtext 1.0pt; border-right: solid windowtext 1.0pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top" width="119"&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;/td&gt;
&lt;td style="width: 77.8pt; border-top: none; border-left: none; border-bottom: solid windowtext 1.0pt; border-right: solid windowtext 1.0pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top" width="104"&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal;"&gt;-&amp;gt; Opening&lt;/p&gt;
&lt;/td&gt;
&lt;td style="width: 64.65pt; border-top: none; border-left: none; border-bottom: solid windowtext 1.0pt; border-right: solid windowtext 1.0pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top" width="86"&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal;"&gt;?&lt;/p&gt;
&lt;/td&gt;
&lt;td style="width: 79.0pt; border-top: none; border-left: none; border-bottom: solid windowtext 1.0pt; border-right: solid windowtext 1.0pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top" width="105"&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal;"&gt;-&amp;gt; Buffering&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal;"&gt;-&amp;gt; Playing&lt;/p&gt;
&lt;/td&gt;
&lt;td style="width: 77.7pt; border-top: none; border-left: none; border-bottom: solid windowtext 1.0pt; border-right: solid windowtext 1.0pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top" width="104"&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;Read the table like this: &amp;ldquo;If the media element is currently in &amp;lt;State&amp;gt;, then it might transition &amp;ldquo;-&amp;gt;&amp;rdquo; to another state either due to its internal workings or due to some method being invoked. If it does transition, then first its CurrentState property is changed, then it may fire a specified event on the UI thread, then it fires the CurrentStateChanged event on the UI thread.&amp;rdquo;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;The media element starts in state &amp;ldquo;Closed&amp;rdquo; upon construction. If its Source property is set in XAML, then that has the same effect as setting the source property in code. The column for &amp;ldquo;reset of source&amp;rdquo; refers to invoking the method &amp;ldquo;mediaElement1.ClearValue(MediaElement.SourceProperty)&amp;rdquo;. That is the only way I have found to completely reset the Source property from code.&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;I noticed that the transition &amp;ldquo;Opening -&amp;gt; Closed (MediaFailed)&amp;rdquo; can take up to 10 seconds in the case of an http:// uri which either doesn&amp;rsquo;t exist (returns an error http code) or where your app lacks Internet Client permissions.&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;I haven&amp;rsquo;t tried to characterize every possible method than can be invoked on mediaElement. I only characterized the ones needed to support my goals.&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;h1&gt;Conclusions&lt;/h1&gt;
&lt;p class="MsoNormal"&gt;Does this code look way more complicated to you than it should? It does to me. To be clear, the &lt;em&gt;use&lt;/em&gt; of the code is very simple&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Await&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; mediaElement1.OpenAsync(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;New&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Uri&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white;"&gt;"ms-appx:///Assets/boooo.mp3"&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;))&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Await&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; mediaElement1.PlayAsync()&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;It&amp;rsquo;s just the implementation of awaitability-support that&amp;rsquo;s complex. Why so complex? Actually, my initial attempt was much simpler. It merely played a media element which already had its Source property set to the right value, and had already opened it successfully.&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Extension&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;gt; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; PlayAsync1(media &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;MediaElement&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;) &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Task&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; tcs &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;New&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;TaskCompletionSource&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Of&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Object&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; lambda &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;RoutedEventHandler&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; = &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;()&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;RemoveHandler&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; media.MediaEnded, lambda&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;tcs.SetResult(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Nothing&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;AddHandler&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; media.MediaEnded, lambda&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; media.Play()&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Return&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; tcs.Task&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;It got more complex because (0) I wanted to support setting the source property, (1) I wanted to support cancellation, (2) I wanted to figure out the complete state diagram, to know in which states it&amp;rsquo;s valid to call PlayAsync, and (3) I wanted to know whether PlayAsync might ever end in failure. People often complain that &amp;ldquo;the Play() method doesn&amp;rsquo;t do anything&amp;rdquo;, and the answer is that Play() is only valid after MediaOpened has fired.&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;My analysis isn&amp;rsquo;t complete. I don&amp;rsquo;t know what happens if you set the source at the wrong time. What would happen if you did &amp;ldquo;Dim t1 = media1.OpenAsync(uri1), t2 = media1.OpenAsync(uri2)&amp;rdquo; ? Would it end up with a random one of the two URIs opened? Or would it end up in some broken internal state? Or what happens if, in response to a button-click, you did &amp;ldquo;Await media1.OpenAsync(uri)&amp;rdquo;, and the user impatiently clicks the button a second time while it was still opening the first one? Would it finish the first open operation? or abort it and start the second open operation? Or would it end in some broken state?&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06/1106.audio_2D00_controls.png"&gt;&lt;img src="http://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06/1106.audio_2D00_controls.png" alt="" width="209" height="75" /&gt;&lt;/a&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;The ultimate problem is that MediaElement has two very different uses &amp;ndash; either as a full-blown media playing control with start/stop/seek controls, or just for doing PlaySoundAsync(). The first use isn&amp;rsquo;t much like a task at all. That&amp;rsquo;s because there&amp;rsquo;s no single obvious completion event, and because it isn&amp;rsquo;t &lt;em&gt;monotonic&lt;/em&gt;. A monotonic state diagram is one where you never return to an earlier state. Readonly variables are also monotonic, since they transition from &amp;ldquo;unassigned&amp;rdquo; to &amp;ldquo;assigned-with-value&amp;rdquo;. Monotonic things are much easier to program against. Compare the state-diagram for MediaElement above, with the simpler state diagram for Tasks that come out of async methods. It&amp;rsquo;s easy to know where you are with tasks.&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;table class="MsoTableGrid" style="border-collapse: collapse; border: none;" border="1" cellspacing="0" cellpadding="0"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style="width: 233.75pt; border: solid windowtext 1.0pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top" width="312"&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal;"&gt;&lt;em&gt;State&lt;/em&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;td style="width: 233.75pt; border: solid windowtext 1.0pt; border-left: none; padding: 0in 5.4pt 0in 5.4pt;" valign="top" width="312"&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal;"&gt;&lt;em&gt;Possible internal transitions&lt;/em&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style="width: 233.75pt; border: solid windowtext 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt;" valign="top" width="312"&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal;"&gt;WaitingForActivation&lt;/p&gt;
&lt;/td&gt;
&lt;td style="width: 233.75pt; border-top: none; border-left: none; border-bottom: solid windowtext 1.0pt; border-right: solid windowtext 1.0pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top" width="312"&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal;"&gt;-&amp;gt; RanToCompletion&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal;"&gt;-&amp;gt; Canceled&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal;"&gt;-&amp;gt; Faulted&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style="width: 233.75pt; border: solid windowtext 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt;" valign="top" width="312"&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal;"&gt;RanToCompletion&lt;/p&gt;
&lt;/td&gt;
&lt;td style="width: 233.75pt; border-top: none; border-left: none; border-bottom: solid windowtext 1.0pt; border-right: solid windowtext 1.0pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top" width="312"&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style="width: 233.75pt; border: solid windowtext 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt;" valign="top" width="312"&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal;"&gt;Canceled&lt;/p&gt;
&lt;/td&gt;
&lt;td style="width: 233.75pt; border-top: none; border-left: none; border-bottom: solid windowtext 1.0pt; border-right: solid windowtext 1.0pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top" width="312"&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style="width: 233.75pt; border: solid windowtext 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt;" valign="top" width="312"&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal;"&gt;Faulted&lt;/p&gt;
&lt;/td&gt;
&lt;td style="width: 233.75pt; border-top: none; border-left: none; border-bottom: solid windowtext 1.0pt; border-right: solid windowtext 1.0pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top" width="312"&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;I do wonder whether it might be best to abandon awaitability of MediaElement itself, and encapsulate it into a simpler API like PlaySoundAsync. But this then would have its own problems: how do I pause it? how do I declare it in XAML? how do I open it just once but then play it multiple times? What forms of uri does it support? How to set the volume? How does it find the visual-tree? My encapsulation might end up just as complicated as MediaElement!&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; PlaySoundAsync(uri &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Uri&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;, &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Optional&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; cancel &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;CancellationToken&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;) &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Task&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: green; background: white;"&gt;...&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10372741" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/lucian/archive/tags/async/">async</category><category domain="http://blogs.msdn.com/b/lucian/archive/tags/win8/">win8</category></item><item><title>How to await a storyboard, and other things</title><link>http://blogs.msdn.com/b/lucian/archive/2012/11/28/how-to-await-a-storyboard-and-other-things.aspx</link><pubDate>Wed, 28 Nov 2012 16:54:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10372738</guid><dc:creator>Lucian Wischik, MSFT</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/lucian/rsscomments.aspx?WeblogPostID=10372738</wfw:commentRss><comments>http://blogs.msdn.com/b/lucian/archive/2012/11/28/how-to-await-a-storyboard-and-other-things.aspx#comments</comments><description>&lt;p&gt;This post is part of a series about an important new design pattern,&amp;nbsp;&lt;strong&gt;awaiting events&lt;/strong&gt;... I also made a Channel9 video introduction "&lt;a href="http://channel9.msdn.com/Series/Three-Essential-Tips-for-Async/Lucian03-TipsForAsyncThreadsAndDatabinding"&gt;Async Over Events&lt;/a&gt;". In this blog series:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="http://blogs.msdn.com/b/lucian/archive/2012/11/28/how-to-await-a-storyboard-and-other-things.aspx"&gt;How to await a storyboard, and other things&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://blogs.msdn.com/b/lucian/archive/2012/11/28/how-to-await-a-mediaelement.aspx"&gt;How to await a MediaElement&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://blogs.msdn.com/b/lucian/archive/2012/11/28/how-to-await-a-drag-operation.aspx"&gt;How to await a drag operation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://blogs.msdn.com/b/lucian/archive/2012/11/28/how-to-await-a-button-click.aspx"&gt;How to await a button-click&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://blogs.msdn.com/b/lucian/archive/2012/12/08/how-to-await-a-system-diagnostics-process.aspx"&gt;How to await a System.Diagnostics.Process (await an external program)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://blogs.msdn.com/b/lucian/archive/2012/12/12/how-to-write-a-custom-awaiter.aspx"&gt;How to write a custom awaiter&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Other resources:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="http://awaitableui.codeplex.com/"&gt;AwaitableUI&lt;/a&gt; - a codeplex project that provides similar wrappers&lt;/li&gt;
&lt;li&gt;&lt;a href="https://socialeboladev.wordpress.com/category/animation/"&gt;Awaitable ManagedAnimation class&lt;/a&gt; for WinRT&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&amp;nbsp; &amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I want to be able to &amp;ldquo;await a storyboard&amp;rdquo;.&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-left: .5in;"&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: blue; background: white;"&gt;Await&lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: black; background: white;"&gt; storyboard1.PlayAsync()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-left: .5in;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;Actually, there are several syntaxes I might choose for awaiting a storyboard:&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Await&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; storyboard1&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Await&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; storyboard1.PlayAsync()&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Await&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; PlayStoryboardAsync(storyboard1)&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;Indeed there are lots of other things that I&amp;rsquo;d like to be awaitable but aren&amp;rsquo;t (and for each of them I&amp;rsquo;ll have to choose which of the three syntaxes). This article is about making things become awaitable. First we&amp;rsquo;ll see &lt;em&gt;how&lt;/em&gt; to make things awaitable. Then we&amp;rsquo;ll evaluate which of the three syntaxes is appropriate in which circumstances.&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;h1&gt;How to await a storyboard&lt;/h1&gt;
&lt;p class="MsoNormal"&gt;I&amp;rsquo;ll jump straight to the punch-line. This is the code you need to await a storyboard in win8:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Extension&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;gt; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; PlayAsync(sb &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; Animation.&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Storyboard&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;) &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Task&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; tcs &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;New&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;TaskCompletionSource&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Of&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Object&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; lambda &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;EventHandler&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Of&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Object&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;) = &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;()&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;RemoveHandler&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; sb.Completed, lambda&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; tcs.TrySetResult(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Nothing&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;AddHandler&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; sb.Completed, lambda&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; sb.Begin()&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Return&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; tcs.Task&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;For this example, and for the next few articles, I&amp;rsquo;m working on a simple &amp;ldquo;blank page&amp;rdquo; Windows 8 app with the following in its Mainpage.xaml:&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white;"&gt;Page&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white;"&gt;&amp;nbsp;x&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;:&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white;"&gt;Class&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;="AppVB.MainPage"&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white;"&gt;&amp;nbsp;xmlns&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;="http://schemas.microsoft.com/winfx/2006/xaml/presentation"&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white;"&gt;&amp;nbsp;xmlns&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;:&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white;"&gt;x&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;="http://schemas.microsoft.com/winfx/2006/xaml"&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white;"&gt;&amp;nbsp;xmlns&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;:&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white;"&gt;local&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;="using:AppVB"&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white;"&gt;&amp;nbsp;xmlns&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;:&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white;"&gt;d&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;="http://schemas.microsoft.com/expression/blend/2008"&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white;"&gt;&amp;nbsp;xmlns&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;:&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white;"&gt;mc&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;="http://schemas.openxmlformats.org/markup-compatibility/2006"&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white;"&gt;&amp;nbsp;mc&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;:&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white;"&gt;Ignorable&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;="d"&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white;"&gt;Page.Resources&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white;"&gt;Storyboard&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white;"&gt; x&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;:&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white;"&gt;Name&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;="storyboard1"&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white;"&gt;DoubleAnimation&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white;"&gt; Storyboard.TargetName&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;="rectangle1"&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Storyboard.TargetProperty&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;="(Canvas.Left)"&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white;"&gt;&amp;nbsp;From&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;="0"&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white;"&gt; To&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;="500"&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white;"&gt; Duration&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;="0:0:1"/&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white;"&gt;Storyboard&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white;"&gt;MediaElement&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white;"&gt; x&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;:&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white;"&gt;Name&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;="mediaElement1"&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white;"&gt; AutoPlay&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;="False"&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Source&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;="Assets/boooo.mp3"/&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white;"&gt;Page.Resources&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white;"&gt;Canvas&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white;"&gt; x&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;:&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white;"&gt;Name&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;="canvas1"&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white;"&gt; Background&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;="Black"&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white;"&gt;Button&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white;"&gt; x&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;:&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white;"&gt;Name&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;="button1"&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white;"&gt; Content&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;="Button" &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white;"&gt;Canvas.Top&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;="150"/&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white;"&gt;Rectangle&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white;"&gt; x&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;:&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white;"&gt;Name&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;="rectangle1"&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white;"&gt; Fill&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;="yellow"&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white;"&gt; Width&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;="100"&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white;"&gt; Height&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;="100"/&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white;"&gt;Canvas&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white;"&gt;Page&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;h1&gt;Why make things awaitable?&lt;/h1&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;There are three main reasons for wanting something to be awaitable:&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;(1) If I can await the storyboard, then I can use the &amp;ldquo;await&amp;rdquo; keyword instead of adding a callback handler to the Storyboard.Completed event. I don&amp;rsquo;t like callbacks because they turn into spaghetti code; I prefer to use &amp;ldquo;await&amp;rdquo; because it keeps my code readable.&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;(2) If I can await the storyboard via the last two syntaxes, then it opens up powerful possibilities, e.g.&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Await&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Task&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;.WhenAll(storyboard1.PlayAsync(), mediaElement1.PlayAsync())&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;In a future article I&amp;rsquo;ll dive into the new &lt;em&gt;async choreography&lt;/em&gt; design patterns that this opens up, for coding the sequences of button-clicks and animations and events that make up an interactive application.&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;(3) If I make something awaitable, then it gives me some interesting flexibility. I can change the way that code resumes after an await, similar to how &amp;ldquo;Await t.ConfigureAwait(false)&amp;rdquo; resumes more efficiently. I can make an awaitable that resumes at a different priority or even a different thread, e.g. &amp;ldquo;Await SwitchThread()&amp;rdquo;. I can implement co-routines. These advanced techniques all require &lt;em&gt;custom awaiters&lt;/em&gt;, which I&amp;rsquo;ll address in a future article.&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;
&lt;h1&gt;How to support the raw Await syntax&lt;/h1&gt;
&lt;p class="MsoNormal"&gt;The first await syntax is the most striking:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Await&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; e&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;If the compiler encounters the above expression, then under the hood it makes a call to&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-left: .5in;"&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: black; background: white;"&gt; temp = e.GetAwaiter()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;Therefore: to allow users to write &amp;ldquo;Await e&amp;rdquo;, we must provide a GetAwaiter method. It has to return a value which satisfies the &lt;em&gt;awaiter pattern&lt;/em&gt;. I&amp;rsquo;ll spell out the full details of that in a future post, but for now we&amp;rsquo;ll just return an instance of System.Runtime.CompilerServices.TaskAwaiter, or its generic version TaskAwaiter(Of T). Here&amp;rsquo;s how I might do it for Storyboard. Storyboard is a built-in type, so I can&amp;rsquo;t add GetAwaiter as an instance method, so instead I&amp;rsquo;ll provide it as an extension method:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Extension&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;gt; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; GetAwaiter(sb &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; Animation.&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Storyboard&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;) &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;TaskAwaiter&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; tcs &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;New&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;TaskCompletionSource&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Of&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Object&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; lambda &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;EventHandler&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Of&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Object&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;) = &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;()&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;RemoveHandler&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; sb.Completed, lambda&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; tcs.TrySetResult(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Nothing&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;AddHandler&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; sb.Completed, lambda&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; sb.Begin()&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; t &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Task&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; = tcs.Task&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; awaiter &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; System.Runtime.CompilerServices.&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;TaskAwaiter&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; = t.GetAwaiter()&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Return&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; awaiter&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;NB. The first code in this function was for &amp;ldquo;&amp;lt;Extension&amp;gt; Function PlayAsync(...) As Task&amp;rdquo;. The reason that works is because the built-in Task type has its own GetAwaiter() instance method which returns TaskAwaiter.&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;If I want to allow users to write &amp;ldquo;Dim x As Integer = Await storyboard1&amp;rdquo;, then I&amp;rsquo;d have had to return TaskAwaiter(Of Integer) instead. And likewise, Task(Of T) has a GetAwaiter() instance method which returns TaskAwaiter(Of T).&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;There are some additional restrictions. GetAwaiter must be a method (not a property or delegate field). It cannot have any optional parameters. It&amp;rsquo;s allowed to be an instance or an extension method, and if an extension method then it&amp;rsquo;s allowed to be generic. (Technical note: if the call to GetAwaiter were late-bound, i.e. if &amp;ldquo;e&amp;rdquo; had type Object in VB or dynamic in C#, then the restrictions are relaxed and normal late-bound rules apply.)&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;You might be wondering about callbacks. The whole point of &amp;ldquo;await&amp;rdquo; is to liberate you from callbacks because they&amp;rsquo;re unpleasant to code with. But we&amp;rsquo;ve used a callback right here in the GetAwaiter method. Well, what we&amp;rsquo;ve done is localize the callbacks. The entire rest of my code can happily use &amp;ldquo;Await Storyboard&amp;rdquo; without ever using callbacks. It&amp;rsquo;s only this one small localized routine that encapsulates the callback.&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;Note: I&amp;rsquo;m doing this trick with &amp;ldquo;TaskCompletionSource&amp;rdquo; because I&amp;rsquo;m awaiting something that fires events. TaskCompletionSource is the way to turn an event-based API into a Task-based API. (It&amp;rsquo;s my experience that most things I want to await, if they aren&amp;rsquo;t already awaitable, are based around events.) If I were awaiting something easier, I might have been able to skip TaskCompletionSource entirely and instead just done this:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Extension&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;gt; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; GetAwaiter(sb &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; Animation.&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Storyboard&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;) &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;TaskAwaiter&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; t &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Task&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; = SomeInternalFunctionAsync(...)&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; awaiter &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; System.Runtime.CompilerServices.&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;TaskAwaiter&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; = t.GetAwaiter()&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Return&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; awaiter&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;Note: There doesn&amp;rsquo;t exist a plain non-generic &amp;ldquo;TaskCompletionSource&amp;rdquo; for creating a non-generic Task. The only thing you can do is use the generic &amp;ldquo;TaskCompletionSource(Of T)&amp;rdquo;, create a generic &amp;ldquo;Task(Of T)&amp;rdquo;, and then cast the result to the non-generic Task.&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;
&lt;h1&gt;What syntax to choose for awaiting?&lt;/h1&gt;
&lt;p class="MsoNormal"&gt;This article started with three possible syntaxes for awaiting:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Await&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; storyboard1&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Await&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; storyboard1.PlayAsync()&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Await&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; PlayStoryboardAsync(storyboard1)&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;It&amp;rsquo;s up to us as coders which syntax we&amp;rsquo;ll support. The first option looks slick, but I think it is poor style &amp;ndash; because it&amp;rsquo;s not clear what is being awaited. The next two options are equally fine.&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;strong&gt;&lt;em&gt;Lucian&amp;rsquo;s personal recommendation:&lt;/em&gt;&lt;/strong&gt; I think it is good style to await an &lt;em&gt;imperative verb&lt;/em&gt; (method) that ends in the suffix &amp;ldquo;Async&amp;rdquo;. It is good style to await a &lt;em&gt;noun&lt;/em&gt; (expression) so long as the type of that noun has the characteristics of a hot task, and not much more, and it&amp;rsquo;s obvious which completion event you&amp;rsquo;re thinking about.&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;Here are some examples of how I might chose to make something awaitable.&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;
&lt;table class="MsoTableGrid" style="border-collapse: collapse; border: none;" border="1" cellspacing="0" cellpadding="0"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style="width: 211.25pt; border: solid windowtext 1.0pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top" width="282"&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; iaa &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;IAsyncAction&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; = &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;...&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Await&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; iaa&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: green; background: white;"&gt;' good&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Await&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; iaa.StartAsync()&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: green; background: white;"&gt;' bad&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Await&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; iaa.AsTask()&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: green; background: white;"&gt;' &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: green;"&gt;good&lt;/span&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;td style="width: 256.25pt; border: solid windowtext 1.0pt; border-left: none; padding: 0in 5.4pt 0in 5.4pt;" valign="top" width="342"&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal;"&gt;Windows.Foundation.IAsyncAction is always given to you &amp;ldquo;hot&amp;rdquo; i.e. already running. Also it really is very much like a task, i.e. it starts then finishes then can&amp;rsquo;t be restarted. Its only additional behaviors are cancellation and progress, which are also task-like. Therefore &amp;ldquo;Await iaa&amp;rdquo; is easily understandable. The form &amp;ldquo;Await iaa.StartAsync()&amp;rdquo; is confusing because it has already started.&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style="width: 211.25pt; border: solid windowtext 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt;" valign="top" width="282"&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; timeout_ms = 150&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Await&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; timeout_ms&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: green; background: white;"&gt;' bad&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Await&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Task&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;.Delay(timeout_ms) &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: green; background: white;"&gt;' good&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;td style="width: 256.25pt; border-top: none; border-left: none; border-bottom: solid windowtext 1.0pt; border-right: solid windowtext 1.0pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top" width="342"&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal;"&gt;The first form is just confusing. There&amp;rsquo;s no obvious completion event for a &lt;em&gt;number!&lt;/em&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style="width: 211.25pt; border: solid windowtext 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt;" valign="top" width="282"&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; sb &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;StoryBoard&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; = &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;...&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Await&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; sb&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: green; background: white;"&gt;' bad&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Await&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; sb.PlayAsync()&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: green; background: white;"&gt;' good&lt;/span&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;td style="width: 256.25pt; border-top: none; border-left: none; border-bottom: solid windowtext 1.0pt; border-right: solid windowtext 1.0pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top" width="342"&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal;"&gt;Windows.UI.Xaml.Media.Animation.Storyboard feels like a very rich type, with far more properties and behaviors than just a Task-like thing, so it&amp;rsquo;s good to call out explicitly which of those behaviors we&amp;rsquo;re awaiting. And again, the second form makes it clear that we&amp;rsquo;re encapsulating a call to sb.Start().&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style="width: 211.25pt; border: solid windowtext 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt;" valign="top" width="282"&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; media &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;MediaElement&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; = &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;...&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Await&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; media&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: green; background: white;"&gt;' bad&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Await&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; media.PlayAsync()&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: green; background: white;"&gt;' good&lt;/span&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;td style="width: 256.25pt; border-top: none; border-left: none; border-bottom: solid windowtext 1.0pt; border-right: solid windowtext 1.0pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top" width="342"&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal;"&gt;Windows.UI.Xaml.Controls.MediaElement has a common well-understood verb, &amp;ldquo;Play&amp;rdquo;. When we make it awaitable, of course we should it clear that we&amp;rsquo;re awaiting completion of that verb.&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style="width: 211.25pt; border: solid windowtext 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt;" valign="top" width="282"&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; r &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Rectangle&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; = &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;...&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Await&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; r&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: green; background: white;"&gt;' bad&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Await&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; r.DragAsync()&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: green; background: white;"&gt;' bad&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;&amp;nbsp;&lt;br /&gt; Await&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; DragAsync(r)&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: green; background: white;"&gt;' good&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;td style="width: 256.25pt; border-top: none; border-left: none; border-bottom: solid windowtext 1.0pt; border-right: solid windowtext 1.0pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top" width="342"&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal;"&gt;Windows.UI.Xaml.Shapes.Rectangle. Here I want to await until a drag operation has finished. The first form is terrible because it&amp;rsquo;s not clear what&amp;rsquo;s being awaited. The second form is ugly because DragAsync() isn&amp;rsquo;t an inherent property of shapes, and should be an extension method on them.&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style="width: 211.25pt; border: solid windowtext 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt;" valign="top" width="282"&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Await&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; button1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: green; background: white;"&gt;' ?&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Await&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; button1.ClickAsync()&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: green; background: white;"&gt;' ?&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Await&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; ButtonClickAsync(button1) &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: green; background: white;"&gt;' ?&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Await&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; ButtonClick(button1)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: green; background: white;"&gt;' ?&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;td style="width: 256.25pt; border-top: none; border-left: none; border-bottom: solid windowtext 1.0pt; border-right: solid windowtext 1.0pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top" width="342"&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal;"&gt;What do you think of the first form? It&amp;rsquo;s fairly obvious that we&amp;rsquo;re waiting until the button gets clicked. The second form I think looks bad because it gives the impression we&amp;rsquo;re performing the click. I&amp;rsquo;m undecided between the third and fourth forms.&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style="width: 211.25pt; border: solid windowtext 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt;" valign="top" width="282"&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; proc &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Process &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;= &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;...&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Await&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; proc&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: green; background: white;"&gt;' ?&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Await&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; proc.RunAsync()&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: green; background: white;"&gt;' good&lt;/span&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;td style="width: 256.25pt; border-top: none; border-left: none; border-bottom: solid windowtext 1.0pt; border-right: solid windowtext 1.0pt; padding: 0in 5.4pt 0in 5.4pt;" valign="top" width="342"&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal;"&gt;System.Diagnostics.Process is often thought of like a task, i.e. something with a well-defined lifetime. So I wouldn&amp;rsquo;t mind awaiting it directly. However, users might easily forget to call Start() on it and so I prefer the second form.&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10372738" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/lucian/archive/tags/async/">async</category><category domain="http://blogs.msdn.com/b/lucian/archive/tags/win8/">win8</category></item><item><title>How to hibernate async methods (how to serialize Task)</title><link>http://blogs.msdn.com/b/lucian/archive/2012/11/24/how-to-hibernate-async-methods-how-to-serialize-task.aspx</link><pubDate>Sat, 24 Nov 2012 01:33:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10371233</guid><dc:creator>Lucian Wischik, MSFT</dc:creator><slash:comments>5</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/lucian/rsscomments.aspx?WeblogPostID=10371233</wfw:commentRss><comments>http://blogs.msdn.com/b/lucian/archive/2012/11/24/how-to-hibernate-async-methods-how-to-serialize-task.aspx#comments</comments><description>&lt;p&gt;Sometimes people ask the question &amp;ldquo;How can I serialize a Task?&amp;rdquo; If you try, it throws a SerializationException:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; t = TestAsync()&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Using&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; stream &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;New&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;MemoryStream&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; formatter &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;New&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; Formatters.Binary.&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;BinaryFormatter&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;formatter.Serialize(stream, t)&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: green; background: white;"&gt;&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp; ' SerializationException: Type 'Task' is not marked as serializable.&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Using&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Async&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; TestAsync() &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Task&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Await&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Task&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;.Delay(10)&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Console&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;.WriteLine(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white;"&gt;"a"&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;It&amp;rsquo;s the wrong question to ask. A Task is something that conceptually can&amp;rsquo;t be serialized. Consider as example this task: &amp;ldquo;Dim t As Task = httpClient.DownloadStringAsync(url)&amp;rdquo;. The task object embodies two things: (1) task is a promise that the operation will be completed at some time in the future, regardless of whether that operation is being performed locally in an async method or thread, or through some packets hurtling through the internet; (2) a task contains a collection of signed-up behaviors, delegates, that will be invoked once the task transitions to the &amp;ldquo;completed&amp;rdquo; state. It doesn&amp;rsquo;t make sense to talk about serializing either of those two things.&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;The right question is: &amp;ldquo;&lt;em&gt;How can I serialize the current state of my progress through this async method?&lt;/em&gt;&amp;rdquo; In this article I&amp;rsquo;ll show how.&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;
&lt;h1&gt;Caveat&lt;/h1&gt;
&lt;p class="MsoNormal"&gt;Personally, I don&amp;rsquo;t yet believe that this is a good idea. I&amp;rsquo;m posting this blog article in case I&amp;rsquo;m wrong, and in case anyone finds a good design pattern for it.&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;In the late 1990s I did my PhD on the topic of message-passing as a way to write concurrent programs. It was a bad idea: &amp;ldquo;await&amp;rdquo; is better than message-passing. At the time most of my peers were doing PhDs on mobile agents which, I think, are even worse idea. Mobile agents are pieces of code which can serialize their state to disk, be transmitted over the internet, be deserialized on a remote machine, and resume their execution there. Biztalk/XLang also has the notion of long-running interactive processes which can be suspended to disk and resumed later.&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;I don&amp;rsquo;t think it&amp;rsquo;s a good idea to serialize &lt;em&gt;processes/functions/behaviors/agents&lt;/em&gt;. That&amp;rsquo;s because they&amp;rsquo;re intangible. I think we should figure out the &lt;em&gt;plain-old-data&lt;/em&gt; embodiment of the current state of our work, and serialize that data to disk, and deserialize it again all as a passive data-structure rather than an active agent. That way, if someone asks &amp;ldquo;What is currently in flight?&amp;rdquo; then in answer we can point them to the data structures.&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;So please only follow this article if you have a real vision of where it will be practically useful and clean.&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;
&lt;h1&gt;Using a serializable async method&lt;/h1&gt;
&lt;p class="MsoNormal"&gt;Here&amp;rsquo;s my serializable async method &amp;ldquo;TestAsync&amp;rdquo;.&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Async&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; TestAsync(min &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Integer&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;, max &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Integer&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;) &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Task&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;For&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; i = min &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;To&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; max&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Console&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;.WriteLine(i)&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Await&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Task&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;.Delay(100)&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;If&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; i = 5 &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Then&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Await&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Hibernator&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;.Hibernate(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white;"&gt;"a.hib"&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;)&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Next&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;One of my programs is going to kick off the async method. But half way through, the async method will hibernate itself to disk and terminate abruptly with a &amp;ldquo;Serialized-to-disk&amp;rdquo; exception.&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Console&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;.WriteLine(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white;"&gt;"INITIATING..."&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;)&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue; font-family: Consolas; font-size: 9.5pt;"&gt;Try&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;&amp;nbsp; &amp;nbsp; Await&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; TestAsync(1, 10)&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Catch&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; ex &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;OperationCanceledException&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;&amp;nbsp; &amp;nbsp; Console&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;.WriteLine(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white;"&gt;"EX" &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;amp; ex.Message)&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: blue; background: white;"&gt;End&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: blue; background: white;"&gt;Try&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; background: black;"&gt;&lt;span style="font-family: Consolas; color: white;"&gt;INITIATING ASYNC METHOD...&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; background: black;"&gt;&lt;span style="font-family: Consolas; color: white;"&gt;1&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; background: black;"&gt;&lt;span style="font-family: Consolas; color: white;"&gt;2&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; background: black;"&gt;&lt;span style="font-family: Consolas; color: white;"&gt;3&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; background: black;"&gt;&lt;span style="font-family: Consolas; color: white;"&gt;4&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; background: black;"&gt;&lt;span style="font-family: Consolas; color: white;"&gt;5&lt;br /&gt;&lt;/span&gt;&lt;span style="color: white; font-family: Consolas;"&gt;EX Serialized to a.hib&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;My other program might be running on a different machine, or maybe just a long time later, or maybe it just needed the async method to be snapshotted to disk for resilience against errors. It&amp;rsquo;s going to resume that same async method, mid-flight, from disk.&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Console&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;.WriteLine(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white;"&gt;"RESUMING..."&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; t &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Task&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; = &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Hibernator&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;.Resume(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white;"&gt;"a.hib"&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Await&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; t&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-left: .5in;"&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: #2b91af; background: white;"&gt;Console&lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: black; background: white;"&gt;.WriteLine(&lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: #a31515; background: white;"&gt;"done"&lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: black; background: white;"&gt;)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; background: black;"&gt;&lt;span style="font-family: Consolas; color: white;"&gt;RESUMING...&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; background: black;"&gt;&lt;span style="font-family: Consolas; color: white;"&gt;6&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; background: black;"&gt;&lt;span style="font-family: Consolas; color: white;"&gt;7&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; background: black;"&gt;&lt;span style="font-family: Consolas; color: white;"&gt;8&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; background: black;"&gt;&lt;span style="font-family: Consolas; color: white;"&gt;9&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-top: 0in; margin-right: 0in; margin-bottom: .0001pt; margin-left: .5in; line-height: normal; background: black;"&gt;&lt;span style="font-family: Consolas; color: white;"&gt;10&lt;br /&gt;&lt;/span&gt;&lt;span style="color: white; font-family: Consolas;"&gt;done&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;em&gt;Q. What makes the async method serializable?&lt;/em&gt; A. All its parameters and local variables must themselves be serializable.&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;em&gt;Q. Why design it so that hibernation throws a SerializationException from the original method?&lt;/em&gt; A. Imagine you stepped into a Star Trek transporter to beam down to the planet&amp;rsquo;s surface. Would you want the original copy of you in the transporter bay to go on living, while the new copy on the planet&amp;rsquo;s surface goes on living as well? So there are now two of you? You&amp;rsquo;d have to be very careful of side-effects, in case the two copies tried to do the same thing at the same time.&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;em&gt;Q. Why design it so the async method itself is responsible for hibernating, rather than its caller?&lt;/em&gt; A. I think that hibernation is serious business, akin to thread-termination or task-cancellation, which should only be done &lt;em&gt;cooperatively&lt;/em&gt; when the async method is ready for it. I&amp;rsquo;d probably want to extend the above code to use HiberationTokenSource / HibernationToken, by analogy to CancellationTokenSource / CancellationToken.&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;
&lt;h1&gt;Implementing serialization of async methods&lt;/h1&gt;
&lt;p class="MsoNormal"&gt;Here&amp;rsquo;s my implementation of the Hibernate and Resume functionality. Code first; discussion afterwards.&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Serializable&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Public&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Class&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Hibernator&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; : &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Implements&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;INotifyCompletion&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;, &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;ISerializable&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Private&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; m_ex &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; Runtime.ExceptionServices.&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;ExceptionDispatchInfo&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Private&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; m_fn &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;As&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;String&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 8pt;"&gt;&amp;nbsp;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 8pt;"&gt;&amp;nbsp;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: green; background: white;"&gt;'===================================================================================&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 8pt;"&gt;&amp;nbsp;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Public&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Shared&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; Hibernate(fn &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;As&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;String&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;) &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;As&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Hibernator&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: green; background: white;"&gt;' Intended use: "Await Hibernator.Hibernate(filename)"&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Return&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;New&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Hibernator&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;With&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; {.m_fn = fn}&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;End&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 8pt;"&gt;&amp;nbsp;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Private&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;New&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;()&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: green; background: white;"&gt;' Private constructor used only by the Hibernate() shared method&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;End&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 8pt;"&gt;&amp;nbsp;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; GetAwaiter() &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;As&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Hibernator&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Return&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Me&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;End&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 8pt;"&gt;&amp;nbsp;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Public&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;ReadOnly&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Property&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; IsCompleted &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;As&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Boolean&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Get&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Return&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;False&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: green; background: white;"&gt;' so that OnCompleted gets called&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;End&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Get&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;End&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Property&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 8pt;"&gt;&amp;nbsp;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Public&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; OnCompleted(continuation &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;As&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Action&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;) &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Implements&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;INotifyCompletion&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;.OnCompleted&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; ex0 &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;As&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Exception&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; = &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Nothing&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Try&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; sm = continuation.Target.GetType().GetField(&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #a31515; background: white;"&gt;"m_stateMachine"&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;,&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;BindingFlags&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;.NonPublic &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Or&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;BindingFlags&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;.Instance).GetValue(continuation.Target)&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Using&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; stream &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;As&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;New&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;FileStream&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;(m_fn, &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;FileMode&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;.Create, &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;FileAccess&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;.Write)&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; formatter &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;As&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;New&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; Runtime.Serialization.Formatters.Binary.&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;BinaryFormatter&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 8pt;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; formatter.Serialize(stream, sm.GetType().Assembly.FullName)&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 8pt;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; formatter.Serialize(stream, sm.GetType().FullName)&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;For&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Each&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; field &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;In&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; sm.GetType().GetFields(&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;BindingFlags&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;.Public &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Or&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; BindingFlags&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;.NonPublic &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Or&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;BindingFlags&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;.Instance)&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; If&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; field.Name = &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #a31515; background: white;"&gt;"$Builder"&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;OrElse&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; field.Name = &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #a31515; background: white;"&gt;"&amp;lt;&amp;gt;t__builder"&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Then&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Continue For&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; fieldValue = field.GetValue(sm)&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;If&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; fieldValue &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Is&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Nothing&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Then&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Continue For&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;If&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; field.Name.Contains(&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #a31515; background: white;"&gt;"$awaiter"&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;) &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;AndAlso&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Not&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Object&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;.ReferenceEquals(fieldValue, &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Me&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;) &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Then&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Continue For&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 8pt;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; formatter.Serialize(stream, field.Name)&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 8pt;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; formatter.Serialize(stream, fieldValue)&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Next&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;End&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Using&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ex0 = &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;New&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;OperationCanceledException&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;(&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #a31515; background: white;"&gt;"Serialized to "&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; &amp;amp; m_fn)&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Catch&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; ex1 &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;As&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Exception&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;If&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;File&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;.Exists(m_fn) &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Then&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; IO.&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;File&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;.Delete(m_fn)&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 8pt;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ex0 = ex1&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;End&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Try&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; m_ex = Runtime.ExceptionServices.&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;ExceptionDispatchInfo&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;.Capture(ex0)&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: green; background: white;"&gt;' That's so we can save+rethrow the exception with its intended callstack&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 8pt;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 8pt;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; continuation()&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;End&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-family: Consolas; font-size: 8pt;"&gt;&amp;nbsp;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Public&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; GetResult()&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;If&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Not&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; m_ex &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Is&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Nothing&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Then&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; m_ex.Throw()&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;End&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;br /&gt;&lt;span style="color: black; font-size: 8pt;"&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 8pt;"&gt;&amp;nbsp;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: green; background: white;"&gt;'===================================================================================&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 8pt;"&gt;&amp;nbsp;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Public&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Shared&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; [Resume](fn &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;As&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;String&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;) &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;As&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Task&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Using&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; stream &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;As&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;New&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; IO.&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;FileStream&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;(fn, &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;FileMode&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;.Open, &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;FileAccess&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;.Read)&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; formatter &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;As&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;New&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; Runtime.Serialization.Formatters.Binary.&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;BinaryFormatter&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; assemblyName = &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;CStr&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;(formatter.Deserialize(stream))&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; typeName = &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;CStr&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;(formatter.Deserialize(stream))&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; sm = &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;TryCast&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;(System.&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Activator&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;.CreateInstance(assemblyName, typeName).Unwrap(),&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;IAsyncStateMachine&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;)&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 8pt;"&gt;&amp;nbsp;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;While&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; stream.Position &amp;lt; stream.Length&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; fieldName = &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;CStr&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;(formatter.Deserialize(stream))&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; fieldValue = formatter.Deserialize(stream)&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; field = sm.GetType().GetField(fieldName, &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;BindingFlags&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;.Public &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Or&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;BindingFlags&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;.NonPublic &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Or&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;BindingFlags&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;.Instance)&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 8pt;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; field.SetValue(sm, fieldValue)&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;End&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;While&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 8pt;"&gt;&amp;nbsp;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; builder = &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;AsyncTaskMethodBuilder&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;.Create()&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; builderField = sm.GetType().GetField(&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #a31515; background: white;"&gt;"$Builder"&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;, &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;BindingFlags&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;.Public &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Or&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;BindingFlags&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;.NonPublic &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Or&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;BindingFlags&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;.Instance)&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;If&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; builderField &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Is&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Nothing&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Then&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; builderField =&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; sm.GetType().GetField(&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #a31515; background: white;"&gt;"&amp;lt;&amp;gt;t__builder"&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;,&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;BindingFlags&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;.Public &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Or&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; BindingFlags&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;.NonPublic &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Or&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;BindingFlags&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;.Instance)&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 8pt;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; builderField.SetValue(sm, builder)&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; builder.Start(sm) &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: green; background: white;"&gt;' This invokes MoveNext()&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; builder2 = &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;CType&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;(builderField.GetValue(sm), &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;AsyncTaskMethodBuilder&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;)&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Return&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; builder2.Task&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;End&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Using&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;End&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 8pt;"&gt;&amp;nbsp;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 8pt;"&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: green; background: white;"&gt;'===================================================================================&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 8pt;"&gt;&amp;nbsp;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Private&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;New&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;(info &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;As&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;SerializationInfo&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;, ctx &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;As&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;StreamingContext&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;)&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: green; background: white;"&gt;' Private constructor used only by the deserializer&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;End&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Public&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; GetObjectData(info &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;As&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;SerializationInfo&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;,&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;context &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;As&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;StreamingContext&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;) &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Implements&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;ISerializable&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;.GetObjectData&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: green; background: white;"&gt;' we have nothing to serialize&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;End&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 8pt;"&gt;&amp;nbsp;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;End&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Class&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;NOTE 1:&amp;nbsp;What fields need be serialized? Well, all of them, except for...&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The builder field won't be serialized. It must be reconstructed. In VB it's called $Builder. In C# it's called &amp;lt;&amp;gt;t__builder. Note that both names are unutterable in user code.&lt;/li&gt;
&lt;li&gt;Most awaiters don't need to be serialized; only the current one is needed. The VB compiler calls its awaiters $awaiterXYZ The C# compiler calls its &amp;lt;&amp;gt;XYZ$awaiterABC. Again, both names are unutterable in user code. Both compilers creates several awaiters: one awaiter of type 'object' for all reference-type awaitables, and one awaiter for each type of value-type.&lt;/li&gt;
&lt;li&gt;Null fields don't need to be serialized, and indeed can't.&lt;/li&gt;
&lt;/ul&gt;
&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;NOTE 2:&amp;nbsp;Normally it's wrong for OnCompleted to invoke continuation() directly, because you end up with MoveNext invoking OnCompleted invoking MoveNext, which if it went too far would blow the stack. We should really be posting a delegate to the current synchronization context. But here the intended usage is "Await Hibernator.Hibernate(filename)" which will terminate the async method with an uncaught exception, so there's no danger.&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;NOTE 3: In this design of hibernator,&amp;nbsp;GetResult will be called at two times:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;when the user did "Await Hibernator.Hibernate()", and our OnCompleted method saved an exception into m_ex, and we always wish to throw that exception&lt;/li&gt;
&lt;li&gt;when the user resumed the aysnc method from hibernation, and the Hibernator was deserialized with all fields null, and we don't want to throw any exception.&lt;/li&gt;
&lt;/ol&gt;
&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;NOTE 4: Why does the [Resume] method retrieve the "builder" field a second time? Well, Async state machines are structures that get boxed at their first await. If they get boxed, then all structure-fields within them get boxed as well. Therefore "sm.builder" after the first MoveNext might not be the same "builder" that we created before.&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;
&lt;h1 class="MsoNormal"&gt;Conclusions&lt;/h1&gt;
&lt;p class="MsoNormal"&gt;This is a pretty sketchy implementation. It does just enough to make the sample code work. It doesn&amp;rsquo;t work with Async Subs or Async Task(Of T) Functions.&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;It also isn't &lt;em&gt;composable&lt;/em&gt;: you can't write a wrapper method around a call to Hibernate(). That's because it only serializes its immediate caller.&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;This code is tightly coupled to the .NET4.5 compilers&amp;rsquo; internal implementations of async methods. It relies on the fact that &amp;ldquo;Await Hibernate()&amp;rdquo; will invoke Hibernate&amp;rsquo;s OnCompleted method with a delegate whose Target object has an m_stateMachine field that points to the state machine. It relies on this state-machine having lifted local variables, and a builder of a recognizable name, and recognizable awaiter fields.&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;We didn&amp;rsquo;t give these things a clean public API, because we didn&amp;rsquo;t have any good use-cases for how or why people would want to serialize their async methods. If you folks come up with compelling mainstream scenarios, then we might consider maybe a Microsoft-authored NuGet package which does serialization, or exposing the necessary fields via a clean public API.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10371233" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/lucian/archive/tags/async/">async</category></item><item><title>Why must async methods return Task?</title><link>http://blogs.msdn.com/b/lucian/archive/2012/11/22/why-must-async-methods-return-task.aspx</link><pubDate>Thu, 22 Nov 2012 01:26:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10370785</guid><dc:creator>Lucian Wischik, MSFT</dc:creator><slash:comments>6</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/lucian/rsscomments.aspx?WeblogPostID=10370785</wfw:commentRss><comments>http://blogs.msdn.com/b/lucian/archive/2012/11/22/why-must-async-methods-return-task.aspx#comments</comments><description>&lt;p class="MsoNormal"&gt;We all know that async methods return Task or Task(Of T):&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Async &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; GetNameAsync() &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Task&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Of &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;String&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;)&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Await &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Task&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;.Delay(10)&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Return &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white;"&gt;"ernest"&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;br /&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;Sometimes, advanced users ask for the ability to return different types out of an async method. That&amp;rsquo;s disallowed: it gives the error message &amp;ldquo;&lt;strong&gt;The Async modifier can only be used on Subs, or on Functions that return Task or Task(Of T)&lt;/strong&gt;&amp;rdquo;.&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Async &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Function &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: red;"&gt;GetNameAsync&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;() &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;ITask&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Of &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;String&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;)&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Await &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Task&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;.Delay(10)&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Return &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white;"&gt;"ernest"&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: blue; background: white;"&gt;End&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;br /&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;In this post I&amp;rsquo;ll explain why it&amp;rsquo;s disallowed.&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;It wasn&amp;rsquo;t always disallowed. Our first internal prototypes of the async feature allowed you to return any suitable type from an async method, and I&amp;rsquo;ll explain how. We did some powerful things with it. But in the end there were solid language-design reasons for disallowing it, and I&amp;rsquo;ll explain those.&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;
&lt;h1&gt;Motives for flexible async return types&lt;/h1&gt;
&lt;p&gt;Why would you ever want to return something other than Task(Of T) from an async method? Here are some (very legitimate) reasons.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;em&gt;The Task type is a reference type, which means I have to pay the cost of at least one heap allocation from every async method. I want to return a structure type to avoid that cost, and reduce the need for garbage collection.&lt;/em&gt;&lt;br /&gt; Let&amp;rsquo;s call such a putative return type &amp;ldquo;TaskLite(Of T)&amp;rdquo;&lt;br /&gt;&amp;nbsp;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;I want to return IAsyncOperation(Of T) directly from my async methods.&lt;br /&gt; &lt;/em&gt;This request comes from people writing WinRT components. It&amp;rsquo;s a special case of the next request...&lt;br /&gt;&amp;nbsp;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;My library/framework/app uses its own different Task-like type because Task wasn&amp;rsquo;t suitable for whatever reason. I want to be able to return my own Task-like type from async methods.&lt;br /&gt;&amp;nbsp;&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;I want an interface type ITask(Of T) instead of Task(Of T). That would be a better object-oriented design. ITask(Of T) wouldn&amp;rsquo;t have any of the blocking synchronous members like Wait() or Result. It would also allow me to provide my own implementations of ITask(Of T).&amp;nbsp;&lt;br /&gt; &lt;/em&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;h1&gt;Early prototype of async had flexible return types&lt;/h1&gt;
&lt;p class="MsoNormal"&gt;Our first internal prototype of async allowed for arbitrary return types. Here&amp;rsquo;s how it worked:&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Async &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; GetNameAsync() &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Taskoid&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Await &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Task&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;.Delay(10)&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Return &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white;"&gt;"ernest"&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;(1) The compiler needs to know, from the return type &amp;ldquo;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: #2b91af; background: white;"&gt;Taskoid&lt;/span&gt;&amp;rdquo;, how to get a &lt;em&gt;builder&lt;/em&gt; for it. We wanted to allow extension methods, so we did it with a &amp;ldquo;fake static method&amp;rdquo; like this:&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: blue; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Dim&lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: black; background: white;"&gt; builder = &lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: blue; background: white;"&gt;CType&lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: black; background: white;"&gt;(&lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: blue; background: white;"&gt;Nothing&lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: black; background: white;"&gt;, &lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: #2b91af; background: white;"&gt;Taskoid&lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: black; background: white;"&gt;).GetBuilder()&lt;br /&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;(2) The compiler turns each async method into a compiler-generated stub, plus a compiler-generated state-machine class. Here&amp;rsquo;s the stub. Observe that it&amp;rsquo;s the builder who&amp;rsquo;s completely responsible for figuring out how to produce the ultimate task-like thing that&amp;rsquo;s returned from the method:&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; GetNameAsync() &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Taskoid&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; sm &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;New &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;GetNameAsync$SM&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sm.builder = &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;CType&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Nothing&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;, &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Taskoid&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;).GetBuilder()&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Return&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; sm.builder.Build(sm)&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;br /&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;(3) The state machine has a MoveNext method that embodies the user&amp;rsquo;s original async method body. The way it implements &amp;ldquo;Await&amp;rdquo; or &amp;ldquo;Return&amp;rdquo; is simply by asking the builder to handle them. Once again, it&amp;rsquo;s the builder who&amp;rsquo;s completely responsible for figuring out how the flow of execution around these operators:&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Public &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; MoveNext() &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Implements &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;IAsyncStateMachine&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;.MoveNext&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Try&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Select&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Case&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; iState&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Case&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; 0&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: green; background: white;"&gt;' await Task.Delay(10)&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Awaiter1 = &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Task&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;.Delay(10).GetAwaiter()&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; iState = 1 : builder.Await(Awaiter1) : &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Return&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Case&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; 1&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: green; background: white;"&gt;' return "ernest"&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; iState = 2 : builder.Return(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white;"&gt;"ernest"&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;) : &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Return&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Select&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Catch&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; ex &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Exception&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; builder.Throw(ex)&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Try&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;br /&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;(4) Actually, we also asked the builder to handle the &amp;ldquo;Yield&amp;rdquo; statement as well. I&amp;rsquo;m not going to write out the details of how we implemented various different builders &amp;ndash; they consist of small clever tricks within a sea of boilerplate code. What was exciting was all the powerful things we could do with them:&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: green; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ' Iterators&lt;br /&gt; &lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: blue; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Async &lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: black; background: white;"&gt; GetNodes() &lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: blue; background: white;"&gt;As &lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: #2b91af; background: white;"&gt;IEnumerable&lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: black; background: white;"&gt;(&lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: blue; background: white;"&gt;Of &lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: blue; background: white;"&gt;Integer&lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: black; background: white;"&gt;)&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: blue; background: white;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: green; background: white;"&gt;' Async iterators&lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: blue; background: white;"&gt; &lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp; Async &lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: black; background: white;"&gt; GetNodesAsync() &lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: blue; background: white;"&gt;As &lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: #2b91af; background: white;"&gt;IAsyncEnumerable&lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: black; background: white;"&gt;(&lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: blue; background: white;"&gt;Of &lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: blue; background: white;"&gt;Integer&lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: black; background: white;"&gt;)&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: green; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ' WinRT operations, using Yield to report progress&lt;br /&gt; &lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: blue; background: white;"&gt;Async &lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: black; background: white;"&gt; GetNodesAsync() &lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: blue; background: white;"&gt;As &lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: #2b91af; background: white;"&gt;IAsyncOperationWithProgress&lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: black; background: white;"&gt;(&lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: blue; background: white;"&gt;Of &lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: blue; background: white;"&gt;String&lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: black; background: white;"&gt;, &lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: blue; background: white;"&gt;Integer&lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: black; background: white;"&gt;)&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: green; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ' RX, using multiple Returns to produce events&lt;br /&gt; &lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: blue; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Async &lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: black; background: white;"&gt; WatchEvents() &lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: blue; background: white;"&gt;As &lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: #2b91af; background: white;"&gt;IObservable&lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: black; background: white;"&gt;(&lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: blue; background: white;"&gt;Of &lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: blue; background: white;"&gt;String&lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: black; background: white;"&gt;)&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: green; background: white;"&gt;&amp;nbsp;&amp;nbsp; ' Async methods that implicitly start on a parallel thread&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: blue; background: white;"&gt;Async Function&lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: black; background: white;"&gt; ThreadpoolWorkAsync() &lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: blue; background: white;"&gt;As &lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: #2b91af; background: white;"&gt;ParallelTask&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;h1&gt;Why it didn&amp;rsquo;t work: type inference&lt;/h1&gt;
&lt;p class="MsoNormal"&gt;The early prototype of async allowed flexible builders, but it didn&amp;rsquo;t support async lambdas nor generic type inference. When we started to add lambdas and type inference, we discovered it was incompatible with the flexible builders.&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;In the choice between &amp;ldquo;generics + lambdas + inferences&amp;rdquo; versus &amp;ldquo;flexible return types&amp;rdquo;, we picked the first, and it was definitely the right choice.&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;Why am I so adamant that it was the right choice? Well, think of things like Task.Run or Task.WhenAll. We pass async lambdas around all over the place. They&amp;rsquo;re essential. They&amp;rsquo;re far more common than flexible return types would have been.&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;In what way are the two options incompatible? Well, let&amp;rsquo;s start with a simple example:&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: blue; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Sub&lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: black; background: white;"&gt; f(&lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: blue; background: white;"&gt;Of &lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: #2b91af; background: white;"&gt;T&lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: black; background: white;"&gt;)(lambda &lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: blue; background: white;"&gt;As &lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: #2b91af; background: white;"&gt;Func&lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: black; background: white;"&gt;(&lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: blue; background: white;"&gt;Of &lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: #2b91af; background: white;"&gt;IAsyncOperation&lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: black; background: white;"&gt;(&lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: blue; background: white;"&gt;Of &lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: #2b91af; background: white;"&gt;T&lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: black; background: white;"&gt;)))&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; f(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Async&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;()&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Return&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; 5&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: blue; background: white;"&gt;End&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: black; background: white;"&gt;)&lt;br /&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;Here you&amp;rsquo;d expect it to have picked T=Integer, and to have figured out that it should have called &amp;ldquo;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: #2b91af; background: white;"&gt;IAsyncOperation&lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: black; background: white;"&gt;(&lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: blue; background: white;"&gt;Of Integer&lt;/span&gt;&lt;span style="font-size: 9.5pt; line-height: 107%; font-family: Consolas; color: black; background: white;"&gt;).GetBuilder()&lt;/span&gt;&amp;rdquo;. How would it have done that? Let&amp;rsquo;s spell out the compiler&amp;rsquo;s thoughts explicitly:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;div class="MsoNormal"&gt;There is a return statement with operand &amp;ldquo;5&amp;rdquo;&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div class="MsoNormal"&gt;Therefore, the call to builder.SetResult(5) must compile&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div class="MsoNormal"&gt;&amp;lt;magic&amp;gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div class="MsoNormal"&gt;Therefore, the builder must have had type IAsyncOperationBuilder(Of Integer)&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div class="MsoNormal"&gt;&amp;lt;magic&amp;gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div class="MsoNormal"&gt;Therefore, the stub method must have invoked IAsyncOperation(Of Integer).GetBuilder()&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div class="MsoNormal"&gt;Therefore the lambda must have had type Func(Of IAsyncOperation(Of Integer))&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div class="MsoNormal"&gt;Therefore, T=Integer&lt;/div&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p class="MsoNormal"&gt;Here&amp;rsquo;s a more difficult example.&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; text-autospace: none;"&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; h(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Of &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;T&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;)(lambda &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;As &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Func&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Of &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Unusual&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Of &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;T&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;)))&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Class &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Unusual&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Of &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;T&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;) : &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Implements &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;IAsyncOperation&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Of &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;IEnumerable&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Of &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white;"&gt;T&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;))&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; h(&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Async&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;()&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Return&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt; 5&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;End&amp;nbsp;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white;"&gt;)&lt;br /&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;I don&amp;rsquo;t know what I&amp;rsquo;d expect it to pick for T in this case. In general it would depend entirely on the vagaries of overload resolution, i.e. what overloads of builder.Return() and builder.Yield() there happen to be, and which ones happen to work with which arguments. It&amp;rsquo;s entirely possible to have builders where &amp;ldquo;T&amp;rdquo; can&amp;rsquo;t be inferred at all just from those calls to Return/Yield.&lt;/p&gt;
&lt;p class="MsoNormal"&gt;There are only a few possible solutions for the &amp;ldquo;magic&amp;rdquo; steps:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;div class="MsoNormal"&gt;We could enforce a convention/constraint: every return type from an async method must be a generic type with exactly one generic type parameter, and the corresponding builder&amp;rsquo;s Return() method must not be overloaded, and it must take exactly one parameter, and the parameter&amp;rsquo;s type must be the generic type parameter. &lt;em&gt;Verdict: this wouldn&amp;rsquo;t generalize to Yield, and it wouldn&amp;rsquo;t work with lots of interesting return types, and this kind of complex and arbitrary restriction is unprecedented and ugly&lt;/em&gt;.&lt;br /&gt;&amp;nbsp;&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div class="MsoNormal"&gt;We could add an arbitrary &amp;ldquo;constraint-solving&amp;rdquo; component to VB and C#. Whenever it encounters a type inference situation, it assembles all the knowledge it has about all overloads and return statements and yield statements, and plugs them into the solver, and sees what answers pop out. &lt;em&gt;Verdict: the behavior of the compiler would now be unpredictable to everyone. It&amp;rsquo;s true that constraint-solvers do exist in other languages, e.g. F# uses the Hindley-Milner solver for type inference. But there&amp;rsquo;s no precedent for the complexity of the constraint-solver we&amp;rsquo;d need here.&lt;br /&gt;&amp;nbsp;&lt;/em&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div class="MsoNormal"&gt;We could decide that flexible return types are allowed only for top-level methods or for lambas where their return-types are explicitly declared. &lt;em&gt;Verdict: this would be ugly. If the language has a feature, you should be able to combine it with other features. If we consider how we use lambdas today, we use type inference in almost all of them.&lt;/em&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p class="MsoNormal" style="margin-left: .25in;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;h1&gt;Workarounds for the lack of flexible types&lt;/h1&gt;
&lt;p class="MsoNormal"&gt;I started this article by outlining some of the scenarios where people want flexible return types. Now that we know they&amp;rsquo;re impossible, let&amp;rsquo;s consider the workarounds.&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;strong&gt;&amp;nbsp;&lt;/strong&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;strong&gt;SCENARIO 1. I want to return a value-type from my async methods, to avoid the cost of a heap-allocated reference type. Even if the data is available immediately and I return Task.FromResult(5), say, that still incurs the cost of allocating Task on the heap.&lt;/strong&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;If we&amp;rsquo;d anticipated the &amp;ldquo;async&amp;rdquo; feature five years ago we might have made the &amp;ldquo;Task&amp;rdquo; type a structure to start with. But it&amp;rsquo;s too late now. The best you can do is make your own Task-like type that&amp;rsquo;s a structure, and return it from a wrapper method. Here below is an example.&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; GetNameLiteAsync() &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;As &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;TaskLite&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;(&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Of &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;String&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;)&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;If &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Not&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; m_cachedName &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Is &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Nothing &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Then &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Return &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;New &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;TaskLite&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;(&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Of &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;String&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;)(m_cachedName)&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Return &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;New &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;TaskLite&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;(&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Of &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;String&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;)(GetNameInternalAsync())&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;End &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Structure &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;TaskLite&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;(&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Of &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;T&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;) : &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Implements&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; Runtime.CompilerServices.&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;INotifyCompletion&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Private&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; m_SyncValue &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;As &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;T&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Private&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; m_AsyncValue &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;As &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Task&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;(&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Of &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;T&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;)&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Public &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Sub &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;New&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;(Value &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;As &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;T&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;)&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; m_SyncValue = Value&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;End &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Public &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Sub &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;New&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;(AsyncValue &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;As &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Task&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;(&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Of &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;T&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;))&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; m_AsyncValue = AsyncValue&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;End &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Public &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; GetAwaiter() &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;As &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;TaskLite&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;(&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Of &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;T&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;)&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Return &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Me&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;End &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Public &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;ReadOnly &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Property&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; IsCompleted &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;As &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Boolean&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Get&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Return&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; (m_AsyncValue &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Is &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Nothing&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;) &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;OrElse&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; (m_AsyncValue.IsCompleted)&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;End &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Get&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;End &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Property&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Public &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; OnCompleted(continuation &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;As &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Action&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;) &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Implements &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;INotifyCompletion&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;.OnCompleted&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; m_AsyncValue.GetAwaiter().OnCompleted(continuation)&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;End &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Sub&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Public &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; GetResult() &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;As &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;T&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;If&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; m_AsyncValue &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Is &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Nothing &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Then &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Return&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; m_SyncValue&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Return&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; m_AsyncValue.GetAwaiter().GetResult()&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;End &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;End &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Structure&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt; &lt;/span&gt;We might have created TaskLite(Of T) ourselves in the .NET framework, and added it into the compiler, and baked in support for async methods to return either Task(Of T) or TaskLite(Of T). That would have worked, but would have been ugly framework design: every single user would have to worry all of the time about whether to return TaskLite or Task, when in reality it&amp;rsquo;s not even worth worrying about for the majority of users.&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;strong&gt;SCENARIOS 2 and 3: I want my async method to return IAsyncOperation(Of T), or some other task-like type, because my existing framework is built around my own task-like type.&lt;/strong&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;The Task type is &lt;em&gt;pluripotent&lt;/em&gt;. You can build any other task-like thing out of it. For instance we provide an extension method to turn a Task(Of T) into an IAsyncOperation(Of T). That&amp;rsquo;s how it is in general: whenever you want to return a different task-like thing, you have to do it via a wrapper method.&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; GetNameRTAsync() &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;As&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; Windows.Foundation.&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;IAsyncOperation&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;(&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Of &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;String&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;)&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; t &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;As &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Task&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;(&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Of &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;String&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;) = GetNameInternalAsync()&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Return&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; System.&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;WindowsRuntimeSystemExtensions&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;.AsAsyncOperation(t)&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;End &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt; &lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;strong&gt;&amp;nbsp;&lt;/strong&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;strong&gt;SCENARIO 4: I want my async method to return an ITask(Of T).&lt;/strong&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;This suggested scenario is a positively bad idea. We already as of .NET4 had a single canonical Task type. All the combinators like Task.WhenAll and Task.WhenAny operate on it, as do the Reactive Extensions, and Dataflow, and other libraries.&lt;/p&gt;
&lt;p class="MsoNormal"&gt;If we introduced ITask(Of T) as well, then suddenly all those libraries would become useless. They&amp;rsquo;d need to be rewritten to take ITask arguments rather than Task arguments. And for every async method or library-routine that you write, you&amp;rsquo;d have to decide whether it should return Task or ITask. That&amp;rsquo;s a detail that&amp;rsquo;s not worth worrying about for most programmers, and shouldn&amp;rsquo;t be forced upon them.&lt;/p&gt;
&lt;p class="MsoNormal"&gt;Also, if the compiler compiles an async method whose declared return type was ITask(Of T), it would &lt;em&gt;still&lt;/em&gt; have to pick a concrete implementation for that return type. (Likewise, when the compiler compiles an iterator method with return type IEnumerable(Of T), it needs to pick a concrete implementation of it). So allowing an async return method to return ITask(Of T) would never change the underlying fact that it returns an object whose runtime type is Task(Of T).&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; GetNameIAsync() &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;As &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;ITask&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;(&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Of&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;String&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;)&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Dim&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt; t &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;As &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;Task&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;(&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Of &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;String&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;) = GetNameInternalAsync()&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Return &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;New &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: #2b91af; background: white;"&gt;TaskWrapper&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;(&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Of &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;String&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;)(t)&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;End &lt;/span&gt;&lt;span style="font-size: 8.0pt; font-family: Consolas; color: blue; background: white;"&gt;Function&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white;"&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt; &lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;It was fascinating to work together with the other members of the VB and C# language design teams, to design the async language feature. We didn&amp;rsquo;t make any decisions lightly. The decision in this article, about flexible return types, took months of discussion and prototypes until we reached an answer. I&amp;rsquo;m confident we made the right decision in this case. Next month I&amp;rsquo;ll discuss why we allowed void-returning async methods. It was altogether more controversial...&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10370785" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/lucian/archive/tags/async/">async</category></item><item><title>Async Targeting Pack</title><link>http://blogs.msdn.com/b/lucian/archive/2012/04/24/async-targeting-pack.aspx</link><pubDate>Tue, 24 Apr 2012 18:16:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10297287</guid><dc:creator>Lucian Wischik, MSFT</dc:creator><slash:comments>14</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/lucian/rsscomments.aspx?WeblogPostID=10297287</wfw:commentRss><comments>http://blogs.msdn.com/b/lucian/archive/2012/04/24/async-targeting-pack.aspx#comments</comments><description>&lt;p&gt;&lt;strong&gt;[UPDATE: The async targeting pack has been renamed "&lt;a href="http://nuget.org/packages/Microsoft.Bcl.Async"&gt;Microsoft.Bcl.Async&lt;/a&gt;". It now supports async for portable libraries]&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;We&amp;rsquo;ve just released an "&lt;span style="text-decoration: line-through;"&gt;Async Targeting Pack&lt;/span&gt;&amp;rdquo; [update: replaced by &lt;a href="http://nuget.org/packages/Microsoft.Bcl.Async"&gt;Microsoft.Bcl.Async&lt;/a&gt;] via NuGet. It lets you use the async feature&amp;nbsp;of VS11 for applications that target Silverlight5 or .NET4. (You don't need it if you're using VS11 to target .NET45).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;We expect all users of VS2010+AsyncCTP to upgrade to VS11&lt;/strong&gt;. That&amp;rsquo;s because VS11 implements the async feature fully, and is supported. By contrast the AsyncCTP has many known bugs in its implementation of async, is not supported, and is difficult to install.&lt;/p&gt;
&lt;p&gt;We intend not to release any further versions of the AsyncCTP. &lt;span style="text-decoration: line-through;"&gt;For&amp;nbsp;the sake of &lt;a href="http://www.windowsazure.com/en-us/develop/vs11/"&gt;Azure&lt;/a&gt; and &lt;a href="http://windowsteamblog.com/windows_phone/b/wpdev/archive/2012/03/05/windows-8-and-the-windows-phone-sdk.aspx"&gt;Windows Phone&lt;/a&gt;, however, we are&amp;nbsp;keeping the AsyncCTP for VS2010 online for now [&lt;a href="http://www.microsoft.com/download/en/details.aspx?displaylang=en&amp;amp;id=9983"&gt;link&lt;/a&gt;]. It remains unsupported&lt;/span&gt;. Update: the new Microsoft.Bcl.Async works fine for Windows Phone.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I want to put in a note of appreciation to David Grunwald, Omer Mor and Alex Davies who released &lt;a href="http://omermor.github.com/AsyncBridge/"&gt;AsyncBridge&lt;/a&gt;&amp;nbsp;while we were putting together the&amp;nbsp;Async Targeting Pack. Great work!&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Some&amp;nbsp;reminiscences of the Async CTP&lt;/h2&gt;
&lt;p&gt;Now that the Async CTP can (almost) finally be laid to rest, here's a logo from it:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06/5468.await_2D00_logo.png"&gt;&lt;img src="http://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06/5468.await_2D00_logo.png" alt="" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Mads Torgersen has two whiteboards in his office that hang from a picture-rail. Right at the very beginning of our first discussions on&amp;nbsp;async, he wrote the word "await" in giant filled-in lettering on the boards and pushed the two halves apart to opposite ends of the wall. I used that to create the logo.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;My favorite real-life Async anecdote comes from covert observations of Anders Heijlsberg in the Microsoft cafeteria checkout line. He ordereds burritos more days than not... (1) He walks to the burrito counter, makes an order, and gets a receipt. (2) He queues up at the cash register to pay for the receipt. (3) Then he goes back to the burrito folks who have by now finished preparing his meal. If that's not async in action, I don't know what is!&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: courier new,courier;"&gt;&lt;span style="color: #0000ff;"&gt;Dim&lt;/span&gt; receipt &lt;span style="color: #0000ff;"&gt;As&lt;/span&gt; &lt;span style="color: #33cccc;"&gt;Task&lt;/span&gt;(&lt;span style="color: #0000ff;"&gt;Of&lt;/span&gt; &lt;span style="color: #33cccc;"&gt;Burrito&lt;/span&gt;) = OrderBurritoAsync()&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new,courier;"&gt;Pay()&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: courier new,courier;"&gt;&lt;span style="color: #0000ff;"&gt;Await&lt;/span&gt; receipt&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;On another occasion, Mads was giving a talk about async to&amp;nbsp;some group&amp;nbsp;within&amp;nbsp;Microsoft. He'd missed breakfast and was eating a banana at the podium, waving it about animatedly as he discussed async control flow. We called it his "async banana". It's an old&amp;nbsp;Microsoft tradition to share chocolates, cakes and cookies with colleagues, so that evening I baked some "Async banana bread" and brought it into the office the next day. First workitem was a bug report from our QA team that Finally bodies were sometimes being executed twice inside async methods. So that evening I baked a second identical "Async banana bread" and brought it into the office as a dupe. (The bugfix on this occasion was so subtle that it left even Jon Skeet&lt;a href="http://stackoverflow.com/questions/5027999/c-sharp-5-async-ctp-why-is-internal-state-set-to-0-in-generated-code-before-e"&gt; scratching his head for weeks&lt;/a&gt;!)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;We just got word that the AsyncCTP has been downloaded over 50,000 times. That's a huge number of downloads for an experimental preview of a language feature. We're hugely glad to have had people use it, and give their feedback, and shape the final product. But it was just an experimental technology preview, and we're&amp;nbsp;glad to finally have&amp;nbsp;VS11 and the&amp;nbsp;Async Targeting Pack as its replacement.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10297287" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/lucian/archive/tags/async/">async</category></item><item><title>Talk: Async Part 1 - the message-loop, and the Task type</title><link>http://blogs.msdn.com/b/lucian/archive/2012/03/29/talk-async-part-1-the-message-loop-and-the-task-type.aspx</link><pubDate>Thu, 29 Mar 2012 00:19:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10288734</guid><dc:creator>Lucian Wischik, MSFT</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/lucian/rsscomments.aspx?WeblogPostID=10288734</wfw:commentRss><comments>http://blogs.msdn.com/b/lucian/archive/2012/03/29/talk-async-part-1-the-message-loop-and-the-task-type.aspx#comments</comments><description>&lt;h1&gt;Talk: Async Part 1 - the message-loop, and the Task type&lt;/h1&gt;
&lt;p&gt;After giving lots of training and talks over the past two years, this is my most recent take on how to explain Async.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;em&gt;The message-loop is fundamental. You can't understand async unless you understand the message-loop. Once you understand how async uses the message-loop, you'll understand everything else.&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;You can use async to write the sort of distributed algorithms - with cancellation and retries - that the new generation of software needs, but that were too hard to write before.&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;The Task type and its various library functions like Task.WhenAll are compositional: they let you build your async algorithms out of smaller pieces.&lt;/em&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;This talk is called "Async Part 1". I've also given a sequel, &lt;a href="http://blogs.msdn.com/b/lucian/archive/2013/02/17/talk-async-part-2-for-architects-under-the-hood.aspx"&gt;Async Part 2&lt;/a&gt;, that goes into more advanced uses of async.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;I gave this talk in C# at NDC2012 in Oslo, Norway on 2012.06.06&lt;/strong&gt;&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="http://ndc2012.oktaset.com/t-4925"&gt;Watch video&lt;/a&gt; online&lt;/li&gt;
&lt;li&gt;Download &lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkAsync2012/3005.Async1-introduction-_5B00_slides_5D00_-_2D00_-Lucian-Wischik-_2D00_-NDC2012-Oslo-2012.06.08.pptx"&gt;slides.pptx&lt;/a&gt; [2.7mb]&lt;/li&gt;
&lt;li&gt;Download &lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkAsync2012/1185.Async1-introduction-_5B00_script_5D00_-_2D00_-Lucian-Wischik-_2D00_-NDC2012-Oslo-2012.06.08.docx"&gt;script.docx&lt;/a&gt; [28k]&lt;/li&gt;
&lt;li&gt;Download &lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkAsync2012/2671.Async1-introduction-_5B00_code_5D00_-_2D00_-Lucian-Wischik-_2D00_-NDC2012-Oslo-2012.06.08.zip"&gt;source_code.zip&lt;/a&gt; [640k, requires VS2012 and Win8]&lt;br /&gt;&amp;nbsp;&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;&lt;strong&gt;I gave this talk in VB at DevConnections2012 in Las Vegas, USA on 2012.03.26&lt;/strong&gt;&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;Download &lt;a href="http://media.ch9.ms/ch9/b8c8/c31a592d-42d3-4e65-810f-a41280b0b8c8/LucianWischikAsync_high.mp4"&gt;video.mp4&lt;/a&gt; [120mb, and I'm afraid the sound quality at the start is pretty bad]&lt;/li&gt;
&lt;li&gt;Download&amp;nbsp;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkAsync2012/2110.Async-_5B00_slides_5D00_-_2D00_-Lucian-Wischik-_2D00_-DevConnections-Vegas-2012.03.26.pptx"&gt;slides.pptx&lt;/a&gt; [3.1mb]&lt;/li&gt;
&lt;li&gt;Download&amp;nbsp;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkAsync2012/0882.Async-_5B00_script_5D00_-_2D00_-Lucian-Wischik-_2D00_-DevConnections-Vegas-2012.03.26.docx"&gt;script.docx&lt;/a&gt; [30k]&lt;/li&gt;
&lt;li&gt;Download &lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkAsync2012/3527.Async-_5B00_code_5D00_-_2D00_-Lucian-Wischik-_2D00_-DevConnections-Vegas-2012.03.26.zip"&gt;source_code.zip&lt;/a&gt;&amp;nbsp;[1.4Mb, requires VS11 and Win8]&lt;br /&gt;&amp;nbsp;&lt;/li&gt;
&lt;/ul&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkAsync2012/1663.async1.jpg"&gt;&lt;img src="http://blogs.msdn.com/resized-image.ashx/__size/400x225/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkAsync2012/1663.async1.jpg" alt="" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkAsync2012/0257.async2.jpg"&gt;&lt;img src="http://blogs.msdn.com/resized-image.ashx/__size/400x225/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkAsync2012/0257.async2.jpg" alt="" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkAsync2012/4152.async3.jpg"&gt;&lt;img src="http://blogs.msdn.com/resized-image.ashx/__size/400x225/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkAsync2012/4152.async3.jpg" alt="" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkAsync2012/8625.async4.jpg"&gt;&lt;img src="http://blogs.msdn.com/resized-image.ashx/__size/400x225/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkAsync2012/8625.async4.jpg" alt="" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkAsync2012/4682.async5.jpg"&gt;&lt;img src="http://blogs.msdn.com/resized-image.ashx/__size/400x225/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkAsync2012/4682.async5.jpg" alt="" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkAsync2012/6406.async6.jpg"&gt;&lt;img src="http://blogs.msdn.com/resized-image.ashx/__size/400x225/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkAsync2012/6406.async6.jpg" alt="" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I want to call out thanks to Greg Paperin, who came up with the slide ideas for "generations of programming languages" and "message-loop animation".&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10288734" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/lucian/archive/tags/async/">async</category><category domain="http://blogs.msdn.com/b/lucian/archive/tags/talk/">talk</category></item><item><title>Talk: What's new in VB11 (VS11 Beta)</title><link>http://blogs.msdn.com/b/lucian/archive/2012/03/29/talk-what-s-new-in-vb11-vs11-beta.aspx</link><pubDate>Wed, 28 Mar 2012 23:21:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10288708</guid><dc:creator>Lucian Wischik, MSFT</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/lucian/rsscomments.aspx?WeblogPostID=10288708</wfw:commentRss><comments>http://blogs.msdn.com/b/lucian/archive/2012/03/29/talk-what-s-new-in-vb11-vs11-beta.aspx#comments</comments><description>&lt;h1&gt;Talk: What's new in VB11&lt;/h1&gt;
&lt;p&gt;This talk details some&amp;nbsp;of what's new in VB11. (Just some: there were too many improvements to cover them all in just one talk).&lt;br /&gt;&lt;br /&gt;&lt;em&gt;Win8 support&lt;br /&gt;VBCore (=&amp;gt; new&amp;nbsp;platform support&amp;nbsp;for VB&amp;nbsp;on Phone, XNA, MicroFramework, Kinect, Surface...)&lt;br /&gt;Async&lt;br /&gt;CallerMemberInfo&lt;br /&gt;Iterators&lt;br /&gt;View Call Hierarchy&lt;br /&gt;Namespace Global&lt;br /&gt;AnyCpu32bitPreferred allows EnC on x64 machines&lt;br /&gt;Better codespit and pretty-listing&lt;br /&gt;Better performance&lt;br /&gt;Better error-reporting for lambdas, and (not yet in VS11beta but will&amp;nbsp;be) no more 102-error-limit for vbc/msbuild&lt;br /&gt;Fixed language corners in ForEach, ReturnArrayLiterals, OptionalParameterOverloads, GenericOverloadResolution&lt;br /&gt;&lt;/em&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;I gave this talk at&amp;nbsp;DevConnections2012 in&amp;nbsp;Vegas on 2012.93.26&lt;/strong&gt;&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="http://channel9.msdn.com/posts/Lucian-Wischik-Whats-New-in-VB11"&gt;Watch video&lt;/a&gt; online&lt;/li&gt;
&lt;li&gt;Download &lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkNewInVB11/3122.What_2700_s-New-in-VB11-_5B00_code_5D00_-_2D00_-Lucian-Wischik-_2D00_-DevConnections-Vegas-2012.03.26.zip"&gt;source_code.zip&lt;/a&gt; [82k, requires VS11]&lt;/li&gt;
&lt;li&gt;Download &lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkNewInVB11/1263.What_2700_s-New-in-VB11-_5B00_script_5D00_-_2D00_-Lucian-Wischik-_2D00_-DevConnections-Vegas-2012.03.26.docx"&gt;script.docx&lt;/a&gt; [29k]&lt;/li&gt;
&lt;li&gt;Download &lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkNewInVB11/7851.What_2700_s-New-in-VB11-_5B00_slides_5D00_-_2D00_-Lucian-Wischik-_2D00_-DevConnections-Vegas-2012.03.26.pptx"&gt;slides.pptx&lt;/a&gt;&amp;nbsp;[3.4mb]&lt;br /&gt;&amp;nbsp;&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;&lt;strong&gt;Similar material elsewhere&lt;/strong&gt;
&lt;ul&gt;
&lt;li&gt;Blog post&amp;nbsp;by Jonathan Aneja - &lt;a href="http://blogs.msdn.com/b/vbteam/archive/2012/02/28/visual-basic-11-beta-available-for-download.aspx"&gt;read article "What's new in VB11"&lt;/a&gt; on the VB Team Blog&lt;/li&gt;
&lt;li&gt;MSDN article &lt;a href="http://msdn.microsoft.com/en-us/library/cc714070(v=vs.110).aspx"&gt;"Breaking Changes in VB11"&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkNewInVB11/6153.vb1.jpg"&gt;&lt;img alt="" src="http://blogs.msdn.com/resized-image.ashx/__size/400x300/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkNewInVB11/6153.vb1.jpg" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkNewInVB11/6787.vb2.jpg"&gt;&lt;img alt="" src="http://blogs.msdn.com/resized-image.ashx/__size/400x300/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkNewInVB11/6787.vb2.jpg" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkNewInVB11/3718.vb3.jpg"&gt;&lt;img alt="" src="http://blogs.msdn.com/resized-image.ashx/__size/400x300/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkNewInVB11/3718.vb3.jpg" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkNewInVB11/4336.vb4.jpg"&gt;&lt;img alt="" src="http://blogs.msdn.com/resized-image.ashx/__size/400x300/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkNewInVB11/4336.vb4.jpg" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkNewInVB11/0842.vb5.jpg"&gt;&lt;img alt="" src="http://blogs.msdn.com/resized-image.ashx/__size/400x300/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkNewInVB11/0842.vb5.jpg" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkNewInVB11/0535.vb6.jpg"&gt;&lt;img alt="" src="http://blogs.msdn.com/resized-image.ashx/__size/400x300/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkNewInVB11/0535.vb6.jpg" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10288708" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/lucian/archive/tags/async/">async</category><category domain="http://blogs.msdn.com/b/lucian/archive/tags/talk/">talk</category><category domain="http://blogs.msdn.com/b/lucian/archive/tags/iterators/">iterators</category></item><item><title>AsyncCTP installation problems (and VS11)</title><link>http://blogs.msdn.com/b/lucian/archive/2012/03/25/asyncctp-installation-problems-and-vs11.aspx</link><pubDate>Sun, 25 Mar 2012 17:01:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10287333</guid><dc:creator>Lucian Wischik, MSFT</dc:creator><slash:comments>7</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/lucian/rsscomments.aspx?WeblogPostID=10287333</wfw:commentRss><comments>http://blogs.msdn.com/b/lucian/archive/2012/03/25/asyncctp-installation-problems-and-vs11.aspx#comments</comments><description>&lt;p&gt;The AsyncCTP is getting a bit long in the tooth. This post is to explain how, why, and what to do.&lt;/p&gt;
&lt;p&gt;Common symptoms:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;AsyncCTP fails to install -- i.e. it doesn't create the "Async CTP" folder inside "My Documents", and VS doesn't recognize the async keywords.&lt;/li&gt;
&lt;li&gt;Installing VS11 causes AsyncCTP to stop working.&lt;/li&gt;
&lt;li&gt;AsyncCTP doesn't work on Windows 8.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Explanation&lt;/h3&gt;
&lt;p&gt;The AsyncCTP is an &lt;em&gt;update&lt;/em&gt;. It updates Visual Studio 2010 with modified versions of the VS IDE compilers that colorize the new keywords. It also updates the .NET4 framework with modified versions of the command-line csc.exe and vbc.exe compilers. But the AsyncCTP was never incorporated into Microsoft's official source code for VS2010 or .NET4, and was never meant to be --&amp;nbsp;it's just an experimental CTP.&lt;/p&gt;
&lt;p&gt;And so, when Microsoft produces official updates to either VS or .NET4, and they touch the same files, then they tend&amp;nbsp;to overwrite the AsyncCTP -- or&amp;nbsp;prevent it from installing in the first place.&lt;/p&gt;
&lt;p&gt;Also .NET45, which comes with VS11 and Win8, is an &lt;em&gt;in-place update&lt;/em&gt; on top of .NET4, and so replaces the csc.exe and vbc.exe from the AsyncCTP with updated ones. As Stephen Toub &lt;a href="http://blogs.msdn.com/b/pfxteam/archive/2012/03/03/10277166.aspx"&gt;explains&lt;/a&gt;, the new csc.exe and vbc.exe are incompatible with AsyncCTPLibrary.dll.&lt;/p&gt;
&lt;h3&gt;What to do&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;Best option is to upgrade to &lt;a href="http://www.microsoft.com/visualstudio/11/en-us"&gt;VS11 Beta&lt;/a&gt;&amp;nbsp;and .NET45.&amp;nbsp;It has much better async support (fewer bugs, more features, better debugging, better perf)&lt;br /&gt;&amp;nbsp;&lt;/li&gt;
&lt;li&gt;Failing that, if you're stuck with VS2010 + AsyncCTP, here are ideas:&lt;/li&gt;
&lt;ol&gt;
&lt;li&gt;If you already have VS2010 + AsyncCTP working, then &lt;em&gt;don't&lt;/em&gt; install VS11.&lt;br /&gt;&amp;nbsp;&lt;/li&gt;
&lt;li&gt;If you have VS2010 SP1 but the AsyncCTP installer doesn't seem to have worked, the problem might be conflicting updates such as&lt;br /&gt;&amp;nbsp;&amp;nbsp; &lt;span style="background-color: #ffff00;"&gt;&lt;strong&gt;KB2635973,&lt;/strong&gt; &lt;strong&gt;KB2615527, KB2645410&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;Any update after October 11 is liable to conflict, though. Go to ControlPanel &amp;gt; AddRemovePrograms &amp;gt; WindowsUpdates and uninstall them if present.&lt;/li&gt;
&lt;/ol&gt;&lt;/ol&gt;
&lt;p&gt;Platforms:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Targeting .NET45&lt;/strong&gt; with async:&lt;br /&gt;you have to use VS11 Beta, which works fine.&lt;br /&gt;&amp;nbsp;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Targeting Silverlight5 or .NET4&lt;/strong&gt; with async:&lt;br /&gt;you're currently stuck&amp;nbsp;with VS2010 + AsyncCTP. We're exploring options here to let you use VS11 instead.&lt;br /&gt;&amp;nbsp;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Targeting Windows Phone&lt;/strong&gt; with async:&lt;br /&gt;again you're currently stuck&amp;nbsp;with VS2010 + AsyncCTP.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Other ideas:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If you can set up a VM with only VS2010 SP1 and no updates, then AsyncCTP should install fine.&lt;br /&gt;Then you can copy the "Microsoft Visual Studio Async CTP" folder out of My Documents, to use on other machines.&lt;br /&gt;&amp;nbsp;&lt;/li&gt;
&lt;li&gt;From the same VM, you could go&amp;nbsp;C:\Windows\Microsoft.Net\Framework and copy the following files. They constitute the entirety of AsyncCTP updates to the .NET4 framework. You could copy these files to a build machine, for instance, and then msbuild would use the AsyncCTP without needing to run the installer. (&lt;em&gt;Caution! This will likely break regular .NET updates!)&lt;/em&gt;&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;csc.exe, vbc.exe, 1033\cscui.dll, 1033\vbc7ui.dll&lt;br /&gt;&amp;nbsp;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;It's theoretically possible to do the same trick for VS2010, copying just the files that were modified by the AsyncCTP installer. But this is error-prone and more likely to leave VS2010 in a broken state than not.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;NB. The AsyncCTP installer has a problem with network drives: if your My Documents is mapped to a network drive, then it won't install.&lt;/p&gt;
&lt;h3&gt;Thoughts&lt;/h3&gt;
&lt;p&gt;We understand async has become indispensable&amp;nbsp;for many of your projects. Ultimately, though, we can't support the AsyncCTP --&amp;nbsp;it's too fragile and has too many bugs. It took us an entire release cycle to fix it, and VS11/.NET45 is the result. The only long-term supportable strategy is for people to migrate over to VS11. As soon as we've figured out the migrate story, we'll let you know.&lt;/p&gt;
&lt;p&gt;PS. If you&amp;nbsp;notice other updates which conflict with AsyncCTP, please post in the comments here, and I'll update the instructions.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10287333" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/lucian/archive/tags/async/">async</category></item><item><title>Async CTP v3 – installation</title><link>http://blogs.msdn.com/b/lucian/archive/2011/11/01/async-ctp-v3-installation.aspx</link><pubDate>Tue, 01 Nov 2011 00:01:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10231882</guid><dc:creator>Lucian Wischik, MSFT</dc:creator><slash:comments>20</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/lucian/rsscomments.aspx?WeblogPostID=10231882</wfw:commentRss><comments>http://blogs.msdn.com/b/lucian/archive/2011/11/01/async-ctp-v3-installation.aspx#comments</comments><description>&lt;p&gt;The third version of the &lt;a href="http://msdn.microsoft.com/en-us/vstudio/async/"&gt;Async CTP&lt;/a&gt; was released on 31st October 2011.&lt;/p&gt;
&lt;p&gt;The reason for releasing v3 is to address installation issues -- a recent Windows Update had conflicted with the Async CTP v2 and stopped it working, or prevented its installation in the first place. The v3 release also contains one small bugfix, to stop intermittent VB IDE crashes.&lt;/p&gt;
&lt;p&gt;This post is to help with some common installation issues.&lt;/p&gt;
&lt;p&gt;&lt;span style="color: #ff0000;" color="#ff0000"&gt;&lt;strong&gt;&lt;em&gt;Run Windows Update before installing Async CTP v3, and get all updates (including Optional updates) from before October 2011&lt;/em&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="color: #ff0000;" color="#ff0000"&gt;&lt;strong&gt;&lt;em&gt;***&amp;nbsp;BUT&amp;nbsp;NONE that came after. Three in particular, KB2635973 and KB2615527 and KB2645410, are known to be incompatible with Async CTP v3.&lt;/em&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="color: #ff0000;" color="#ff0000"&gt;&lt;strong&gt;&lt;em&gt;After installing Async CTP v3, then you can get subsequent updates.&lt;/em&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Q. Why do we keep releasing updates of the Async CTP? This is the third version so far:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;October 2010 &amp;ndash; original &lt;strong&gt;Async CTP v1&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;April 2011 &amp;ndash; &lt;strong&gt;Async CTP v2 (SP1 Refresh)&lt;/strong&gt;, to make it compatible with SP1 of VS2010 and fix some bugs&lt;/li&gt;
&lt;li&gt;October 2011 &amp;ndash; &lt;strong&gt;Async CTP v3&lt;/strong&gt;, to make it work with recent product updates&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The reason is that Visual Studio and .NET4 keep being updated. The Async CTP is actually a mini-fork of VS2010. Therefore, each time there&amp;rsquo;s a Windows Update which includes changes to the VB or C# compilers, then the Async CTP gets out of date: the update will break the Async CTP if you have it on your machine, and it&amp;rsquo;ll block the Async CTP from being installed on top of it. So far these conflicting updates have been coming out about once every six months, and so we&amp;rsquo;ve had to re-release the Async CTP every six months.&lt;/p&gt;
&lt;p&gt;Note: &lt;span style="color: #000000;" color="#ff0000"&gt;The Async CTP v3 can be installed over the top of Async CTP v2 (SP1 Refresh): you don&amp;rsquo;t need to uninstall it first.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;&lt;span style="color: #ff0000;" color="#ff0000"&gt;&lt;span style="color: #260859;" color="#260859"&gt;A quick check that the Async CTP is working&lt;/span&gt;&lt;/span&gt;&lt;/h2&gt;
&lt;p&gt;&lt;span style="color: #000000;" color="#000000"&gt;If you suspect that something&amp;rsquo;s wrong with your installation of the Async CTP, then please check the version number of these four files. (Please check all four, even if you&amp;rsquo;re not doing C# or VB or Phone development).&lt;/span&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;C:\Windows\Microsoft.Net\Framework\v4.0.30319\&lt;b&gt;csc.exe&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;C:\Windows\Microsoft.Net\Framework\v4.0.30319\&lt;b&gt;vbc.exe&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\&lt;b&gt;msvbide.dll&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;My Documents\Microsoft Visual Studio Async CTP\Samples\&lt;b&gt;AsyncCtpLibrary_Phone.dll&lt;/b&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;If you see the following version numbers, then the Async CTP Refresh has been correctly installed:&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;table class="MsoNormalTable" style="list-style-type: disc; mso-cellspacing: 0in; mso-yfti-tbllook: 1184; mso-padding-alt: 0in 0in 0in 0in;" border="1" cellspacing="0" cellpadding="0"&gt;
&lt;tbody&gt;
&lt;tr style="mso-yfti-irow: 0; mso-yfti-firstrow: yes;"&gt;
&lt;td width="96" valign="top" style="background: black; padding: 0in;"&gt;&amp;nbsp;&lt;/td&gt;
&lt;td width="100" valign="top" style="background: black; padding: 0in;"&gt;&lt;span style="font-size: 12pt;"&gt;&lt;/span&gt;&lt;span style="color: #ffffff;" color="#ffffff"&gt;&lt;/span&gt;
&lt;p class="MsoNormal" style="margin: 0in 0in 10pt; line-height: normal; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"&gt;&lt;b&gt;&lt;span style="mso-fareast-font-family: 'Times New Roman';"&gt;&lt;span style="color: #ffffff; font-size: 10.5pt;" color="#ffffff"&gt;csc.exe&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="mso-fareast-font-family: 'Times New Roman';"&gt;&lt;/span&gt;&lt;/p&gt;
&lt;span style="font-size: 12pt;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td width="102" valign="top" style="background: black; padding: 0in;"&gt;&lt;span style="font-size: 12pt;"&gt;&lt;/span&gt;&lt;span style="color: #ffffff;" color="#ffffff"&gt;&lt;/span&gt;
&lt;p class="MsoNormal" style="margin: 0in 0in 10pt; line-height: normal; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"&gt;&lt;b&gt;&lt;span style="mso-fareast-font-family: 'Times New Roman';"&gt;&lt;span style="color: #ffffff; font-size: 10.5pt;" color="#ffffff"&gt;vbc.exe&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="mso-fareast-font-family: 'Times New Roman';"&gt;&lt;/span&gt;&lt;/p&gt;
&lt;span style="font-size: 12pt;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td width="103" valign="top" style="background: black; padding: 0in;"&gt;&lt;span style="font-size: 12pt;"&gt;&lt;/span&gt;&lt;span style="color: #ffffff;" color="#ffffff"&gt;&lt;/span&gt;
&lt;p class="MsoNormal" style="margin: 0in 0in 10pt; line-height: normal; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"&gt;&lt;b&gt;&lt;span style="mso-fareast-font-family: 'Times New Roman';"&gt;&lt;span style="color: #ffffff; font-size: 10.5pt;" color="#ffffff"&gt;msvbide.dll&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="mso-fareast-font-family: 'Times New Roman';"&gt;&lt;/span&gt;&lt;/p&gt;
&lt;span style="font-size: 12pt;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td width="156" valign="top" style="background: black; padding: 0in;"&gt;&lt;span style="font-size: 12pt;"&gt;&lt;/span&gt;&lt;span style="color: #ffffff;" color="#ffffff"&gt;&lt;/span&gt;
&lt;p class="MsoNormal" style="margin: 0in 0in 10pt; line-height: normal; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"&gt;&lt;b&gt;&lt;span style="mso-fareast-font-family: 'Times New Roman';"&gt;&lt;span style="color: #ffffff; font-size: 10.5pt;" color="#ffffff"&gt;AsyncCTPLibrary_Phone.dll&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="mso-fareast-font-family: 'Times New Roman';"&gt;&lt;/span&gt;&lt;/p&gt;
&lt;span style="font-size: 12pt;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr style="mso-yfti-irow: 1; mso-yfti-lastrow: yes;"&gt;
&lt;td width="96" valign="top" style="background: yellow; padding: 0in;"&gt;&lt;span style="font-size: 12pt;"&gt;&lt;/span&gt;
&lt;p class="MsoNormal" style="margin: 0in 0in 10pt; line-height: normal; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"&gt;&lt;b&gt;&lt;span style="mso-fareast-font-family: 'Times New Roman';"&gt;&lt;span style="font-size: 10.5pt;"&gt;Async CTP v3&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;span style="font-size: 12pt;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td width="100" valign="top" style="background: yellow; padding: 0in;"&gt;&lt;span style="font-size: 12pt;"&gt;&lt;/span&gt;
&lt;p class="MsoNormal" style="margin: 0in 0in 10pt; line-height: normal; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"&gt;&lt;span style="mso-fareast-font-family: 'Times New Roman';"&gt;&lt;span style="font-size: 10.5pt;"&gt;4.0.30319.517&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;span style="font-size: 12pt;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td width="102" valign="top" style="background: yellow; padding: 0in;"&gt;&lt;span style="font-size: 12pt;"&gt;&lt;/span&gt;
&lt;p class="MsoNormal" style="margin: 0in 0in 10pt; line-height: normal; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"&gt;&lt;span style="mso-fareast-font-family: 'Times New Roman';"&gt;&lt;span style="font-size: 10.5pt;"&gt;10.0.30319.517 &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;span style="font-size: 12pt;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td width="103" valign="top" style="background: yellow; padding: 0in;"&gt;&lt;span style="font-size: 12pt;"&gt;&lt;/span&gt;
&lt;p class="MsoNormal" style="margin: 0in 0in 10pt; line-height: normal; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"&gt;&lt;span style="mso-fareast-font-family: 'Times New Roman';"&gt;&lt;span style="font-size: 10.5pt;"&gt;4.0.40219.355&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;span style="font-size: 12pt;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td width="156" valign="top" style="background: yellow; padding: 0in;"&gt;&lt;span style="font-size: 12pt;"&gt;&lt;/span&gt;
&lt;p class="MsoNormal" style="margin: 0in 0in 10pt; line-height: normal; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"&gt;&lt;span style="mso-fareast-font-family: 'Times New Roman';"&gt;&lt;span style="font-size: 10.5pt;"&gt;1.1.0.0&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;span style="font-size: 12pt;"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Q. How to find those version numbers? A. Right-click on each file and choose Properties; the version number is in the "Details" tab.&lt;/p&gt;
&lt;p&gt;&lt;img src="http://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/CommunityServer-Blogs-Components-WeblogFiles/00-00-01-12-06/3187.async_2D00_refresh_2D00_version_2D00_1.png" border="0" /&gt; &lt;img src="http://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/CommunityServer-Blogs-Components-WeblogFiles/00-00-01-12-06/1121.async_2D00_refresh_2D00_version_2D00_2.png" border="0" /&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;h2&gt;If the version numbers are incorrect&lt;/h2&gt;
&lt;p&gt;Here are some other version numbers you might find&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;table class="MsoNormalTable" style="list-style-type: disc; mso-cellspacing: 0in; mso-yfti-tbllook: 1184; mso-padding-alt: 0in 0in 0in 0in;" border="1" cellspacing="0" cellpadding="0"&gt;
&lt;tbody&gt;
&lt;tr style="mso-yfti-irow: 0; mso-yfti-firstrow: yes;"&gt;
&lt;td width="96" valign="top" style="background: black; padding: 0in;"&gt;&amp;nbsp;&lt;/td&gt;
&lt;td width="100" valign="top" style="background: black; padding: 0in;"&gt;&lt;span style="color: #ffffff; font-size: x-small;" color="#ffffff" size="2"&gt;&lt;/span&gt;
&lt;p class="MsoNormal" style="margin: 0in 0in 10pt; line-height: normal; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"&gt;&lt;b&gt;&lt;span style="mso-fareast-font-family: 'Times New Roman';"&gt;&lt;span style="color: #ffffff; font-size: x-small;" color="#ffffff" size="2"&gt;csc.exe&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="mso-fareast-font-family: 'Times New Roman';"&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;td width="95" valign="top" style="background: black; padding: 0in;"&gt;&lt;span style="color: #ffffff; font-size: x-small;" color="#ffffff" size="2"&gt;&lt;/span&gt;
&lt;p class="MsoNormal" style="margin: 0in 0in 10pt; line-height: normal; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"&gt;&lt;b&gt;&lt;span style="mso-fareast-font-family: 'Times New Roman';"&gt;&lt;span style="color: #ffffff; font-size: x-small;" color="#ffffff" size="2"&gt;vbc.exe&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="mso-fareast-font-family: 'Times New Roman';"&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;td width="97" valign="top" style="background: black; padding: 0in;"&gt;&lt;span style="color: #ffffff; font-size: x-small;" color="#ffffff" size="2"&gt;&lt;/span&gt;
&lt;p class="MsoNormal" style="margin: 0in 0in 10pt; line-height: normal; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"&gt;&lt;b&gt;&lt;span style="mso-fareast-font-family: 'Times New Roman';"&gt;&lt;span style="color: #ffffff; font-size: x-small;" color="#ffffff" size="2"&gt;msvbide.dll&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="mso-fareast-font-family: 'Times New Roman';"&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;td width="163" valign="top" style="background: black; padding: 0in;"&gt;&lt;span style="color: #ffffff; font-size: x-small;" color="#ffffff" size="2"&gt;&lt;/span&gt;
&lt;p class="MsoNormal" style="margin: 0in 0in 10pt; line-height: normal; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"&gt;&lt;span style="font-size: x-small;" size="2"&gt;&lt;span style="color: #ffffff;" color="#ffffff"&gt;&lt;b&gt;&lt;span style="mso-fareast-font-family: 'Times New Roman';"&gt;AsyncCTPLibrary_Phone.dll&lt;/span&gt;&lt;/b&gt;&lt;b&gt;&lt;span style="mso-fareast-font-family: 'Times New Roman';"&gt;l&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;span style="mso-fareast-font-family: 'Times New Roman';"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr style="mso-yfti-irow: 1;"&gt;
&lt;td width="96" valign="top" style="background: #aaaaaa; padding: 0in;"&gt;&lt;span style="font-size: x-small;" size="2"&gt;&lt;/span&gt;
&lt;p class="MsoNormal" style="margin: 0in 0in 10pt; line-height: normal; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"&gt;&lt;span style="font-size: x-small;" size="2"&gt;&lt;b&gt;&lt;span style="mso-fareast-font-family: 'Times New Roman';"&gt;VS2010&lt;/span&gt;&lt;/b&gt;&lt;span style="mso-fareast-font-family: 'Times New Roman';"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;td width="100" valign="top" style="background: #aaaaaa; padding: 0in;"&gt;&lt;span style="font-size: x-small;" size="2"&gt;&lt;/span&gt;
&lt;p class="MsoNormal" style="margin: 0in 0in 10pt; line-height: normal; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"&gt;&lt;span style="mso-fareast-font-family: 'Times New Roman';"&gt;&lt;span style="font-size: x-small;" size="2"&gt;4.0.30319.1&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;td width="95" valign="top" style="background: #aaaaaa; padding: 0in;"&gt;&lt;span style="font-size: x-small;" size="2"&gt;&lt;/span&gt;
&lt;p class="MsoNormal" style="margin: 0in 0in 10pt; line-height: normal; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"&gt;&lt;span style="mso-fareast-font-family: 'Times New Roman';"&gt;&lt;span style="font-size: x-small;" size="2"&gt;10.0.30319.1&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;td width="97" valign="top" style="background: #aaaaaa; padding: 0in;"&gt;&lt;span style="font-size: x-small;" size="2"&gt;&lt;/span&gt;
&lt;p class="MsoNormal" style="margin: 0in 0in 10pt; line-height: normal; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"&gt;&lt;span style="mso-fareast-font-family: 'Times New Roman';"&gt;&lt;span style="font-size: x-small;" size="2"&gt;10.0.30319.1&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;td width="163" valign="top" style="background: #aaaaaa; padding: 0in;"&gt;&lt;span style="font-size: x-small;" size="2"&gt;&lt;/span&gt;
&lt;p class="MsoNormal" style="margin: 0in 0in 10pt; line-height: normal; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"&gt;&lt;span style="font-size: x-small;" size="2"&gt;&lt;i&gt;&lt;span style="mso-fareast-font-family: 'Times New Roman';"&gt;absent&lt;/span&gt;&lt;/i&gt;&lt;span style="mso-fareast-font-family: 'Times New Roman';"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr style="mso-yfti-irow: 2;"&gt;
&lt;td width="96" valign="top" style="background: #aaaaaa; padding: 0in;"&gt;&lt;span style="font-size: x-small;" size="2"&gt;&lt;/span&gt;
&lt;p class="MsoNormal" style="margin: 0in 0in 10pt; line-height: normal; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"&gt;&lt;span style="font-size: x-small;" size="2"&gt;&lt;b&gt;&lt;span style="mso-fareast-font-family: 'Times New Roman';"&gt;Async CTP v1&lt;/span&gt;&lt;/b&gt;&lt;span style="mso-fareast-font-family: 'Times New Roman';"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;td width="100" valign="top" style="background: #aaaaaa; padding: 0in;"&gt;&lt;span style="font-size: x-small;" size="2"&gt;&lt;/span&gt;
&lt;p class="MsoNormal" style="margin: 0in 0in 10pt; line-height: normal; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"&gt;&lt;span style="mso-fareast-font-family: 'Times New Roman';"&gt;&lt;span style="font-size: x-small;" size="2"&gt;4.0.30319.326&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;td width="95" valign="top" style="background: #aaaaaa; padding: 0in;"&gt;&lt;span style="font-size: x-small;" size="2"&gt;&lt;/span&gt;
&lt;p class="MsoNormal" style="margin: 0in 0in 10pt; line-height: normal; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"&gt;&lt;span style="mso-fareast-font-family: 'Times New Roman';"&gt;&lt;span style="font-size: x-small;" size="2"&gt;10.0.30319.326&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;td width="97" valign="top" style="background: #aaaaaa; padding: 0in;"&gt;&lt;span style="font-size: x-small;" size="2"&gt;&lt;/span&gt;
&lt;p class="MsoNormal" style="margin: 0in 0in 10pt; line-height: normal; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"&gt;&lt;span style="mso-fareast-font-family: 'Times New Roman';"&gt;&lt;span style="font-size: x-small;" size="2"&gt;10.0.30319.326&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;td width="163" valign="top" style="background: #aaaaaa; padding: 0in;"&gt;&lt;span style="font-size: x-small;" size="2"&gt;&lt;/span&gt;
&lt;p class="MsoNormal" style="margin: 0in 0in 10pt; line-height: normal; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"&gt;&lt;span style="font-size: x-small;" size="2"&gt;&lt;i&gt;&lt;span style="mso-fareast-font-family: 'Times New Roman';"&gt;absent&lt;/span&gt;&lt;/i&gt;&lt;span style="mso-fareast-font-family: 'Times New Roman';"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr style="mso-yfti-irow: 3;"&gt;
&lt;td width="96" valign="top" style="background: #aaaaaa; padding: 0in;"&gt;&lt;span style="font-size: x-small;" size="2"&gt;&lt;/span&gt;
&lt;p class="MsoNormal" style="margin: 0in 0in 10pt; line-height: normal; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"&gt;&lt;span style="font-size: x-small;" size="2"&gt;&lt;b&gt;&lt;span style="mso-fareast-font-family: 'Times New Roman';"&gt;VS2010 SP1&lt;/span&gt;&lt;/b&gt;&lt;span style="mso-fareast-font-family: 'Times New Roman';"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;td width="100" valign="top" style="background: #aaaaaa; padding: 0in;"&gt;&lt;span style="font-size: x-small;" size="2"&gt;&lt;/span&gt;
&lt;p class="MsoNormal" style="margin: 0in 0in 10pt; line-height: normal; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"&gt;&lt;span style="mso-fareast-font-family: 'Times New Roman';"&gt;&lt;span style="font-size: x-small;" size="2"&gt;4.0.30319.1&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;td width="95" valign="top" style="background: #aaaaaa; padding: 0in;"&gt;&lt;span style="font-size: x-small;" size="2"&gt;&lt;/span&gt;
&lt;p class="MsoNormal" style="margin: 0in 0in 10pt; line-height: normal; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"&gt;&lt;span style="mso-fareast-font-family: 'Times New Roman';"&gt;&lt;span style="font-size: x-small;" size="2"&gt;10.0.30319.225 &lt;br /&gt; 10.0.30319.431&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;td width="97" valign="top" style="background: #aaaaaa; padding: 0in;"&gt;&lt;span style="font-size: x-small;" size="2"&gt;&lt;/span&gt;
&lt;p class="MsoNormal" style="margin: 0in 0in 10pt; line-height: normal; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"&gt;&lt;span style="mso-fareast-font-family: 'Times New Roman';"&gt;&lt;span style="font-size: x-small;" size="2"&gt;10.0.40219.1&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;td width="163" valign="top" style="background: #aaaaaa; padding: 0in;"&gt;&lt;span style="font-size: x-small;" size="2"&gt;&lt;/span&gt;
&lt;p class="MsoNormal" style="margin: 0in 0in 10pt; line-height: normal; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"&gt;&lt;span style="font-size: x-small;" size="2"&gt;&lt;i&gt;&lt;span style="mso-fareast-font-family: 'Times New Roman';"&gt;absent&lt;/span&gt;&lt;/i&gt;&lt;span style="mso-fareast-font-family: 'Times New Roman';"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr style="mso-yfti-irow: 3;"&gt;
&lt;td width="96" valign="top" style="background: #aaaaaa; padding: 0in;"&gt;&lt;span style="font-size: x-small;" size="2"&gt;&lt;strong&gt;Windows Update or &lt;br /&gt;&lt;/strong&gt;&lt;a href="http://www.microsoft.com/download/en/details.aspx?displaylang=en&amp;amp;id=3556"&gt;&lt;strong&gt;KB2468871&lt;/strong&gt;&lt;/a&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td width="100" valign="top" style="background: #aaaaaa; padding: 0in;"&gt;&lt;span style="font-size: x-small;" size="2"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td width="95" valign="top" style="background: #aaaaaa; padding: 0in;"&gt;&lt;span style="font-size: x-small;" size="2"&gt;10.0.30319.233 &lt;br /&gt;10.0.30319.450&lt;/span&gt;&lt;/td&gt;
&lt;td width="97" valign="top" style="background: #aaaaaa; padding: 0in;"&gt;&lt;span style="font-size: x-small;" size="2"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td width="163" valign="top" style="background: #aaaaaa; padding: 0in;"&gt;&lt;span style="font-size: x-small;" size="2"&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr style="mso-yfti-irow: 3;"&gt;
&lt;td width="96" valign="top" style="background: #aaaaaa; padding: 0in;"&gt;&lt;strong&gt;&lt;span style="font-size: x-small;" size="2"&gt;Async CTP v2 &lt;br /&gt;(SP1 Refresh)&lt;/span&gt;&lt;/strong&gt;&lt;/td&gt;
&lt;td width="100" valign="top" style="background: #aaaaaa; padding: 0in;"&gt;&lt;span style="font-size: x-small;" size="2"&gt;4.0.30319.440&lt;/span&gt;&lt;/td&gt;
&lt;td width="95" valign="top" style="background: #aaaaaa; padding: 0in;"&gt;&lt;span style="font-size: x-small;" size="2"&gt;10.0.30319.440&lt;/span&gt;&lt;/td&gt;
&lt;td width="97" valign="top" style="background: #aaaaaa; padding: 0in;"&gt;&lt;span style="font-size: x-small;" size="2"&gt;10.0.40219.15&lt;/span&gt;&lt;/td&gt;
&lt;td width="163" valign="top" style="background: #aaaaaa; padding: 0in;"&gt;&lt;span style="font-size: x-small;" size="2"&gt;1.0.0.0&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr style="mso-yfti-irow: 4; mso-yfti-lastrow: yes;"&gt;
&lt;td width="96" valign="top" style="background: yellow; padding: 0in;"&gt;&lt;span style="font-size: x-small;" size="2"&gt;&lt;/span&gt;
&lt;p class="MsoNormal" style="margin: 0in 0in 10pt; line-height: normal; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"&gt;&lt;b&gt;&lt;span style="mso-fareast-font-family: 'Times New Roman';"&gt;&lt;span style="font-size: x-small;" size="2"&gt;Async CTP v3 &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;td width="100" valign="top" style="background: yellow; padding: 0in;"&gt;&lt;span style="font-size: x-small;" size="2"&gt;&lt;/span&gt;
&lt;p class="MsoNormal" style="margin: 0in 0in 10pt; line-height: normal; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"&gt;&lt;span style="mso-fareast-font-family: 'Times New Roman';"&gt;&lt;span style="font-size: x-small;" size="2"&gt;4.0.30319.517&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;td width="95" valign="top" style="background: yellow; padding: 0in;"&gt;&lt;span style="font-size: x-small;" size="2"&gt;&lt;/span&gt;
&lt;p class="MsoNormal" style="margin: 0in 0in 10pt; line-height: normal; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"&gt;&lt;span style="mso-fareast-font-family: 'Times New Roman';"&gt;&lt;span style="font-size: x-small;" size="2"&gt;10.0.30319.517&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;td width="97" valign="top" style="background: yellow; padding: 0in;"&gt;&lt;span style="font-size: x-small;" size="2"&gt;&lt;/span&gt;
&lt;p class="MsoNormal" style="margin: 0in 0in 10pt; line-height: normal; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"&gt;&lt;span style="mso-fareast-font-family: 'Times New Roman';"&gt;&lt;span style="font-size: x-small;" size="2"&gt;10.0.40219.355&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;td width="163" valign="top" style="background: yellow; padding: 0in;"&gt;&lt;span style="font-size: x-small;" size="2"&gt;&lt;/span&gt;
&lt;p class="MsoNormal" style="margin: 0in 0in 10pt; line-height: normal; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"&gt;&lt;span style="mso-fareast-font-family: 'Times New Roman';"&gt;&lt;span style="font-size: x-small;" size="2"&gt;1.1.0.0&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;I want to stress this: you should check the version numbers of &lt;b&gt;&lt;i&gt;all four files&lt;/i&gt;&lt;/b&gt;. Even if you think you're not going to use C#, say, you should still check all four files including csc.exe. Any discrepancy in &lt;em&gt;any&lt;/em&gt; of the files will indicate your machine is in an incorrect state.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;If Visual Studio crashes upon opening a VB/C# project&lt;/h2&gt;
&lt;p&gt;The issue is that VS2010 SP1 had a bug in NGEN. The Async CTP v3 requires that this bug have already been fixed &amp;ndash; either by having Async CTP v2 already on the machine, or by running Windows Update and getting &lt;em&gt;all&lt;/em&gt; updates including optional ones, or by manually installing &lt;strong&gt;&lt;span style="font-size: x-small;" size="2"&gt;&lt;a href="http://www.microsoft.com/download/en/details.aspx?displaylang=en&amp;amp;id=3556"&gt;KB2468871&lt;/a&gt;&lt;/span&gt;&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Alternatively, if you&amp;rsquo;ve already got Async CTP v3 on your machine, you can fix the bug manually: open a Developer Command Prompt, run the following command in it, and have a nice cup of tea and biscuit because it&amp;rsquo;s going to take up to an hour:&lt;/p&gt;
&lt;p&gt;&lt;span style="color: #333333; font-family: Consolas;" color="#333333" face="Consolas"&gt;ngen update /force&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h1&gt;Uninstallation&lt;/h1&gt;
&lt;p&gt;&lt;strong&gt;To uninstall Async CTP v3&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;This is easy: &lt;strong&gt;Start &amp;gt; ControlPanel &amp;gt; UninstallPrograms &amp;gt; ViewUpdates&lt;/strong&gt;, search for all updates with &amp;ldquo;async&amp;rdquo; in their title, and uninstall all of them. You might see as many as &lt;em&gt;five&lt;/em&gt; different entries that need to be uninstalled.&lt;/p&gt;
&lt;p&gt;TIP: Although uninstall order doesn&amp;rsquo;t matter, it goes easier if you uninstall the &amp;ldquo;Visual Studio Async&amp;rdquo; updates first and the &amp;ldquo;.NET Framework Async&amp;rdquo; updates next. That&amp;rsquo;s because the uninstalling the latter will require a reboot.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;To uninstall Async CTP v2 (SP1 Refresh)&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The uninstall of Async CTP v2 has become broken due to a conflicting Windows Update. If you have v2 on your machine and want to install it, then you actually have to install v3 first.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;To uninstall Async CTP v1 (pre-SP1 release)&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;If anyone still has Async CTP v1 from October 2010: we recommend to &lt;em&gt;uninstall&lt;/em&gt; this prior to installing SP1.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h1&gt;If you still have problems&lt;/h1&gt;
&lt;p&gt;Please post a request for help in the &lt;a href="http://social.msdn.microsoft.com/Forums/en-US/async/threads"&gt;Async Discussion Forums&lt;/a&gt;. We do monitor those forums frequently, and we may be able to help.&lt;/p&gt;
&lt;p&gt;When you ask for help, &lt;em&gt;PLEASE&lt;/em&gt; mention all four version numbers. That will help!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10231882" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/lucian/archive/tags/async/">async</category></item><item><title>Talk: How to write an [Async] connected app for Windows Phone 7.1</title><link>http://blogs.msdn.com/b/lucian/archive/2011/05/20/talk-how-to-write-an-async-connected-app-for-windows-phone-7.aspx</link><pubDate>Fri, 20 May 2011 22:04:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10166861</guid><dc:creator>Lucian Wischik, MSFT</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/lucian/rsscomments.aspx?WeblogPostID=10166861</wfw:commentRss><comments>http://blogs.msdn.com/b/lucian/archive/2011/05/20/talk-how-to-write-an-async-connected-app-for-windows-phone-7.aspx#comments</comments><description>&lt;h1&gt;Talk: How to write an [Async] connected app for Windows Phone 7.1&lt;/h1&gt;
&lt;p&gt;This talk is&amp;nbsp;all about writing a &lt;em&gt;connected app&lt;/em&gt; for the Windows Phone. The talk covered:&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;(1) Azure/WP7 toolkit &lt;/strong&gt;for the server-side of the phone app. Most good phone apps will need some kind of server-side component, be it for live tile updates or just to form a social community.&lt;br /&gt;&lt;strong&gt;(2) XNA&lt;/strong&gt; for the phone app itself (a Breakout clone) using the new Mango (7.1) phone tools,&amp;nbsp;&lt;br /&gt;&lt;strong&gt;(3) Async language feature&lt;/strong&gt; which makes it &lt;em&gt;much&lt;/em&gt; easier to program how the app communicates with the server -- in this case to download high-score table.&lt;/p&gt;
&lt;p&gt;The material here is in VB, but it all works equally well with C#.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;I gave a VB-specific version of this talk at TechEd 2010 in Atlanta, Georgia on 2011.06.18&lt;/strong&gt; 
&lt;ul&gt;
&lt;li&gt;Download &lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkConnectedPhoneApp/3582.How-to-write-a-connected-Phone-app-_5B00_slides_5D00_-_2D00_-Lucian-Wischik-_2D00_-2011.06.18-TechEd2011_2C00_-Atlanta_2C00_-USA.pptx"&gt;slides.pptx&lt;/a&gt; [4.8mb]&lt;/li&gt;
&lt;li&gt;Download &lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkConnectedPhoneApp/7888.How-to-write-a-connected-Phone-app-_5B00_script_5D00_-_2D00_-Lucian-Wischik-_2D00_-2011.06.18-TechEd2011_2C00_-Atlanta_2C00_-USA.docx"&gt;script.docx&lt;/a&gt; [27k]&lt;/li&gt;
&lt;li&gt;Download &lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkConnectedPhoneApp/2570.How-to-write-a-connected-Phone-app-_5B00_code_5D00_-_2D00_-Lucian-Wischik-_2D00_-2011.06.18-TechEd2011_2C00_-Atlanta_2C00_-USA.zip"&gt;source_code.zip&lt;/a&gt; [115k], requires VS2010+SP1+&lt;a href="http://www.microsoft.com/downloads/en/details.aspx?FamilyID=77586864-ab15-40e1-bc38-713a95a56a05&amp;amp;displaylang=en"&gt;WindowsPhone7.1&lt;/a&gt;+&lt;a href="http://www.microsoft.com/downloads/en/details.aspx?FamilyID=4738205d-5682-47bf-b62e-641f6441735b&amp;amp;displaylang=en"&gt;AsyncCTP&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Session &lt;a href="http://northamerica.msteched.com/topic/details/DEV317"&gt;page online&lt;/a&gt;. Alas they haven't put up a video of it yet.&lt;/li&gt;
&lt;li&gt;To encourage people to give feedback, I donate to charity for each session eval submitted.&amp;nbsp;In all&amp;nbsp;22 were submitted and so on May 24th the charity "Doctors Without Borders" got $220, half from me, half from Microsoft. Thank you Atlanta!&lt;br /&gt;&amp;nbsp;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Sri Narayanan and I gave a C#-and-VB version of this talk at VSLive2011 in Vegas on 2011.04.19&lt;/strong&gt; 
&lt;ul&gt;
&lt;li&gt;The talk covered exactly the same material as above.&lt;/li&gt;
&lt;li&gt;The audience in Vegas filled out 14 evals and so on May 20th 2011 the charity "Doctors Without Borders" got $140, half from me, half from Microsoft. That's the kind of win I like to see at Vegas!&amp;nbsp;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&amp;nbsp;&lt;a href="http://blogs.msdn.com/cfs-filesystemfile.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkConnectedPhoneApp/2553.atlanta1.jpg"&gt;&lt;img src="http://blogs.msdn.com/cfs-filesystemfile.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkConnectedPhoneApp/2553.atlanta1.jpg" border="0" style="max-height: 225px; max-width: 400px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-filesystemfile.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkConnectedPhoneApp/5280.atlanta2.jpg"&gt;&lt;img src="http://blogs.msdn.com/cfs-filesystemfile.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkConnectedPhoneApp/5280.atlanta2.jpg" border="0" style="max-height: 225px; max-width: 400px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-filesystemfile.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkConnectedPhoneApp/7750.atlanta3.jpg"&gt;&lt;img src="http://blogs.msdn.com/cfs-filesystemfile.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkConnectedPhoneApp/7750.atlanta3.jpg" border="0" style="max-height: 225px; max-width: 400px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-filesystemfile.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkConnectedPhoneApp/0081.atlanta4.jpg"&gt;&lt;img src="http://blogs.msdn.com/cfs-filesystemfile.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-TalkConnectedPhoneApp/0081.atlanta4.jpg" border="0" style="max-height: 225px; max-width: 400px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10166861" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/lucian/archive/tags/async/">async</category><category domain="http://blogs.msdn.com/b/lucian/archive/tags/talk/">talk</category><category domain="http://blogs.msdn.com/b/lucian/archive/tags/phone/">phone</category></item><item><title>Talk: The [Async] Future of VB and C#</title><link>http://blogs.msdn.com/b/lucian/archive/2011/05/20/talk-the-future-of-vb-and-c.aspx</link><pubDate>Fri, 20 May 2011 18:29:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10166815</guid><dc:creator>Lucian Wischik, MSFT</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/lucian/rsscomments.aspx?WeblogPostID=10166815</wfw:commentRss><comments>http://blogs.msdn.com/b/lucian/archive/2011/05/20/talk-the-future-of-vb-and-c.aspx#comments</comments><description>&lt;h1&gt;Talk: The [Async] Future of VB and C#&lt;/h1&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;I gave a C#-specific version of this talk at DevConnections in Las Vegas, USA on 2011.11.01&lt;/strong&gt;&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;This talk is mainly about &lt;strong&gt;Async&lt;/strong&gt; and goes into considerable technical depth. It also covers the new&lt;strong&gt; CallerInfo &lt;/strong&gt;feature, and includes a &lt;strong&gt;Roslyn&lt;/strong&gt; demo.&lt;/li&gt;
&lt;li&gt;Download &lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-AsyncFutureOfVBCSharp/8272.Future-of-C_2300_-and-VB-_5B00_slides_5D00_-_2D00_-Lucian-Wischik-_2D00_-2011.11.01-DevConnections_2C00_-Vegas_2C00_-USA.pptx"&gt;slides.pptx&lt;/a&gt; [3mb]&lt;/li&gt;
&lt;li&gt;Download &lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-AsyncFutureOfVBCSharp/8371.Future-of-C_2300_-and-VB-_5B00_script_5D00_-_2D00_-Lucian-Wischik-_2D00_-2011.11.01-DevConnections_2C00_-Vegas_2C00_-USA.docx"&gt;script.docx&lt;/a&gt; [29k]&lt;/li&gt;
&lt;li&gt;Download &lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-AsyncFutureOfVBCSharp/7608.Future-of-C_2300_-and-VB-_5B00_code_5D00_-_2D00_-Lucian-Wischik-_2D00_-2011.11.01-DevConnections_2C00_-Vegas_2C00_-USA.zip"&gt;source_code.zip&lt;/a&gt; [42k, requires VS2010 SP1 and Async CTP v3 and Phone Developer Tools]&lt;/li&gt;
&lt;li&gt;To encourage people to give feedback, I donate to charity for each session eval submitted. I'm&amp;nbsp;looking forward to see how many evals the DevConnections audience&amp;nbsp;hand in.&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;&lt;strong&gt;I gave this talk at TechDays11 in Basel, Switzerland on 2011.05.02&lt;/strong&gt;.
&lt;ul&gt;
&lt;li&gt;This talk is mainly about &lt;strong&gt;Async&lt;/strong&gt;, with a little about &lt;strong&gt;Roslyn&lt;/strong&gt;. At PDC 2010, Anders Hejlsberg delivered talk &lt;a href="http://channel9.msdn.com/Events/PDC/PDC10/FT09"&gt;FT09&lt;/a&gt; on the same topics&lt;/li&gt;
&lt;li&gt;&lt;a href="http://channel9.msdn.com/Series/TechDays-11-Switzerland/TechDays-11-Basel-The-Future-of-C-and-Visual-Basic"&gt;Watch the talk&lt;/a&gt; online&lt;/li&gt;
&lt;li&gt;Download &lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-AsyncFutureOfVBCSharp/6406.Future-of-C_2300_-and-VB-_5B00_slides_5D00_-_2D00_-Lucian-Wischik-_2D00_-2011.05.02-Techdays11_2C00_-Basel_2C00_-Switzerland.pptx"&gt;slides.pptx&lt;/a&gt; [6.3mb]&lt;/li&gt;
&lt;li&gt;Download &lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-AsyncFutureOfVBCSharp/0726.Future-of-C_2300_-and-VB-_5B00_script_5D00_-_2D00_-Lucian-Wischik-_2D00_-2011.05.02-Techdays11_2C00_-Basel_2C00_-Switzerland.docx"&gt;script.docx&lt;/a&gt; [22k]&lt;/li&gt;
&lt;li&gt;Download&amp;nbsp;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-AsyncFutureOfVBCSharp/3750.Future-of-C_2300_-and-VB-_5B00_code_5D00_-_2D00_-Lucian-Wischik-_2D00_-2011.05.02-Techdays11_2C00_-Basel_2C00_-Switzerland.zip"&gt;source_code.zip&lt;/a&gt; [16k, requires VS2010 SP1 and Async CTP]&lt;/li&gt;
&lt;li&gt;The audience at Basel filled out 89 evals and so on May 20th 2011 the charity "Medecins Sans Frontiers" got $890 half from me and half from Microsoft. Thank you, Basel!&lt;br /&gt;&amp;nbsp;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;I gave a VB-specific version of this talk at TechEd Europe 2010 in Berlin, Germany on 2010.11.11&lt;/strong&gt;
&lt;ul&gt;
&lt;li&gt;This VB-specific&amp;nbsp;talk&amp;nbsp;covered &lt;strong&gt;Iterators in VB&lt;/strong&gt;, and &lt;strong&gt;VB Support for Windows Phone 7&lt;/strong&gt;, as well as &lt;strong&gt;Async&lt;/strong&gt;&amp;nbsp;above. (The presentation of Async above was better than what I did here...)&lt;/li&gt;
&lt;li&gt;&lt;a href="http://channel9.msdn.com/Events/TechEd/Europe/2010/DEV315"&gt;Watch the talk&lt;/a&gt; online&lt;/li&gt;
&lt;li&gt;Download &lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-AsyncFutureOfVBCSharp/4846.2010.11.11-_2D00_-Berlin-_2D00_-Future-of-VB-_5B00_DEV315_5D00_-_2D00_-lucian-wischik.pptx"&gt;slides.pptx&lt;/a&gt; [8mb]&lt;/li&gt;
&lt;li&gt;Download &lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-AsyncFutureOfVBCSharp/4431.2010.11.11-_2D00_-Berlin-_2D00_-Future-of-VB-_5B00_DEV315_5D00_-_2D00_-iterator-samples.zip"&gt;source_code.zip&lt;/a&gt; for iterators [3k, requires VS2010 SP1 and Async CTP]&lt;/li&gt;
&lt;li&gt;The audience in Berlin filled out 29 evals and so on on November 11th 2010 the charity "Medecins Sans Frontiers" got $290 half from me and half from Microsoft. Thank you, Berlin!&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-filesystemfile.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-AsyncFutureOfVBCSharp/5707.basel1.jpg"&gt;&lt;img style="max-height: 300px; max-width: 400px;" border="0" src="http://blogs.msdn.com/cfs-filesystemfile.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-AsyncFutureOfVBCSharp/5707.basel1.jpg" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-filesystemfile.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-AsyncFutureOfVBCSharp/4743.basel2.jpg"&gt;&lt;img style="max-height: 300px; max-width: 400px;" border="0" src="http://blogs.msdn.com/cfs-filesystemfile.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-AsyncFutureOfVBCSharp/4743.basel2.jpg" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-filesystemfile.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-AsyncFutureOfVBCSharp/3225.basel3.jpg"&gt;&lt;img style="max-height: 300px; max-width: 400px;" border="0" src="http://blogs.msdn.com/cfs-filesystemfile.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-AsyncFutureOfVBCSharp/3225.basel3.jpg" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-filesystemfile.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-AsyncFutureOfVBCSharp/0511.basel4.jpg"&gt;&lt;img style="max-height: 300px; max-width: 400px;" border="0" src="http://blogs.msdn.com/cfs-filesystemfile.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-06-AsyncFutureOfVBCSharp/0511.basel4.jpg" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10166815" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/lucian/archive/tags/async/">async</category><category domain="http://blogs.msdn.com/b/lucian/archive/tags/talk/">talk</category><category domain="http://blogs.msdn.com/b/lucian/archive/tags/iterators/">iterators</category></item><item><title>[placeholder] How to write a connected VB phone app</title><link>http://blogs.msdn.com/b/lucian/archive/2011/05/18/how-to-write-a-connected-vb-phone-app.aspx</link><pubDate>Wed, 18 May 2011 12:20:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10165795</guid><dc:creator>Lucian Wischik, MSFT</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/lucian/rsscomments.aspx?WeblogPostID=10165795</wfw:commentRss><comments>http://blogs.msdn.com/b/lucian/archive/2011/05/18/how-to-write-a-connected-vb-phone-app.aspx#comments</comments><description>&lt;p&gt;This blog&amp;nbsp;article has been moved and enhanced:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;a href="http://blogs.msdn.com/b/lucian/archive/2011/05/20/talk-how-to-write-an-async-connected-app-for-windows-phone-7.aspx"&gt;http://blogs.msdn.com/b/lucian/archive/2011/05/20/talk-how-to-write-an-async-connected-app-for-windows-phone-7.aspx&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10165795" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/lucian/archive/tags/async/">async</category></item><item><title>Async CTP Refresh - mitigation strategies</title><link>http://blogs.msdn.com/b/lucian/archive/2011/04/18/async-ctp-refresh-mitigation-strategies.aspx</link><pubDate>Mon, 18 Apr 2011 14:13:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10155180</guid><dc:creator>Lucian Wischik, MSFT</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/lucian/rsscomments.aspx?WeblogPostID=10155180</wfw:commentRss><comments>http://blogs.msdn.com/b/lucian/archive/2011/04/18/async-ctp-refresh-mitigation-strategies.aspx#comments</comments><description>&lt;h1&gt;Async CTP Refresh - mitigations when using the CTP&lt;/h1&gt;
&lt;p&gt;&amp;nbsp;The preceding four blog posts have outlined the known and unknown bug areas in the CTP. Now it's time to set out some concrete mitigation strategies.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;&amp;nbsp;&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Use F# instead&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;F# already shipped in VS2010 with Async support. It is tried, tested, high-quality, supported, and flexible. With F# you can go ahead writing Async code without even worrying about the CTP. F# is also a great language to learn in its own right.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Run PEVerify.exe and unit tests&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;Running PEVerify has caught about half of the codegen bugs we've found. You should definitely use it too.&lt;/p&gt;
&lt;p&gt;Hint: quickly launch the "VS Command Prompt (2010)" from the start menu by typing the word "Command" in the search box and picking it from the results.&lt;/p&gt;
&lt;p&gt;And as always, run unit tests. Most of the codegen bugs show up quite clearly even with superficial unit testing. The key bugs we found which didn't (especially the race-condition bugs) have been fixed in the CTP Refresh.&lt;/p&gt;
&lt;p&gt;&lt;img src="http://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/CommunityServer-Blogs-Components-WeblogFiles/00-00-01-12-06/5557.start.png" border="0" /&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src="http://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/CommunityServer-Blogs-Components-WeblogFiles/00-00-01-12-06/4162.command.png" border="0" /&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Be careful of synchronization context in async Windows Phone apps&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;There's a known issue on Windows Phone 7 such that any Await statements will always resume on the UI thread - even if you performed the Await on a background thread. In the CTP, the "AsyncBackgroundThread" sample shows a work-around for this issue.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;&amp;nbsp;&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Avoid complicated nested awaits&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;A "nested await" is an await expression that occurs as a sub-expression of another expression. A good rule of thumb is this: "If I &lt;i&gt;couldn't&lt;/i&gt; easily have rewritten my statement by pulling out all &lt;i&gt;awaits&lt;/i&gt; onto the preceding line, then the Async CTP might not have done it right either." For instance&lt;/p&gt;
&lt;p&gt;&lt;span style="line-height: 115%; font-family: Consolas; color: green; font-size: 9.5pt; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA;"&gt;// OK: SIMPLE OPERATORS IN VB+C#:&lt;br /&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; color: blue; font-size: 9.5pt; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA;"&gt;var&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA;"&gt; s = &lt;span style="color: blue;"&gt;await&lt;/span&gt; f() &amp;amp;&amp;amp; &lt;span style="color: blue;"&gt;await&lt;/span&gt; g();&lt;br /&gt;&lt;span style="color: green;"&gt;// is probably safe (albeit wrong order), since it can be trusted to behave like this:&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;var&lt;/span&gt; $temp1 = &lt;span style="color: blue;"&gt;await&lt;/span&gt; f();&lt;br /&gt;&lt;span style="color: blue;"&gt;var&lt;/span&gt; $temp2 = &lt;span style="color: blue;"&gt;await&lt;/span&gt; g();&lt;br /&gt;&lt;span style="color: blue;"&gt;var&lt;/span&gt; s = $temp1 &amp;amp;&amp;amp; $temp2;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: green;"&gt;' OK: USING CLAUSES IN VB+C#:&lt;/span&gt;&lt;br /&gt;&lt;span style="color: blue;"&gt;Using&lt;/span&gt; x = &lt;span style="color: blue;"&gt;Await&lt;/span&gt; g() : ... : &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Using&lt;/span&gt;&lt;br /&gt;&lt;span style="color: green;"&gt;' is probably safe, since it can be trusted to behave like this:&lt;/span&gt;&lt;br /&gt;&lt;span style="color: blue;"&gt;Dim&lt;/span&gt; $temp1 = &lt;span style="color: blue;"&gt;Await&lt;/span&gt; g()&lt;br /&gt;&lt;span style="color: blue;"&gt;Using&lt;/span&gt; x = $temp1 : ... : &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Using&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: green;"&gt;' USE WITH CARE, AND VERIFY+TEST: LOOPS IN VB:&lt;/span&gt;&lt;br /&gt;&lt;span style="color: blue;"&gt;While&lt;/span&gt; &lt;span style="color: blue;"&gt;Await&lt;/span&gt; g() : ... : &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;While&lt;/span&gt;&lt;br /&gt;&lt;span style="color: green;"&gt;' Note that it must have been more complicated than just a simple rewrite:&lt;/span&gt;&lt;br /&gt;&lt;span style="color: blue;"&gt;Dim&lt;/span&gt; $temp = &lt;span style="color: blue;"&gt;Await&lt;/span&gt; g()&lt;br /&gt;&lt;span style="color: blue;"&gt;While&lt;/span&gt; temp : ... : &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;While&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: green;"&gt;// DANGER: OTHER COMPLICATED IDIOMS IN VB+C#:&lt;/span&gt;&lt;br /&gt;f(&lt;span style="color: blue;"&gt;await&lt;/span&gt; g()).StructField.x = f(&lt;span style="color: blue;"&gt;await&lt;/span&gt; g()).m(&lt;span style="color: blue;"&gt;ref&lt;/span&gt; y, &lt;span style="color: blue;"&gt;await&lt;/span&gt; t);&lt;br /&gt;&lt;span style="color: green;"&gt;// Distrust this kind of code!&lt;/span&gt;&lt;br /&gt;&lt;span style="color: green;"&gt;// * Avoid "await" on the left hand side of an assignment&lt;/span&gt;&lt;br /&gt;&lt;span style="color: green;"&gt;// * Avoid combining awaits with ByRef arguments&lt;/span&gt;&lt;br /&gt;&lt;span style="color: green;"&gt;// * Avoid invoking methods on expressions involving awaits&lt;/span&gt;&lt;br /&gt;&lt;span style="color: green;"&gt;// * Avoid "await" inside array or object initializers&lt;/span&gt;&lt;br style="mso-special-character: line-break;" /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Miscellaneous&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;Be careful of For loops in VB, and also Select Case statements. Unit-test them thoroughly and PEVerify.&lt;/p&gt;
&lt;p&gt;In particular, non-constant "Steps" in For statements don't work. Thanks to MattMc3 for &lt;a href="http://social.msdn.microsoft.com/Forums/en-US/async/thread/10df3b37-4e52-4d36-bd13-15776c3e8587"&gt;reporting this&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10155180" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/lucian/archive/tags/async/">async</category></item><item><title>Async CTP Refresh - what bugs remain in it?</title><link>http://blogs.msdn.com/b/lucian/archive/2011/04/17/async-ctp-refresh-what-bugs-remain-in-it.aspx</link><pubDate>Sun, 17 Apr 2011 21:50:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10154955</guid><dc:creator>Lucian Wischik, MSFT</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/lucian/rsscomments.aspx?WeblogPostID=10154955</wfw:commentRss><comments>http://blogs.msdn.com/b/lucian/archive/2011/04/17/async-ctp-refresh-what-bugs-remain-in-it.aspx#comments</comments><description>&lt;h1&gt;Async CTP Refresh - what bugs remain in it?&lt;/h1&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Q. How many bugs remain in the Async CTP Refresh?&lt;/p&gt;
&lt;p&gt;A. That's hard to know. Our QA efforts have been dedicated to testing our async codebase that will be part of the next Visual Studio. We've spent far less time testing the CTP. There might be bugs in the CTP that were never visible in the real codebase. There might be (there &lt;i&gt;were&lt;/i&gt;)&lt;i&gt; &lt;/i&gt;bugs in the real codebase that never impacted the CTP.&lt;/p&gt;
&lt;p&gt;The best I can do is give you an indication of the asyncs compiler bugs we've found, and which aspects of the async feature they're associated with. From this you'll get an idea of which code patterns might be more exposed to bugs.&lt;/p&gt;
&lt;p&gt;Here's an overview of the &lt;i&gt;all&lt;/i&gt; the async bugs we've logged. The green ones have been fixed in the original Async CTP or in the CTP Refresh. All the rest have remain unaddressed in the CTP Refresh.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;img src="http://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/CommunityServer-Blogs-Components-WeblogFiles/00-00-01-12-06/4113.pie1.png" border="0" /&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Bad codegen bugs&lt;/h2&gt;
&lt;p&gt;The red segment of the pie chart is where our main codebase has, at some stage in development, produced bad codegen for certain async coding patterns, and hasn't been fixed in the CTP Refresh, and isn't in one of the main "unsupported-for-CTP" areas.&lt;/p&gt;
&lt;p&gt;All the bugs we've seen in the red category stem from &lt;i&gt;nested awaits&lt;/i&gt;. A 'nested await' is one that appears as a sub-expression of a larger expression. For instance,&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-family: Consolas; color: blue; font-size: 9.5pt;"&gt;var&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt; x = &lt;span style="color: blue;"&gt;await&lt;/span&gt; f() + &lt;span style="color: blue;"&gt;await&lt;/span&gt; g();&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The CTP uses an incorrect order of evaluation for nested awaits. In particular, &lt;i&gt;the CTP will first evaluate all Awaits in a given statement in order; once it has evaluated them all, then it will evaluate the remainder of the expression&lt;/i&gt;.&lt;/p&gt;
&lt;p&gt;For the final version of async, which will be shipped in the next Visual Studio, we had to make a major overhaul of the codegen for nested awaits to fix the order of evaluation.&lt;/p&gt;
&lt;p&gt;All of the bugs in this red category were associated in some way either with the CTP's treatment of nested awaits, or with the overhaul of them. Even some code patterns that don't use nested awaits (such as VB "Continue" out of a "Do / Loop Until") suffer bugs that come from the CTP's incorrect order of evaluation.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span style="line-height: 115%; font-family: Consolas; color: green; font-size: 9.5pt; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA;"&gt;' BUG: VB-only&amp;nbsp;bad behavior in async/iterator methods even without nested awaits.&lt;br /&gt;' SUGGESTION: use PEVerify and run unit-tests&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA;"&gt;&lt;br /&gt;&lt;span style="color: blue;"&gt;Do&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;If&lt;/span&gt; &lt;span style="color: blue;"&gt;True&lt;/span&gt; &lt;span style="color: blue;"&gt;Then&lt;/span&gt; &lt;span style="color: blue;"&gt;Continue Do&lt;br /&gt;Loop&lt;/span&gt; &lt;span style="color: blue;"&gt;Until&lt;/span&gt; &lt;span style="color: blue;"&gt;True&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: blue;"&gt;Dim&lt;/span&gt; i = 1&lt;br /&gt;&lt;span style="color: blue;"&gt;For&lt;/span&gt; i = 0 &lt;span style="color: blue;"&gt;To&lt;/span&gt; 100&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;.WriteLine(i)&lt;br /&gt;&lt;span style="color: blue;"&gt;Next&lt;br /&gt;&lt;br /&gt;&lt;span style="color: blue;"&gt;&lt;span style="line-height: 115%; font-family: Consolas; color: green; font-size: 9.5pt; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA;"&gt;' BUG: VB-only&amp;nbsp;For loops don't work if you have non-constant "steps"&lt;br /&gt;' SUGGESTION: Rewrite non-constant Steps into While loops.&lt;br /&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA;"&gt;&lt;span style="color: #008000;"&gt;' (Actually, this is the only bug that's not related to nested awaits...)&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;Dim&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;nbsp;s = -1&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;For&lt;/span&gt;&lt;span style="color: #000000;"&gt; i =&amp;nbsp;10 &lt;/span&gt;&lt;span style="color: blue;"&gt;To&lt;/span&gt;&lt;span style="color: #000000;"&gt; 1&amp;nbsp;&lt;span style="color: blue;"&gt;Step&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;nbsp;s&lt;/span&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;Yield &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;i&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;Next&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: green;"&gt;&lt;br /&gt;&lt;br /&gt;// BUG: VB and C# bugs calling MyBase in async method (especially with generic params)&lt;br /&gt;// SUGGESTION: use PEVerify to catch these&lt;/span&gt;&lt;br /&gt;&lt;span style="color: blue;"&gt;base&lt;/span&gt;.Foo&amp;lt;T&amp;gt;();&lt;br /&gt;&lt;span style="color: green;"&gt;&lt;br /&gt;&lt;br /&gt;' BUG: Nested awaits cause VB-only incorrect behavior in flow-control-constructs&lt;br /&gt;' SUGGESTION: Avoid nested awaits in these conditionals&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;Do&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515;"&gt;"a"&lt;/span&gt;)&lt;br /&gt;&lt;span style="color: blue;"&gt;Loop&lt;/span&gt; &lt;span style="color: blue;"&gt;Until&lt;/span&gt; &lt;span style="color: blue;"&gt;Await&lt;/span&gt; f()&lt;br /&gt;&lt;span style="color: blue;"&gt;&lt;br /&gt;Do&lt;/span&gt; &lt;span style="color: blue;"&gt;While&lt;/span&gt; &lt;span style="color: blue;"&gt;Await&lt;/span&gt; f()&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515;"&gt;"a"&lt;/span&gt;)&lt;br /&gt;&lt;span style="color: blue;"&gt;Loop&lt;br /&gt;&lt;br /&gt;If&lt;/span&gt; 11 = &lt;span style="color: blue;"&gt;Await&lt;/span&gt; f(10) &lt;span style="color: blue;"&gt;Then&lt;/span&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515;"&gt;"a"&lt;/span&gt;)&lt;br /&gt;&lt;span style="color: blue;"&gt;ElseIf&lt;/span&gt; 10 = &lt;span style="color: blue;"&gt;Await&lt;/span&gt; f(10) &lt;span style="color: blue;"&gt;Then&lt;/span&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515;"&gt;"b"&lt;/span&gt;)&lt;br /&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;If&lt;/span&gt;&lt;br /&gt;&lt;span style="color: green;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;For&lt;/span&gt; i = &lt;span style="color: blue;"&gt;Await&lt;/span&gt; f(1) &lt;span style="color: blue;"&gt;To&lt;/span&gt; &lt;span style="color: blue;"&gt;Await&lt;/span&gt; f(2) &lt;span style="color: blue;"&gt;Step&lt;/span&gt; &lt;span style="color: blue;"&gt;Await&lt;/span&gt; f(3)&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;.WriteLine(i)&lt;br /&gt;&lt;span style="color: blue;"&gt;Next&lt;/span&gt;&lt;br /&gt;&lt;span style="color: green;"&gt;&lt;br /&gt;&lt;br /&gt;' BUG: Nested awaits are dodgy in both VB and C# in these situations&lt;br /&gt;' SUGGESTION: use PEVerify and unit tests to check you&amp;rsquo;re getting right behavior&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;Dim&lt;/span&gt; x = &lt;span style="color: blue;"&gt;New&lt;/span&gt; &lt;span style="color: blue;"&gt;With&lt;/span&gt; {.A = &lt;span style="color: blue;"&gt;Await&lt;/span&gt; f(1)}&lt;br /&gt;&lt;br /&gt;&lt;span style="color: blue;"&gt;var&lt;/span&gt; c2 = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: blue;"&gt;int&lt;/span&gt;[] {&lt;span style="color: blue;"&gt;await&lt;/span&gt; f(1), &lt;span style="color: blue;"&gt;await&lt;/span&gt; f(2) };&lt;br /&gt;&lt;span style="color: green;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;Dim&lt;/span&gt; s = (&lt;span style="color: blue;"&gt;Await&lt;/span&gt; f()) &amp;amp; &lt;span style="color: blue;"&gt;Await&lt;/span&gt; f()&lt;br /&gt;&lt;br /&gt;h(&lt;span style="color: blue;"&gt;await&lt;/span&gt; f(1));&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="color: green;"&gt;// when passed to a &amp;ldquo;ParamArray&amp;rdquo; parameter of h()&lt;/span&gt;&lt;br style="mso-special-character: line-break;" /&gt;&lt;br style="mso-special-character: line-break;" /&gt;&lt;/span&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Bugs in language/IDE areas unsupported by the CTP&lt;/h2&gt;
&lt;p&gt;The lower segment of the above pie chart was about areas that are just not supported by the CTP. Here are those areas in detail. They're mostly easy to avoid.&lt;/p&gt;
&lt;p&gt;&lt;img src="http://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/CommunityServer-Blogs-Components-WeblogFiles/00-00-01-12-06/2117.pie2.png" border="0" /&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Contextual keywords&lt;/b&gt; - the final implementation of Async will treat "async/await" as contextual keywords in C#, and "Async/Await/Iterator/Yield" as contextual keywords in VB. We have had many bugs in this area, but none of them should affect the CTP - since they are all simply reserved words in the CTP.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Overload resolution&lt;/b&gt; - the final implementation of Async has a host of new rules for overload resolution and type inference. This was a difficult area to implement and test, and none of the changes have been back-ported to the CTP. When you pass an async or iterator lambda as an argument to a method, the CTP will usually fail to do the correct type inference, and will usually fail to pick the correct overload.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Late-bound await&lt;/b&gt; - the final implementation of Async will allow late-bound await, i.e. "Await Object" in VB and "await dynamic" in C#. We had a fair number of bugs in this area. The CTP simply gives an error on these cases, so bypassing all the bugs.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Await in unsafe code&lt;/b&gt; - the final implementation of Async will allow async methods to have unsafe code blocks, just so long as await is never itself in an unsafe context. None of this has been back-ported or tested in the CTP. You should avoid mixing await/async with unsafe in the CTP.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Await in expression lambdas&lt;/b&gt; - the final implementation of Async will allow expression lambdas. The CTP doesn't.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;IDE niceties&lt;/b&gt; - the final implementation of Async will have proper intellisense, quick-fixes, GenerateFromUsage, GoToDefinition and so on. None of these are present in the CTP.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Debugger&lt;/b&gt; - the final implementation of Async will have debugger support. It is absent in the CTP.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Nicer error messages&lt;/b&gt; - We filed many bugs and made lots of effort to improve the quality of error messages for erroneous uses of await. None of this work has been back-ported to the CTP, which remains stuck with rather poor error messages.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Bugs in illegal code&lt;/b&gt; - There are many places where the CTP compiler has not yet been hardened against erroneous uses of await. The good news is that if your code is correct, then you won't run into any of these bugs! The bad news is that you may inadvertently write erroneous code, and get VS crashes or PEVerify failures or incorrect codegen. Here are some of the erroneous code that triggers these bugs:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;incorrect implementation of the awaiter pattern, or one with constraints&lt;/li&gt;
&lt;li&gt;multiple or missing references to AsyncCtpLibrary.dll&lt;/li&gt;
&lt;li&gt;async constructors or async Main&lt;/li&gt;
&lt;li&gt;ByRef parameters in async/iterator methods&lt;/li&gt;
&lt;li&gt;On Error Goto or Resume in async methods&lt;/li&gt;
&lt;li&gt;async methods with __refvalue or __argiterator params&lt;/li&gt;
&lt;li&gt;await of restricted types such as argiterator/typereference&lt;/li&gt;
&lt;li&gt;use of await in attribute arguments&lt;/li&gt;
&lt;li&gt;use of yield/await in catch/finally blocks&lt;/li&gt;
&lt;li&gt;Also, devious-but-not-illegal implementation of the await pattern may trigger bugs:&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;span style="line-height: 115%; font-family: Consolas; color: green; font-size: 9.5pt; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA;"&gt;' BUG: VB and C#, awaitable pattern with partial/conditional/obsolete pattern methods&lt;br /&gt;' SUGGESTION: implement the pattern straightforwardly&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA;"&gt;&lt;br /&gt;&lt;span style="color: blue;"&gt;Dim&lt;/span&gt; t &lt;span style="color: blue;"&gt;As&lt;/span&gt; MyAwaitable&lt;br /&gt;&lt;span style="color: blue;"&gt;Await&lt;/span&gt; t&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Miscellaneous bugs and workitems&lt;/h2&gt;
&lt;p&gt;Here are the rest of the bugs and workitems. None of these affect the quality of code you produce using the Async CTP.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;img src="http://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/CommunityServer-Blogs-Components-WeblogFiles/00-00-01-12-06/7701.pie3.png" border="0" /&gt;&lt;/p&gt;
&lt;p&gt;The few IDE crashes were nebulous and not reproducible. If you run into any, &lt;i&gt;please&lt;/i&gt; let us know! The only ones we've been able to pin down were all reported too late to be fixed in the CTP:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;VB: Trying to do a late-bound await in a VBCore project&lt;/li&gt;
&lt;li&gt;VB: Making a task-returning async event handler&lt;/li&gt;
&lt;li&gt;C#: hovering over a named indexer that is awaited and then invoked in one expression&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;b&gt;&amp;nbsp;&lt;/b&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10154955" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/lucian/archive/tags/async/">async</category></item><item><title>Async CTP Refresh - compiler bug fixes</title><link>http://blogs.msdn.com/b/lucian/archive/2011/04/16/async-ctp-refresh-compiler-bug-fixes.aspx</link><pubDate>Sat, 16 Apr 2011 17:10:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10154803</guid><dc:creator>Lucian Wischik, MSFT</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/lucian/rsscomments.aspx?WeblogPostID=10154803</wfw:commentRss><comments>http://blogs.msdn.com/b/lucian/archive/2011/04/16/async-ctp-refresh-compiler-bug-fixes.aspx#comments</comments><description>&lt;h1&gt;Async CTP Refresh - Compiler Bug fixes &lt;/h1&gt;
&lt;p&gt;The big news about the Async CTP Refresh is that it enabled development on SP1 and for Windows Phone 7, and came with a new EULA. But there were also a few bug fixes...&lt;/p&gt;
&lt;p&gt;&lt;i&gt;We heard about some of these bugs from community feedback. Thank you. Such feedback is &lt;b&gt;enormously&lt;/b&gt; useful to us.&lt;/i&gt;&lt;/p&gt;
&lt;p&gt;This blog post lists the only compiler bugs fixed in the Async CTP Refresh. (In addition there were several library bugs fixed in the dataflow libraries). I want to spell out in detail some of these bugs we've found and fixed. This will give you an idea of how deeply we looked, and the kinds of bugs that arose. I also want to publically thank the users who alerted us to two of these bugs.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Fixed bug: a C# race condition in "finally" blocks&lt;/h2&gt;
&lt;p&gt;This bug was found by our QA department, and also by a user who emailed us directly, and also by the author of this blog: &lt;a href="http://fredrikeriksson.me/post/3276445803/a-subtle-race-condition-in-the-asyncctp"&gt;http://fredrikeriksson.me/post/3276445803/a-subtle-race-condition-in-the-asyncctp&lt;/a&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-family: Consolas; color: green; font-size: 9.5pt;"&gt;// Bug: in the first CTP, if you had high concurrency, then sometimes the finally&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; color: green; font-size: 9.5pt;"&gt;// block would not be executed, and sometimes it would be executed more than once&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; color: blue; font-size: 9.5pt;"&gt;try&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt; { &lt;span style="color: blue;"&gt;await&lt;/span&gt; e; }&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; color: blue; font-size: 9.5pt;"&gt;finally&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt; { &lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515;"&gt;"f"&lt;/span&gt;); }&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;/span&gt;&lt;/p&gt;
&lt;table cellpadding="0" cellspacing="0" border="1"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td width="319" valign="top"&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="line-height: 115%; font-family: Consolas; color: green; font-size: 9.5pt;"&gt;// The first CTP generated this:&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="line-height: 115%; font-family: Consolas; color: blue; font-size: 9.5pt;"&gt;&lt;br /&gt;class&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt; &lt;span style="color: #2b91af;"&gt;StateMachine&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt;{&lt;br /&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="background: yellow; color: blue; mso-highlight: yellow;"&gt;bool&lt;/span&gt;&lt;span style="background: yellow; mso-highlight: yellow;"&gt; $doFinallyBodies&lt;/span&gt;;&lt;br /&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt;&lt;o:p&gt;&amp;nbsp;&lt;br /&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;void&lt;/span&gt; MoveNext() {&lt;br /&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;$doFinallyBodies = &lt;span style="color: blue;"&gt;true&lt;/span&gt;;&lt;br /&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt;&lt;o:p&gt;&amp;nbsp;&lt;br /&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;try&lt;/span&gt; {&lt;br /&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;var&lt;/span&gt; $temp = e.GetAwaiter();&lt;br /&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;$doFinallyBodies = &lt;span style="color: blue;"&gt;false&lt;/span&gt;;&lt;br /&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;if&lt;/span&gt; (temp.BeginAwait(&lt;i style="mso-bidi-font-style: normal;"&gt;cont&lt;/i&gt;))&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;return&lt;/span&gt;;&lt;br /&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;i style="mso-bidi-font-style: normal;"&gt;cont&lt;/i&gt;:&lt;br /&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;$doFinallyBodies = &lt;span style="color: blue;"&gt;true&lt;/span&gt;;&lt;br /&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;$temp.EndAwait();&lt;br /&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;finally&lt;/span&gt; {&lt;br /&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;if&lt;/span&gt; ($doFinallyBodies) {&lt;br /&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515;"&gt;"f"&lt;/span&gt;);&lt;br /&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA;"&gt;}&lt;/span&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;td width="319" valign="top"&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="line-height: 115%; font-family: Consolas; color: green; font-size: 9.5pt;"&gt;// The CTP Refresh generates this:&lt;br /&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt;&lt;o:p&gt;&amp;nbsp;&lt;br /&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; color: blue; font-size: 9.5pt;"&gt;class&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt; &lt;span style="color: #2b91af;"&gt;StateMachine&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt;{&lt;br /&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt;&lt;o:p&gt;&amp;nbsp;&lt;br /&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt;&lt;o:p&gt;&amp;nbsp;&lt;br /&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;void&lt;/span&gt; MoveNext() {&lt;br /&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="background: yellow; color: blue; mso-highlight: yellow;"&gt;bool&lt;/span&gt;&lt;span style="background: yellow; mso-highlight: yellow;"&gt; $doFinallyBodies&lt;/span&gt; = &lt;span style="color: blue;"&gt;true&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; color: blue; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; color: blue; font-size: 9.5pt;"&gt;&lt;o:p&gt;&amp;nbsp;&lt;br /&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; color: blue; font-size: 9.5pt;"&gt;&lt;o:p&gt;&amp;nbsp;&lt;br /&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; color: blue; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;...&lt;br /&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; color: blue; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;...&lt;br /&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; color: blue; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;...&lt;br /&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt;&lt;o:p&gt;&amp;nbsp;&lt;br /&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt;&lt;o:p&gt;&amp;nbsp;&lt;br /&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt;&lt;o:p&gt;&amp;nbsp;&lt;br /&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt;&lt;o:p&gt;&amp;nbsp;&lt;br /&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt;&lt;o:p&gt;&amp;nbsp;&lt;br /&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt;&lt;o:p&gt;&amp;nbsp;&lt;br /&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt;&lt;o:p&gt;&amp;nbsp;&lt;br /&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt;&lt;o:p&gt;&amp;nbsp;&lt;br /&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt;&lt;o:p&gt;&amp;nbsp;&lt;br /&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA;"&gt;}&lt;/span&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&lt;b&gt;&amp;nbsp;&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;The field "$doFinallyBodies" is used so that, if we had to return early due to an "await" statement, then we'd bypass the finally bodies that the user had written. They should only be executed on normal exit.&lt;/p&gt;
&lt;p&gt;The bug was that by accident I'd made $doFinallyBodies&lt;i&gt; &lt;/i&gt;a field. It should have been a local. As a field, if there was high concurrency and lots of threads running the same async method, then one thread might overwrite the shared $doFinallyBodies at the wrong time.&lt;/p&gt;
&lt;p&gt;VB did not have this bug, since in VB it was already a local.&lt;i&gt;&lt;/i&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Fixed bug: another C#/VB race condition in "finally" blocks&lt;/h2&gt;
&lt;p&gt;This bug was found by user "Soonts" on StackOverflow, &lt;a href="http://stackoverflow.com/questions/5032784/async-ctp-and-finally"&gt;http://stackoverflow.com/questions/5032784/async-ctp-and-finally&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Consolas; color: green; font-size: 9.5pt;"&gt;' Bug: in the first CTP, this would print "1" then "finally".&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; color: green; font-size: 9.5pt;"&gt;' But it's supposed to print "finally" then "1".&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; color: blue; font-size: 9.5pt;"&gt;Sub&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt; Main()&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;.WriteLine(f().Result)&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; color: blue; font-size: 9.5pt;"&gt;End&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt; &lt;span style="color: blue;"&gt;Sub&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; color: blue; font-size: 9.5pt;"&gt;Async&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt; &lt;span style="color: blue;"&gt;Function&lt;/span&gt; f() &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Task&lt;/span&gt;(&lt;span style="color: blue;"&gt;Of&lt;/span&gt; &lt;span style="color: blue;"&gt;Integer&lt;/span&gt;)&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;Try&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;Await&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;TaskEx&lt;/span&gt;.Yield()&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;Return&lt;/span&gt; 1&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;Finally&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515;"&gt;"finally"&lt;/span&gt;)&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Try&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; color: blue; font-size: 9.5pt;"&gt;End&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt; &lt;span style="color: blue;"&gt;Function&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;/p&gt;
&lt;table cellpadding="0" cellspacing="0" border="1"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td width="319" valign="top"&gt;
&lt;p&gt;&lt;span style="line-height: 115%; font-family: Consolas; color: green; font-size: 9.5pt; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA;"&gt;' The first CTP generated this:&lt;br /&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA;"&gt;&lt;br /&gt;&lt;span style="color: blue;"&gt;Class&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;StateMachine&lt;/span&gt;&lt;br /&gt;&lt;span style="color: blue;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Dim&lt;/span&gt; $builder &lt;span style="color: blue;"&gt;As&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;AsyncTaskMethodBuilder&lt;/span&gt;(&lt;span style="color: blue;"&gt;Of&lt;/span&gt; &lt;span style="color: blue;"&gt;Integer&lt;/span&gt;)&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;Sub&lt;/span&gt; MoveNext()&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;Try&lt;/span&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;Await&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;TaskEx&lt;/span&gt;.Yield()&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;$builder.SetResult(1)&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;Catch&lt;/span&gt; ex &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Exception&lt;/span&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;$builder.SetException(ex)&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Try&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;br /&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;td width="319" valign="top"&gt;&lt;span style="line-height: 115%; font-family: Consolas; color: green; font-size: 9.5pt; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA;"&gt;' The CTP Refresh generates this:&lt;br /&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA;"&gt;&lt;br /&gt;&lt;span style="color: blue;"&gt;Class&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;StateMachine&lt;/span&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;Dim&lt;/span&gt; $builder &lt;span style="color: blue;"&gt;As&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;AsyncTaskMethodBuilder&lt;/span&gt;(&lt;span style="color: blue;"&gt;Of&lt;/span&gt; &lt;span style="color: blue;"&gt;Integer&lt;/span&gt;)&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;Sub&lt;/span&gt; MoveNext()&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;Dim&lt;/span&gt; &lt;span style="background: yellow; mso-highlight: yellow;"&gt;$ReturnVariable&lt;/span&gt; &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Integer&lt;/span&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;Try&lt;/span&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;Await&lt;/span&gt; TaskEx.Yield()&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="background: yellow; mso-highlight: yellow;"&gt;$ReturnVariable = 1&lt;/span&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="background: yellow; color: blue; mso-highlight: yellow;"&gt;GoTo&lt;/span&gt;&lt;span style="background: yellow; mso-highlight: yellow;"&gt; exit&lt;/span&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;Catch&lt;/span&gt; ex &lt;span style="color: blue;"&gt;As&lt;/span&gt; Exception&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;$builder.SetException(ex)&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="background: yellow; color: blue; mso-highlight: yellow;"&gt;Return&lt;/span&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Try&lt;/span&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="background: yellow; mso-highlight: yellow;"&gt;exit:&lt;/span&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;$builder.SetResult(ReturnVariable)&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;br /&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Fixed bug: a VB compiler bug with caught exception variables&lt;/h2&gt;
&lt;p&gt;Use of the exception variable would often have failed PEVerify, and in code like below where you're using the exception variable it would have caused a runtime exception. The root cause was that VB compiler generates IL with a temporary local variable for "ex". In an async method, the temporary local variable had to be lifted into the state machine.&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-family: Consolas; color: green; font-size: 9.5pt;"&gt;' Bug: in the first CTP,&amp;nbsp;runtime crashes in some uses of the caught exception variable.&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; color: green; font-size: 9.5pt;"&gt;' This has been fixed in the CTP Refresh&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; color: blue; font-size: 9.5pt;"&gt;Async&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; f()&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;Try&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;Throw&lt;/span&gt; &lt;span style="color: blue;"&gt;New&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Exception&lt;/span&gt;(&lt;span style="color: #a31515;"&gt;"test"&lt;/span&gt;)&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;Catch&lt;/span&gt; ex &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Exception&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;Dim&lt;/span&gt; lambda = &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;()&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;.WriteLine(ex) &lt;span style="color: green;"&gt;' crash right here&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;lambda()&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Try&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; color: blue; font-size: 9.5pt;"&gt;End&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;/p&gt;
&lt;h2&gt;Fixed bug: a VB compiler bug with Select statements in asyncs/iterators&lt;/h2&gt;
&lt;p&gt;Select Case statements in VB async+iterator methods had a chance of incorrect codegen, depending on the exact types used by the statement and the ranges of the Case clauses. It sometimes failed PEVerify, and more often just picked the wrong Case branch. Once again the problem was one of IL with temporary local variables, this time for the Select Case operand. This bug was hard to track down because VB does aggressive optimization on Select Case statements, and only certain optimizations would expose the bug.&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-family: Consolas; color: green; font-size: 9.5pt;"&gt;' Bug: in the first CTP, if invoked with "10", this would print ERROR&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; color: green; font-size: 9.5pt;"&gt;' This has been fixed in the CTP refresh&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; color: blue; font-size: 9.5pt;"&gt;Async&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; f(y &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Long&lt;/span&gt;)&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;Select&lt;/span&gt; &lt;span style="color: blue;"&gt;Case&lt;/span&gt; y&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;Case&lt;/span&gt; 8 &lt;span style="color: blue;"&gt;To&lt;/span&gt; 10&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515;"&gt;"SUCCESS"&lt;/span&gt;)&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;Case Else&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515;"&gt;"ERROR"&lt;/span&gt;)&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Select&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; color: blue; font-size: 9.5pt;"&gt;End&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/span&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;/p&gt;
&lt;h2&gt;Fixed bug: a VB compiler bug with late-bound For loops in async/iterators&lt;/h2&gt;
&lt;p&gt;Late-bound VB For loops in VB async+iterator methods would often have failed PEVerify and crashed at runtime. Once again the problem was one of IL with temporary local variables, this time for the upper bound of the For loop, and the fix was to lift it to be a field in the state machine. You might get the feeling that a lot of our bugs were due to the need to lift temporary local variables. Once we twigged onto this, we systematically tracked down every last case that needed it.&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-family: Consolas; color: green; font-size: 9.5pt;"&gt;' Bug: in the first CTP, late-bound For loop crashed&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; color: green; font-size: 9.5pt;"&gt;' This has been fixed in the CTP Refresh&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; color: blue; font-size: 9.5pt;"&gt;Async&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; f()&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;Dim&lt;/span&gt; x &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Object&lt;/span&gt; = 4&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;For&lt;/span&gt; i = 1 &lt;span style="color: blue;"&gt;To&lt;/span&gt; x&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;.WriteLine(i)&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;Next&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; color: blue; font-size: 9.5pt;"&gt;End&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;Note: on the Async Forums, user MattMc3 pointed&amp;nbsp;out a &lt;a href="http://social.msdn.microsoft.com/Forums/en-US/async/thread/10df3b37-4e52-4d36-bd13-15776c3e8587?prof=required"&gt;For loop bug that still exists&lt;/a&gt;: if the&amp;nbsp;For loop has a Step that isn't constant, then it won't work. The reason is that (sigh) the CTP fails to lift the "step variable temporary".&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;/p&gt;
&lt;h2&gt;Fixed bug: a VB compiler bug with type constraints in asyncs/iterators&lt;/h2&gt;
&lt;p&gt;Generic constraints in async/iterator methods would have caused problems if they referred to other generic parameters of the async method. This usually manifest as a VS crash; sometimes it failed PEVerify instead. The bug was because, when we lift the method into a state machine class, we have to turn the method's generic parameters into generic parameters of the state machine class, and we have to remember to rewrite the constraints accordingly.&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-family: Consolas; color: green; font-size: 9.5pt;"&gt;' Bug: in the first CTP, this could would sometimes crash the IDE.&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; color: green; font-size: 9.5pt;"&gt;' This has been fixed in the CTP Refresh&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; color: blue; font-size: 9.5pt;"&gt;Async&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; CreateListElement(&lt;span style="color: blue;"&gt;Of&lt;/span&gt; TElem, TList &lt;span style="color: blue;"&gt;As&lt;/span&gt; {&lt;span style="color: blue;"&gt;New&lt;/span&gt;, &lt;span style="color: #2b91af;"&gt;List&lt;/span&gt;(&lt;span style="color: blue;"&gt;Of&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;TElem&lt;/span&gt;)})()&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;Dim&lt;/span&gt; l2 &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;New&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;TList&lt;/span&gt;()&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; color: blue; font-size: 9.5pt;"&gt;End&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;/p&gt;
&lt;h2&gt;Fixed bug: a pre-existing C# bug with nullables&lt;/h2&gt;
&lt;p&gt;In C#, a method with nullable equality tests would sometimes evaluate the operands more than once. We seemed to hit this more in our async testing, but we discovered that it was caused by a pre-existing bug in VS2010, one that arose from optimizations in the handling of nullables. This has been fixed in the CTP Refresh.&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-family: Consolas; color: green; font-size: 9.5pt;"&gt;// Bug: in the first CTP, this would do incorrect codegen&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; color: blue; font-size: 9.5pt;"&gt;async&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; g() {&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Task&lt;/span&gt;&amp;lt;&lt;span style="color: blue;"&gt;bool&lt;/span&gt;&amp;gt; t = &lt;span style="color: blue;"&gt;null&lt;/span&gt;;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;bool&lt;/span&gt; b = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Nullable&lt;/span&gt;&amp;lt;&lt;span style="color: blue;"&gt;bool&lt;/span&gt;&amp;gt;(&lt;span style="color: blue;"&gt;true&lt;/span&gt;) == &lt;span style="color: blue;"&gt;await&lt;/span&gt; t;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; color: green; font-size: 9.5pt;"&gt;// Bug: in VS2010, this would print "f" twice.&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; color: green; font-size: 9.5pt;"&gt;// This has been fixed in the CTP Refresh&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; color: blue; font-size: 9.5pt;"&gt;void&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt; Test() {&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;bool&lt;/span&gt; b = (&lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Nullable&lt;/span&gt;&amp;lt;&lt;span style="color: blue;"&gt;bool&lt;/span&gt;&amp;gt;(&lt;span style="color: blue;"&gt;true&lt;/span&gt;)) == f();&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;.WriteLine(b);&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; color: blue; font-size: 9.5pt;"&gt;bool&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt; f() {&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515;"&gt;"f"&lt;/span&gt;); &lt;span style="color: blue;"&gt;return&lt;/span&gt; &lt;span style="color: blue;"&gt;true&lt;/span&gt;;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;/p&gt;
&lt;h2&gt;Fixed bug: a pre-existing C# bug using "base.property"&lt;/h2&gt;
&lt;p&gt;In C#, an async method which accessed "base.property" would cause incorrect codegen in the case where that property had different accessibility of its getter and setter. We discovered that this was caused by a pre-existing bug in VS2010, to do with the similar case of iterators. It would produce incorrect codegen, fail PEVerify, and sometimes throw an exception at runtime. The root cause of the bug is that both iterator and async methods must be lifted into a class. This class can no longer call any protected "base" method since it lacks accessibility. Instead it needs to generate a stub method which does have that accessibility. And the logic for deciding when that stub method was needed, failed to take into account the potential different visibility of setters and getters.&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-family: Consolas; color: green; font-size: 9.5pt;"&gt;// Bug: in the first CTP, both "f" and "g" would produce bad codegen&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; color: green; font-size: 9.5pt;"&gt;// that failed PEVerify. This has been fixed in the CTP Refresh.&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; color: blue; font-size: 9.5pt;"&gt;class&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt; &lt;span style="color: #2b91af;"&gt;C&lt;/span&gt; : &lt;span style="color: #2b91af;"&gt;B&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;{&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerator&lt;/span&gt; f() { &lt;span style="color: blue;"&gt;base&lt;/span&gt;.p = 1; &lt;span style="color: blue;"&gt;yield&lt;/span&gt; &lt;span style="color: blue;"&gt;break&lt;/span&gt;; }&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;async&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; g() { &lt;span style="color: blue;"&gt;base&lt;/span&gt;.p = 1; }&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; color: blue; font-size: 9.5pt;"&gt;class&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt; &lt;span style="color: #2b91af;"&gt;B&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;{&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;virtual&lt;/span&gt; &lt;span style="color: blue;"&gt;int&lt;/span&gt; p { &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;get&lt;/span&gt;; &lt;span style="color: blue;"&gt;set&lt;/span&gt;; }&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;/p&gt;
&lt;h2&gt;Fixed bug: VB would sometimes crash VS if you double-dotted&lt;/h2&gt;
&lt;p&gt;In the first CTP, in VB, if you had a variable whose type was inferred from an expression involving Await, and if you then double-dotted off this variable, it would crash VS.&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="font-family: Consolas; color: blue; font-size: 9.5pt;"&gt;Dim&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt; s = &lt;span style="color: blue;"&gt;Await&lt;/span&gt; (&lt;span style="color: blue;"&gt;New&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;WebClient&lt;/span&gt;).DownloadStringTaskAsync(&lt;span style="color: #a31515;"&gt;&lt;a href="http://a.com"&gt;http://a.com&lt;/a&gt;&lt;/span&gt;)&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: Consolas; color: blue; font-size: 9.5pt;"&gt;Dim&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt; len = &lt;span style="color: #2b91af;"&gt;s&lt;/span&gt;.ToUpper().Length &lt;span style="color: green;"&gt;' would crash VS in the first CTP&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;The reasons are pretty bizarre. When you dot off something, the intellisense engine needs to know the type. It finds the type by "keyhole analysis", i.e. only looking at the lines in question, without regard to the containing method. Indeed it didn't know that the containing method was async. There was a mismatch between intellisense (who therefore thought the Await was illegal) and the compiler (who knew it was legal). This mismatch caused the crash. The fix is for intellisense not to judge legality or otherwise of an Await.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10154803" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/lucian/archive/tags/async/">async</category></item><item><title>Async CTP Refresh - design changes</title><link>http://blogs.msdn.com/b/lucian/archive/2011/04/15/async-ctp-refresh-design-changes.aspx</link><pubDate>Fri, 15 Apr 2011 15:44:18 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10154525</guid><dc:creator>Lucian Wischik, MSFT</dc:creator><slash:comments>6</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/lucian/rsscomments.aspx?WeblogPostID=10154525</wfw:commentRss><comments>http://blogs.msdn.com/b/lucian/archive/2011/04/15/async-ctp-refresh-design-changes.aspx#comments</comments><description>&lt;h1&gt;Async CTP Refresh - Design Changes&lt;/h1&gt;
&lt;p&gt;The big news about the Async CTP Refresh is that it enabled development on SP1 and for Windows Phone 7, and came with a new EULA. But there were also a few design changes...&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Async is like the zombie virus&lt;/h2&gt;
&lt;p&gt;I've come to believe that async will be like the "zombie virus" - once it bites one part of your program, you'll be inclined make more of your program async just in case an operation takes time. But normally your async methods will complete immediately -- and we've made some design changes to help the performance of this &lt;i&gt;"fast path"&lt;/i&gt;.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Design change: new "await" pattern for greater efficiency&lt;/h2&gt;
&lt;p&gt;In the Async CTP Refresh, we changed the "await" pattern to make the fast path more efficient. This will affect anyone who made their own types awaitable. The following code is in VB, but it applies equally to C#.&lt;/p&gt;
&lt;p&gt;&lt;span style="line-height: 115%; font-family: Consolas; color: green; font-size: 9.5pt; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA;"&gt;' What pattern does the compiler use to implement an await expression?&lt;br /&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; color: blue; font-size: 9.5pt; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA;"&gt;Dim&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA;"&gt; i = &lt;span style="color: blue;"&gt;Await&lt;/span&gt; e&lt;/span&gt;&lt;/p&gt;
&lt;table cellpadding="0" cellspacing="0" border="1"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td width="319" valign="top"&gt;
&lt;p class="MsoNormal" style="margin: 0in 0in 10pt; mso-layout-grid-align: none;"&gt;&lt;span style="line-height: 115%; font-family: Consolas; color: green; font-size: 9.5pt;"&gt;' Old pattern, in first Async CTP&lt;br /&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; color: blue; font-size: 9.5pt;"&gt;&lt;br /&gt;Dim&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt; $temp = e.&lt;span style="background: yellow; mso-highlight: yellow;"&gt;GetAwaiter&lt;/span&gt;()&lt;br /&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt;SAVE_STATE()&lt;br /&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; color: blue; font-size: 9.5pt;"&gt;If&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt; $temp.&lt;span style="background: yellow; mso-highlight: yellow;"&gt;BeginAwait&lt;/span&gt;(&lt;span style="color: blue;"&gt;AddressOf&lt;/span&gt; &lt;i style="mso-bidi-font-style: normal;"&gt;cont&lt;/i&gt;) &lt;span style="color: blue;"&gt;Then&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Return&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; color: blue; font-size: 9.5pt;"&gt;End If&lt;br /&gt;&lt;/span&gt;&lt;i style="mso-bidi-font-style: normal;"&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt;cont&lt;/span&gt;&lt;/i&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt;:&lt;br /&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt;RESTORE_STATE()&lt;br /&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; color: blue; font-size: 9.5pt; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA;"&gt;Dim&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA;"&gt; i = $temp.&lt;span style="background: yellow; mso-highlight: yellow;"&gt;EndAwait&lt;/span&gt;()&lt;/span&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;td width="319" valign="top"&gt;
&lt;p class="MsoNormal" style="margin: 0in 0in 10pt; mso-layout-grid-align: none;"&gt;&lt;span style="line-height: 115%; font-family: Consolas; color: green; font-size: 9.5pt;"&gt;' New pattern, in Async CTP Refresh&lt;br /&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt;&lt;o:p&gt;&amp;nbsp;&lt;br /&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; color: blue; font-size: 9.5pt;"&gt;Dim&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt; $temp = e.&lt;span style="background: yellow; mso-highlight: yellow;"&gt;GetAwaiter&lt;/span&gt;()&lt;br /&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; color: blue; font-size: 9.5pt;"&gt;If&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt; &lt;span style="color: blue;"&gt;Not&lt;/span&gt; $temp.&lt;span style="background: yellow; mso-highlight: yellow;"&gt;IsCompleted&lt;/span&gt; &lt;span style="color: blue;"&gt;Then&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;SAVE_STATE()&lt;br /&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;$temp.&lt;span style="background: yellow; mso-highlight: yellow;"&gt;OnCompleted&lt;/span&gt;(&lt;span style="color: blue;"&gt;AddressOf&lt;/span&gt; &lt;i style="mso-bidi-font-style: normal;"&gt;cont&lt;/i&gt;)&lt;br /&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;Return&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;i style="mso-bidi-font-style: normal;"&gt;cont&lt;/i&gt;:&lt;br /&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;RESTORE_STATE()&lt;br /&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; color: blue; font-size: 9.5pt;"&gt;End&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt; &lt;span style="color: blue;"&gt;If&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; color: blue; font-size: 9.5pt; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA;"&gt;Dim&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA;"&gt; i = $temp.&lt;span style="background: yellow; mso-highlight: yellow;"&gt;GetResult&lt;/span&gt;()&lt;br /&gt;i = &lt;span style="color: blue;"&gt;Nothing&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;In the old pattern, it incurred the cost of SAVE_STATE() even if the task had already completed. We couldn't put SAVE_STATE() inside the "Then" clause, because of the off chance that &lt;i&gt;cont&lt;/i&gt; might be executed even before the "Then" clause had executed. In the new pattern we separated it out, so SAVE_STATE() and RESTORE_STATE() are never even executed in the "fast path".&lt;/p&gt;
&lt;p&gt;In the old pattern, it also incurred the cost of constructing a delegate (which I've written "&lt;i&gt;Addressof &amp;lt;label&amp;gt;&lt;/i&gt;" in the pseudo-code above), even if it wasn't needed. The new pattern will allow us to allocate that delegate lazily, potentially saving a heap allocation in the fast path. However, this is just potential for the future: we don't actually take advantage of it.&lt;/p&gt;
&lt;p&gt;In the old pattern, it also left the "awaiter" field hanging around. In the new pattern, we null it out -- the C# equivalent is "default(T)". This is so that the awaiter does not hold onto references for longer than necessary, which could harm garbage collection.&lt;/p&gt;
&lt;table cellpadding="0" cellspacing="0" border="1" class="MsoTableGrid" style="border-collapse: collapse; background: #ffffcc; mso-border-alt: solid windowtext .5pt; mso-yfti-tbllook: 1184; mso-padding-alt: 0in 5.4pt 0in 5.4pt;"&gt;
&lt;tbody&gt;
&lt;tr style="mso-yfti-irow: 0; mso-yfti-firstrow: yes; mso-yfti-lastrow: yes;"&gt;
&lt;td width="638" valign="top" style="padding-bottom: 0in; background-color: transparent; padding-left: 5.4pt; width: 6.65in; padding-right: 5.4pt; padding-top: 0in; mso-border-alt: solid windowtext .5pt; border: windowtext 1pt solid;"&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;&lt;b style="mso-bidi-font-weight: normal;"&gt;GUIDANCE&lt;/b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;If you are concerned about garbage collection in a long-running async method, you can &amp;ldquo;null out&amp;rdquo; your local variables. This is the same as what you can currently do in long-running non-async methods.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Taking advantage of async "fast path"&lt;/h2&gt;
&lt;p&gt;How might you take advantage of the async fast path? Here's some sample code, this time in C#. It uses the new method &lt;span style="color: #0000ff;"&gt;TaskEx&lt;/span&gt;.FromResult&amp;lt;T&amp;gt;() which was added in the CTP Refresh.&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"&gt;&lt;span style="font-family: Consolas; color: green; font-size: 9.5pt;"&gt;// This will use the "fast path" in the common case where the&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"&gt;&lt;span style="font-family: Consolas; color: green; font-size: 9.5pt;"&gt;// data is already available locally. It will avoid heap allocations&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"&gt;&lt;span style="font-family: Consolas; color: green; font-size: 9.5pt;"&gt;// in that case.&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"&gt;&lt;span style="font-family: Consolas; color: #2b91af; font-size: 9.5pt;"&gt;Database&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt; db;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"&gt;&lt;span style="font-family: Consolas; color: blue; font-size: 9.5pt;"&gt;while&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt; (&lt;span style="color: blue;"&gt;await&lt;/span&gt; db.MoveNextAsync())&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;{&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;.WriteLine(db.Current);&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;Thanks to the change, this "while" loop incurs only two small method calls (and no heap allocations) beyond what would be needed in a non-async alternative. The performance boost comes down to this implementation:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"&gt;&lt;span style="font-family: Consolas; color: blue; font-size: 9.5pt;"&gt;class&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt; &lt;span style="color: #2b91af;"&gt;Database&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;{&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;static&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Task&lt;/span&gt;&amp;lt;&lt;span style="color: blue;"&gt;bool&lt;/span&gt;&amp;gt; trueTask = &lt;span style="color: #2b91af;"&gt;TaskEx&lt;/span&gt;.FromResult(&lt;span style="color: blue;"&gt;true&lt;/span&gt;);&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;string&lt;/span&gt;[] currentBuffer = &lt;span style="color: blue;"&gt;null&lt;/span&gt;;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;int&lt;/span&gt; currentIndex = -1;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;string&lt;/span&gt; Current {&lt;span style="color: blue;"&gt;get&lt;/span&gt; {&lt;span style="color: blue;"&gt;return&lt;/span&gt; currentBuffer[currentIndex];}}&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Task&lt;/span&gt;&amp;lt;&lt;span style="color: blue;"&gt;bool&lt;/span&gt;&amp;gt; MoveNextAsync()&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;if&lt;/span&gt; (currentBuffer != &lt;span style="color: blue;"&gt;null&lt;/span&gt; &amp;amp;&amp;amp; currentIndex &amp;lt; currentBuffer.Length-1)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;currentIndex++;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="background: yellow; color: blue; mso-highlight: yellow;"&gt;return&lt;/span&gt;&lt;span style="background: yellow; mso-highlight: yellow;"&gt; trueTask;&lt;/span&gt; &lt;span style="color: green;"&gt;// avoids the cost of allocating a task&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;return&lt;/span&gt; MoveNextAsyncInternal();&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;async&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Task&lt;/span&gt;&amp;lt;&lt;span style="color: blue;"&gt;bool&lt;/span&gt;&amp;gt; MoveNextAsyncInternal()&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;currentBuffer = DownloadNextChunkFromDatabase();&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;if&lt;/span&gt; (currentBuffer == &lt;span style="color: blue;"&gt;null&lt;/span&gt;) &lt;span style="color: blue;"&gt;return&lt;/span&gt; &lt;span style="color: blue;"&gt;false&lt;/span&gt;;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;currentIndex = 0;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;return&lt;/span&gt; &lt;span style="color: blue;"&gt;true&lt;/span&gt;;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Costs of async... where even the "fast path" doesn't help&lt;/h2&gt;
&lt;p&gt;Even if the fast paths are taken everywhere, there are still some inherent costs in async:&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"&gt;&lt;span style="font-family: Consolas; color: green; font-size: 9.5pt;"&gt;' Compare the cost of these two statements.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"&gt;&lt;span style="font-family: Consolas; color: green; font-size: 9.5pt;"&gt;' The async one has some overhead...&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"&gt;&lt;span style="font-family: Consolas; color: blue; font-size: 9.5pt;"&gt;Dim&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt; i1 = f1()&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"&gt;&lt;span style="font-family: Consolas; color: blue; font-size: 9.5pt;"&gt;Dim&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt; i2 = &lt;span style="color: blue;"&gt;Await&lt;/span&gt; f2()&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"&gt;&lt;span style="font-family: Consolas; color: blue; font-size: 9.5pt;"&gt;Function&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt; f1() &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Integer&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;Return&lt;/span&gt; 1&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"&gt;&lt;span style="font-family: Consolas; color: blue; font-size: 9.5pt;"&gt;End&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt; &lt;span style="color: blue;"&gt;Function&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"&gt;&lt;span style="font-family: Consolas; color: blue; font-size: 9.5pt;"&gt;Async&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt; &lt;span style="color: blue;"&gt;Function&lt;/span&gt; f2() &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Task&lt;/span&gt;(&lt;span style="color: blue;"&gt;Of&lt;/span&gt; &lt;span style="color: blue;"&gt;Integer&lt;/span&gt;)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;Return&lt;/span&gt; 1&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"&gt;&lt;span style="font-family: Consolas; color: blue; font-size: 9.5pt;"&gt;End&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt; &lt;span style="color: blue;"&gt;Function&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The async call costs THREE extra heap allocations - one for the async state machine, one for its continuation delegate, and one for the resultant Task object.&lt;b&gt;&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;The async call costs several extra method calls to set up those objects and those fields.&lt;b&gt;&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;The async call costs additional IL instructions and an additional try/catch block within the body of the method.&lt;b&gt;&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;The return statement in the async case costs an additional method call.&lt;b&gt;&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;To await f2() costs two extra method calls.&lt;b&gt;&lt;/b&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;So how is it that the previous code managed to avoid most of those costs?&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"&gt;&lt;span style="font-family: Consolas; color: blue; font-size: 9.5pt;"&gt;while&lt;/span&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt; (&lt;span style="color: blue;"&gt;await&lt;/span&gt; db.MoveNextAsync()) {&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt; mso-layout-grid-align: none;"&gt;&lt;span style="font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;.WriteLine(db.Current);&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA;"&gt;}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Here "MoveNextAsync()" was not actually an async method, i.e. it didn't use the async modifier. And it returned a pre-allocated Task object instead of allocating a new one each time. In this way it bypasses most of the extra async costs. The only extra cost it incurs over and above the synchronous case is two extra method calls.&lt;b&gt;&lt;/b&gt;&lt;/p&gt;
&lt;table cellpadding="0" cellspacing="0" border="1" class="MsoTableGrid" style="border-collapse: collapse; background: #ffffcc; mso-border-alt: solid windowtext .5pt; mso-yfti-tbllook: 1184; mso-padding-alt: 0in 5.4pt 0in 5.4pt;"&gt;
&lt;tbody&gt;
&lt;tr style="mso-yfti-irow: 0; mso-yfti-firstrow: yes; mso-yfti-lastrow: yes;"&gt;
&lt;td width="638" valign="top" style="padding-bottom: 0in; background-color: transparent; padding-left: 5.4pt; width: 6.65in; padding-right: 5.4pt; padding-top: 0in; mso-border-alt: solid windowtext .5pt; border: windowtext 1pt solid;"&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;&lt;b style="mso-bidi-font-weight: normal;"&gt;GUIDANCE&lt;/b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;Create &amp;ldquo;chunky async&amp;rdquo; APIs rather than &amp;ldquo;fine-grained async&amp;rdquo; APIs. For instance, create APIs which asynchronously retrieve a batch of rows from the database in one call, rather than just one row at a time.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;The extra cost of async is negligible compared to the latency of network operations or UI operations. It is only ever worth thinking about in inner-loops or in server code where you&amp;rsquo;re optimizing for scalability. As always, measure performance before optimizing.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;Don&amp;rsquo;t make your code async just for the sake of it. Only do so for a reason, e.g. to avoid blocking the UI, or because the API calls you&amp;rsquo;re making are async, or to avoid consuming too many threads.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;As in the above case, the extra costs of consuming a &amp;ldquo;fast-path&amp;rdquo; async can be minimized in some situations. The extra costs of producing an async method cannot.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;In case of the zombie virus, it&amp;rsquo;s best to prepare an emergency kit beforehand. Include a shovel.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Design change: new exception behavior for "Async Subs"&lt;/h2&gt;
&lt;p&gt;We made another change because we wanted uniformity in the behavior of exceptions in Async Subs ("void-returning asyncs" in C#).&lt;/p&gt;
&lt;p&gt;&lt;i&gt;This hopefully won't affect anyone!&lt;/i&gt;&lt;/p&gt;
&lt;table cellpadding="0" cellspacing="0" border="1" class="MsoTableGrid" style="border-collapse: collapse; background: #ffffcc; mso-border-alt: solid windowtext .5pt; mso-yfti-tbllook: 1184; mso-padding-alt: 0in 5.4pt 0in 5.4pt;"&gt;
&lt;tbody&gt;
&lt;tr style="mso-yfti-irow: 0; mso-yfti-firstrow: yes; mso-yfti-lastrow: yes;"&gt;
&lt;td width="638" valign="top" style="padding-bottom: 0in; background-color: transparent; padding-left: 5.4pt; width: 6.65in; padding-right: 5.4pt; padding-top: 0in; mso-border-alt: solid windowtext .5pt; border: windowtext 1pt solid;"&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;&lt;b style="mso-bidi-font-weight: normal;"&gt;GUIDANCE&lt;/b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;It&amp;rsquo;s fine to use async methods that are Subs (void-returning asyncs) for top-level event handlers and the like. It&amp;rsquo;s okay for these to throw exceptions&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;It&amp;rsquo;s fine to use async methods that are Task-returning or Task&amp;lt;T&amp;gt;-returning, and have the suffix &amp;ldquo;Async&amp;rdquo;, for your normal async methods.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;o:p&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;As for other acceptable uses of async methods, there are a few niche cases where it makes sense to write &amp;ldquo;fire-and-forget&amp;rdquo; Async Subs which the caller is never able to await, but these should not throw exceptions that your program is intended to handle.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;For everyone who followed that guidance, the change in behavior of exception-throwing Async Subs won't have any effect. That's because the only exception-throwing Async Subs were the top-level event handlers.&lt;/p&gt;
&lt;table cellpadding="0" cellspacing="0" border="1"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td width="319" valign="top"&gt;
&lt;p&gt;&amp;nbsp;&lt;span style="line-height: 115%; font-family: Consolas; color: green; font-size: 9.5pt;"&gt;' First Async CTP: idiosyncratic exceptions&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; color: blue; font-size: 9.5pt;"&gt;Async&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; f()&lt;br /&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;Throw&lt;/span&gt; &lt;span style="color: blue;"&gt;New&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Exception&lt;/span&gt;(&lt;span style="color: #a31515;"&gt;"A"&lt;/span&gt;)&lt;br /&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;Await&lt;/span&gt; t&lt;br /&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;Throw&lt;/span&gt; &lt;span style="color: blue;"&gt;New&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Exception&lt;/span&gt;(&lt;span style="color: #a31515;"&gt;"B"&lt;/span&gt;)&lt;br /&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;Await&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;TaskEx&lt;/span&gt;.Yield()&lt;br /&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;Throw&lt;/span&gt; &lt;span style="color: blue;"&gt;New&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Exception&lt;/span&gt;(&lt;span style="color: #a31515;"&gt;"C"&lt;/span&gt;)&lt;br /&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; color: blue; font-size: 9.5pt;"&gt;End&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt; &lt;span style="color: blue;"&gt;Sub&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin: 0in 0in 10pt; mso-layout-grid-align: none;"&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin: 0in 0in 10pt; mso-layout-grid-align: none;"&gt;&lt;span style="line-height: 115%; font-family: Consolas; color: green; font-size: 9.5pt;"&gt;' [A] Always thrown to the caller of f()&lt;br /&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; color: green; font-size: 9.5pt;"&gt;' [B] Might be thrown to caller of f(),&lt;br /&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; color: green; font-size: 9.5pt;"&gt;' or maybe to the caller of the&lt;br /&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; color: green; font-size: 9.5pt;"&gt;' continuation-after-t (usually the UI&lt;br /&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; color: green; font-size: 9.5pt;"&gt;' message pump), depending on whether t&lt;br /&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; color: green; font-size: 9.5pt;"&gt;' took the fast path or not&lt;br /&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; color: green; font-size: 9.5pt;"&gt;' [C] Always thrown by whoever called the&lt;br /&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; color: green; font-size: 9.5pt;"&gt;' continuation-after-Yield&lt;/span&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;td width="319" valign="top"&gt;
&lt;p&gt;&amp;nbsp;&lt;span style="line-height: 115%; font-family: Consolas; color: green; font-size: 9.5pt;"&gt;' Async CTP Refresh: uniform exceptions&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; color: blue; font-size: 9.5pt;"&gt;Async&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; g()&lt;br /&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;Throw&lt;/span&gt; &lt;span style="color: blue;"&gt;New&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Exception&lt;/span&gt;(&lt;span style="color: #a31515;"&gt;"A"&lt;/span&gt;)&lt;br /&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;Await&lt;/span&gt; t&lt;br /&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;Throw&lt;/span&gt; &lt;span style="color: blue;"&gt;New&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Exception&lt;/span&gt;(&lt;span style="color: #a31515;"&gt;"B"&lt;/span&gt;)&lt;br /&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;Await&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;TaskEx&lt;/span&gt;.Yield()&lt;br /&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;Throw&lt;/span&gt; &lt;span style="color: blue;"&gt;New&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Exception&lt;/span&gt;(&lt;span style="color: #a31515;"&gt;"C"&lt;/span&gt;)&lt;br /&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; color: blue; font-size: 9.5pt;"&gt;End&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt; &lt;span style="color: blue;"&gt;Sub&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin: 0in 0in 10pt; mso-layout-grid-align: none;"&gt;&lt;span style="line-height: 115%; font-family: Consolas; font-size: 9.5pt;"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin: 0in 0in 10pt; mso-layout-grid-align: none;"&gt;&lt;span style="line-height: 115%; font-family: Consolas; color: green; font-size: 9.5pt;"&gt;' [A] Thrown to the caller's Sync.Context&lt;br /&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; color: green; font-size: 9.5pt;"&gt;' [B] Thrown to the caller's Sync.Context&lt;br /&gt;&lt;/span&gt;&lt;span style="line-height: 115%; font-family: Consolas; color: green; font-size: 9.5pt; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA;"&gt;' [C] Thrown to the caller's Sync.Context&lt;/span&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td width="319" valign="top"&gt;
&lt;p&gt;Implementation in first Async CTP:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;At the start of the async method, the compiler implicitly generates a call to&lt;br /&gt;&lt;b&gt;System.Runtime.CompilerServices.&lt;br /&gt;VoidAsyncMethodBuilder.Create()&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;This saves the current &lt;b&gt;SynchronizationContext.Current&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;It then calls &lt;b&gt;sc.OperationStarted()&lt;br /&gt;&lt;br /&gt;&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;When the method completes normally, the compiler implicitly generates a call to&lt;br /&gt;&lt;b&gt;builder.SetCompleted()&lt;/b&gt;.&lt;br /&gt;This calls &lt;b&gt;sc.OperationCompleted()&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;If the method completed due to an exception, the compiler implicitly generates a call to&lt;br /&gt;&lt;b&gt;builder.SetCompleted().&lt;br /&gt;&lt;/b&gt;This calls &lt;b&gt;sc.OperationCompleted()&lt;/b&gt;.&lt;br /&gt;Next, the compiler lets the exception propagate up the callstack.&lt;/li&gt;
&lt;/ol&gt;&lt;/td&gt;
&lt;td width="319" valign="top"&gt;
&lt;p&gt;Implementation in CTP Refresh:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;At the start of the async method, the compiler implicitly generates a call to&lt;br /&gt;&lt;b&gt;System.Runtime.CompilerServices.&lt;br /&gt;AsyncVoidMethodBuilder.Create()&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;This saves the current&lt;br /&gt;&lt;b&gt;SynchronizationContext.Current&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;It then calls &lt;b&gt;&lt;span style="background-color: #ffff00;"&gt;sc.OperationStarted()&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;When the method completes normally, the compiler implicitly generates a call to&lt;br /&gt;&lt;b&gt;builder.SetCompleted()&lt;/b&gt;.&lt;br /&gt;This calls &lt;b&gt;&lt;span style="background-color: #ffff00;"&gt;sc.OperationCompleted()&lt;/span&gt;&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;If the method completed due to an exception, the compiler implicitly generates a call to&lt;br /&gt;&lt;b&gt;builder.SetException(ex)&lt;/b&gt;.&lt;br /&gt;This calls &lt;b&gt;&lt;span style="background-color: #ffff00;"&gt;sc.OperationCompleted()&lt;/span&gt;&lt;/b&gt;.&lt;br /&gt;It then does &lt;b&gt;&lt;span style="background-color: #ffff00;"&gt;sc.Post&lt;/span&gt;( () =&amp;gt;&lt;span style="background-color: #ffff00;"&gt; throw ex &lt;/span&gt;)&lt;/b&gt;.&lt;/li&gt;
&lt;/ol&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10154525" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/lucian/archive/tags/async/">async</category></item><item><title>Async CTP Refresh - its state and direction</title><link>http://blogs.msdn.com/b/lucian/archive/2011/04/14/async-ctp-refresh-its-state-and-direction.aspx</link><pubDate>Thu, 14 Apr 2011 16:31:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10153994</guid><dc:creator>Lucian Wischik, MSFT</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/lucian/rsscomments.aspx?WeblogPostID=10153994</wfw:commentRss><comments>http://blogs.msdn.com/b/lucian/archive/2011/04/14/async-ctp-refresh-its-state-and-direction.aspx#comments</comments><description>&lt;h1&gt;Async CTP Refresh - its state and direction&lt;/h1&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;We really appreciate everyone who has installed the Async CTP. It has given us tremendous feedback. We appreciate all the discussion and bug reports that have come from users. We made the CTP uninstallable to encourage people to test it.&lt;/p&gt;
&lt;p&gt;But this is just a CTP. You install it, use it and deploy apps with it &lt;i&gt;at your own&lt;/i&gt; risk. This and the following four blog posts will give you raw information to help you make an informed judgment about the state of the CTP.&lt;/p&gt;
&lt;p&gt;We do not provide support for the Async CTP. We wanted to get the CTP out early, and so haven't held it up to the same quality standards as we will for the final polished release of async that's coming in the next version of Visual Studio. But I'd like to point out that Microsoft has &lt;i&gt;already&lt;/i&gt; released a polished version of "Async"...&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;b&gt;&lt;span style="background-color: #ffff00;"&gt;F# has already shipped with Async in Visual Studio 2010.&lt;br /&gt;It is high quality, robust, tested, flexible, and supported by Microsoft.&lt;/span&gt;&lt;br /&gt;&lt;/b&gt;I personally think F# is a great language -- ahead of the curve, so that if you learn F# then you'll be a couple of years ahead of your peers. Indeed, F# introduced Async first.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Q. What issues are there in the Async CTP?&lt;/h2&gt;
&lt;p&gt;We've discovered close to 400 bugs in async since the first CTP, thanks to feedback from early adopters and thanks to the phenomenal testers in the MS Languages QA team. I'll analyze those 400 bugs over the next four days:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;b&gt;&lt;a href="http://blogs.msdn.com/b/lucian/archive/2011/04/15/async-ctp-refresh-design-changes.aspx"&gt;Design changes in the Async CTP Refresh&lt;/a&gt;&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;&lt;b&gt;&lt;a href="http://blogs.msdn.com/b/lucian/archive/2011/04/16/async-ctp-refresh-compiler-bug-fixes.aspx"&gt;Bugs fixed in the Async CTP Refresh&lt;/a&gt;&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;&lt;b&gt;&lt;a href="http://blogs.msdn.com/b/lucian/archive/2011/04/17/async-ctp-refresh-what-bugs-remain-in-it.aspx"&gt;Known and unknown bugs in the Async CTP Refresh&lt;/a&gt;&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;&lt;b&gt;&lt;a href="http://blogs.msdn.com/b/lucian/archive/2011/04/18/async-ctp-refresh-mitigation-strategies.aspx"&gt;Mitigations when using the Async CTP Refresh&lt;/a&gt;&lt;/b&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&amp;nbsp;The first Async CTP was an early branch of our main development code base. We have spent all our bug-fixing work on that main codebase, which will eventually ship with the next Visual Studio.&lt;/p&gt;
&lt;p&gt;We have back-ported only a handful of bug-fixes from the main codebase back into the Async CTP Refresh - mainly ones to do with quality of code-generation.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Q. Why not just release a supported out-of-band Async feature already?&lt;br /&gt;Q. Why delay it until the next release of Visual Studio?&lt;br /&gt;Q. Why not put &lt;em&gt;all&lt;/em&gt; the bug-fixes into the CTP Refresh?&lt;/h2&gt;
&lt;p&gt;Async is a &lt;i&gt;huge&lt;/i&gt; feature! I think it will change the way most people program. We have to do it right, to make it consumable by everyone rather than just early-adopters. Here's some of what has to happen:&lt;/p&gt;
&lt;p&gt;&lt;b&gt;1. Language support in C# and VB&lt;br /&gt;&lt;/b&gt;The CTP only contains about half of the language support. It doesn't yet support proper overload resolution with async lambdas, nor lambda type inference, nor async expression lambdas, nor late-bound await.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;2. IDE support&lt;br /&gt;&lt;/b&gt;Debugger support, GenerateFromUsage, Intellisense - all are absent from the CTP&lt;/p&gt;
&lt;p&gt;&lt;b&gt;3. Adding async APIs to the .NET framework&lt;br /&gt;&lt;/b&gt;The CTP threw together a bunch of extension methods to make existing async APIs support the language feature, but we need to properly engineer all of them, and think about new APIs that should have async versions.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;4. Bringing the support libraries to other platforms, such as Windows Phone 7 and Silverlight&lt;br /&gt;&lt;/b&gt;The CTP had a provisional AsyncCtpLibrary_Silverlight.dll, and the CTP Refresh has a provisional AsyncCtpLibrary_Phone.dll, but these are not in a final state. We can make them faster and cleaner if we engineer them properly.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;5. Making ASP work with async&lt;br /&gt;&lt;/b&gt;The CTP has an ASP async sample, but we think we can bake async support more powerfully into ASP.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;6. Unit-testing&lt;br /&gt;&lt;/b&gt;The CTP Refresh has a unit-testing sample, but we've put it out as an exploration of what should be done - not the final answer&lt;/p&gt;
&lt;p&gt;&lt;b&gt;7. Inventing the new design patterns for async programming, and guidance&lt;br /&gt;&lt;/b&gt;The new style of async programming, from F# and now C#/VB, has not been used widely before. We think it will become ubiquitous. But no one yet has the programming maturity to know what are the best design practices around async. It'll take Microsoft and the wider community both time and experience before we figure out how best to use async. We can't be sure we've got the feature right until we've got more real-world experience. And we'd never want to commit something into the C#/VB languages until we're sure it's right.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;In all, to do Async right we need wholesale work on all of VS and on all of .NET. It can never be done just by a CTP. It is the kind of disruptive change that requires a whole new release of VS.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Q. Will my Async CTP code still work the same when the next VS is released and I upgrade to it?&lt;/h2&gt;
&lt;p&gt;&lt;i&gt;NO!&lt;/i&gt;&amp;nbsp; Your current Async CTP Refresh code &lt;i&gt;will require changes&lt;/i&gt; when the next VS is released and you upgrade to it... Here are the chief differences:&lt;/p&gt;
&lt;p&gt;&lt;b&gt;TaskEx class&lt;br /&gt;&lt;/b&gt;In the Async CTP you have to use &lt;span style="color: #33cccc;"&gt;TaskEx&lt;/span&gt; everywhere. In the final version, we'll be rolling all these things into the main &lt;span style="color: #33cccc;"&gt;Task&lt;/span&gt; class.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Overload resolution and type inference&lt;br /&gt;&lt;/b&gt;The Async CTP does not implement overload resolution of async lambdas properly, nor type inference. If you pass an async lambda then it might pick different overloads when you upgrade to the next VS. This was a real issue for us in the CTP where we had to separate &lt;span style="color: #33cccc;"&gt;TaskEx&lt;/span&gt;.Run and &lt;span style="color: #33cccc;"&gt;TaskEx&lt;/span&gt;.RunEx to work around the overload-resolution limitations.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Order of evaluation&lt;/b&gt;&lt;br /&gt;The Async CTP has the wrong order of evaluation for compound expressions. In particular, it evaluates all await expressions in a statement first, and then execute the rest of the statement afterwards. Thus, "&lt;span style="color: #0000ff;"&gt;if&lt;/span&gt; (f() &lt;span style="color: #0000ff;"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;await&lt;/span&gt; g())" in the CTP will always evaluate g() and await it first, even if f() returns &lt;span style="color: #0000ff;"&gt;false&lt;/span&gt;. The next version of VS will have the correct order of evaluation. To make this fix we actually had to overhaul a large part of the codegen side of the compilers. We thought this too risky, at this stage,&amp;nbsp;to back-port to the CTP Refresh.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Extension-method APIs&lt;br /&gt;&lt;/b&gt;The Async CTP uses extension methods to provide async versions of the .NET APIs. The next release of VS will have them as instance methods. Overload resolution rules treats extension methods differently from instance methods, and so the next release may&amp;nbsp;cause subtle differences in overload resolution.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Precedence&lt;/b&gt;&lt;br /&gt;The Async CTP has the wrong precedence for the &lt;span style="color: #0000ff;"&gt;Await&lt;/span&gt; operator in VB. It should have highest precedence, but actually has lowest. For instance, "&lt;span style="color: #0000ff;"&gt;Await&lt;/span&gt; e + &lt;span style="color: #0000ff;"&gt;Await&lt;/span&gt; f" is parsed in the CTP as if it&amp;nbsp;were "&lt;span style="color: #0000ff;"&gt;Await&lt;/span&gt; (e + &lt;span style="color: #0000ff;"&gt;Await&lt;/span&gt; f)," but&amp;nbsp;in the final release it will parse as&amp;nbsp;"(&lt;span style="color: #0000ff;"&gt;Await&lt;/span&gt; e) + (&lt;span style="color: #0000ff;"&gt;Await&lt;/span&gt; f)" .&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10153994" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/lucian/archive/tags/async/">async</category></item></channel></rss>