<?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>C# Frequently Asked Questions</title><link>http://blogs.msdn.com/b/csharpfaq/</link><description>The C# team posts answers to common questions and describes new language features</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>Microsoft "Roslyn" September 2012 CTP now available</title><link>http://blogs.msdn.com/b/csharpfaq/archive/2012/09/17/microsoft-quot-roslyn-quot-september-2012-ctp-now-available.aspx</link><pubDate>Mon, 17 Sep 2012 16:02:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10350136</guid><dc:creator>CSharpFAQ</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/csharpfaq/rsscomments.aspx?WeblogPostID=10350136</wfw:commentRss><comments>http://blogs.msdn.com/b/csharpfaq/archive/2012/09/17/microsoft-quot-roslyn-quot-september-2012-ctp-now-available.aspx#comments</comments><description>&lt;p&gt;Following last Wednesday&amp;rsquo;s &lt;a href="http://msdn.com/vstudio"&gt;official launch&lt;/a&gt; of Visual Studio 2012, we&amp;rsquo;re excited to announce that the Roslyn September 2012 CTP is now &lt;a href="http://msdn.com/roslyn"&gt;available&lt;/a&gt; for &lt;a href="http://go.microsoft.com/fwlink/?LinkID=228400"&gt;download&lt;/a&gt; and provides support for VS 2012 RTM. Please note that Visual Studio 2010 is no longer supported by this CTP.&lt;/p&gt;
&lt;p&gt;We&amp;rsquo;ve been hard at work since the &lt;a href="http://blogs.msdn.com/b/somasegar/archive/2011/10/19/roslyn-ctp-available-now.aspx"&gt;first public release of Roslyn&lt;/a&gt; adding support for &lt;a href="http://go.microsoft.com/fwlink/?LinkID=254240&amp;amp;clcid=0x409"&gt;new language features&lt;/a&gt; and improving our APIs. In addition, we&amp;rsquo;ve also updated our &lt;a href="http://nuget.org/packages/Roslyn"&gt;NuGet packages&lt;/a&gt; to match the September 2012 CTP.&lt;/p&gt;
&lt;p&gt;As always, your feedback is critical! Please get involved in the discussion on the &lt;a href="http://social.msdn.microsoft.com/forums/en-us/roslyn"&gt;Roslyn forum&lt;/a&gt;, file bugs on &lt;a href="https://connect.microsoft.com/VisualStudio"&gt;Connect&lt;/a&gt;, or just give us a shout-out on Twitter @ &lt;a href="http://twitter.com/#!/search/realtime/%23RoslynCTP"&gt;#RoslynCTP&lt;/a&gt;!&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Enjoy!&lt;/p&gt;
&lt;p&gt;Dustin Campbell&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;About the Author&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://twitter.com/dcampbell"&gt;Dustin&amp;nbsp;Campbell&lt;/a&gt; is a Program Manager responsible for the C# and VB language experiences on the Roslyn project.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10350136" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/IDE/">IDE</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/C_2300_/">C#</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/C_2300_+compiler/">C# compiler</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/roslyn/">roslyn</category></item><item><title>Understanding a Simple Async Program</title><link>http://blogs.msdn.com/b/csharpfaq/archive/2012/06/26/understanding-a-simple-async-program.aspx</link><pubDate>Tue, 26 Jun 2012 16:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10323000</guid><dc:creator>CSharpFAQ</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/csharpfaq/rsscomments.aspx?WeblogPostID=10323000</wfw:commentRss><comments>http://blogs.msdn.com/b/csharpfaq/archive/2012/06/26/understanding-a-simple-async-program.aspx#comments</comments><description>&lt;p&gt;&lt;em&gt;by Alan Berman&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;The &lt;a href="http://msdn.microsoft.com/en-us/library/hh191443(VS.110).aspx"&gt;Async&lt;/a&gt; feature in Visual Studio 2012 RC makes it easy to invoke asynchronous methods.&lt;/p&gt;
&lt;p&gt;If you mark a method with or &lt;a href="http://msdn.microsoft.com/en-us/library/hh156513(v=vs.110)"&gt;async&lt;/a&gt; modifier, you can use the &lt;a href="http://msdn.microsoft.com/en-us/library/hh156528(v=vs.110).aspx"&gt;await&lt;/a&gt; operator in the method. When control reaches an await expression in the async method, control returns to the caller, and progress in the method is suspended until the awaited task completes. When the task is complete, execution can resume in the method.&lt;/p&gt;
&lt;p&gt;Using asynchronous methods instead of synchronous methods can provide benefits.&amp;nbsp; Asynchrony makes UI applications more responsive because the UI thread that launches that operation can perform other work.&amp;nbsp; Asynchrony also improves the scalability of server-based application by reducing the need for threads.&lt;/p&gt;
&lt;p&gt;This blog provides a simple async example and describes what occurs when it runs. It also provides an example of exception handling for a call to an async method.&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;&lt;strong&gt;Running the Examples&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;To run the examples in this blog, you can do the following:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Install Visual Studio 2012 RC, which can be accessed from &lt;a href="http://msdn.microsoft.com/en-US/vstudio/async"&gt;Visual Studio Asynchronous Programming&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Create &lt;strong&gt;WPF Application&lt;/strong&gt; or &lt;strong&gt;Windows Forms Application&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;In your project, add a reference to &lt;strong&gt;System.Net.Http&lt;/strong&gt;.&amp;nbsp; This allows you to use the &lt;a href="http://msdn.microsoft.com/en-us/library/hh193681(v=vs.110)"&gt;HttpClient&lt;/a&gt; class in the first example,&lt;/li&gt;
&lt;li&gt;Add a &lt;strong&gt;Button&lt;/strong&gt; to the application.&amp;nbsp; Modify the Button_Click event handler to add the &lt;a href="http://msdn.microsoft.com/en-us/library/hh156513(v=vs.110)"&gt;async&lt;/a&gt; modifier.&amp;nbsp; The Button_Click event handlers in the examples are from a &lt;strong&gt;WPF Application&lt;/strong&gt;, however they can be modified for a &lt;strong&gt;Windows Form Application&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Include the following &lt;strong&gt;using&lt;/strong&gt; statements.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Consolas;" face="Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; using System.Diagnostics; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; using System.Net.Http; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; using System.Threading.Tasks;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Example of async and await&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The following example illustrates use of the &lt;a href="http://msdn.microsoft.com/en-us/library/hh156513(v=vs.110)"&gt;async&lt;/a&gt; modifier and the &lt;a href="http://msdn.microsoft.com/en-us/library/hh156528(v=vs.110).aspx"&gt;await&lt;/a&gt; operator.&amp;nbsp; An explanation is provided below.&amp;nbsp; Note that startButton_Click is marked with the &lt;a href="http://msdn.microsoft.com/en-us/library/hh156513(v=vs.110)"&gt;async&lt;/a&gt; modifier&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;&lt;span style="font-family: Consolas;" face="Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; private async void startButton_Click(object sender, RoutedEventArgs e) &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; { &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Task&amp;lt;string&amp;gt; getWebPageTask = GetWebPageAsync("&lt;/span&gt;&lt;a href="http://msdn.microsoft.com&amp;quot;);"&gt;&lt;span style="font-family: Consolas;" face="Consolas"&gt;http://msdn.microsoft.com");&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Consolas;" face="Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Debug.WriteLine("In startButton_Click before await"); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; string webText = await getWebPageTask; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Debug.WriteLine("Characters received: " + webText.Length.ToString()); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Consolas;" face="Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; private async Task&amp;lt;string&amp;gt; GetWebPageAsync(string url) &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; { &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Start an async task. &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Task&amp;lt;string&amp;gt; getStringTask = (new HttpClient()).GetStringAsync(url);&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Consolas;" face="Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Await the task. This is what happens: &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // 1. Execution immediately returns to the calling method, returning a &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //&amp;nbsp;&amp;nbsp;&amp;nbsp; different task from the task created in the previous statement. &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //&amp;nbsp;&amp;nbsp;&amp;nbsp; Execution in this method is suspended. &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // 2. When the task created in the previous statement completes, the &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //&amp;nbsp;&amp;nbsp;&amp;nbsp; result from the GetStringAsync method is produced by the Await &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //&amp;nbsp;&amp;nbsp;&amp;nbsp; statement, and execution continues within this method. &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Debug.WriteLine("In GetWebPageAsync before await"); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; string webText = await getStringTask; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Debug.WriteLine("In GetWebPageAsync after await");&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Consolas;" face="Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return webText; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Consolas;" face="Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Output: &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; //&amp;nbsp;&amp;nbsp; In GetWebPageAsync before await &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; //&amp;nbsp;&amp;nbsp; In startButton_Click before await &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; //&amp;nbsp;&amp;nbsp; In GetWebPageAsync after await &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; //&amp;nbsp;&amp;nbsp; Characters received: 44306&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;&lt;strong&gt;Control Flow in Example&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;In the above example, the startButton_Click method calls the GetWebPageAsync method.&amp;nbsp; In the GetWebPageAsync method, the following occurs:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The GetWebPageAsync method calls the &lt;a href="http://msdn.microsoft.com/en-us/library/system.net.http.httpclient.getstringasync(v=vs.110)"&gt;GetStringAsync&lt;/a&gt; method, which returns a task.&lt;/li&gt;
&lt;li&gt;The &amp;ldquo;await getStringTask&amp;rdquo; expression causes the GetWebPageAsync method to immediately exit and return a different task.&amp;nbsp; Execution in the GetWebPageAsync method is suspended.&lt;/li&gt;
&lt;li&gt;When the awaited task (getStringTask) completes at a later time, processing resumes after the await statement in the GetWebPageAsync method.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The startButton_Click method also contains an await expression, which is &amp;ldquo;await getWebPageTask&amp;rdquo;.&amp;nbsp; Because GetWebPageAsync is an async method, the task for the call to GetWebPageAsync needs to be awaited.&amp;nbsp; startButton_Click needs to be defined with the async modifier because it has an await expression.&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;&lt;strong&gt;Return Types in Example&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;In the above example, the GetWebPageAsync method has a &lt;strong&gt;return&lt;/strong&gt; statement that returns a &lt;strong&gt;string&lt;/strong&gt;.&amp;nbsp; Therefore, the method declaration of GetWebPageAsync has a return type of &lt;strong&gt;Task&amp;lt;string&amp;gt;&lt;/strong&gt;.&amp;nbsp; Because the return type is &lt;strong&gt;Task&amp;lt;string&amp;gt;&lt;/strong&gt;, the evaluation of the await expression in startButton_Click produces a &lt;strong&gt;string&lt;/strong&gt;, as the following statement demonstrates:&amp;nbsp; string webText = await getWebPageTask; .&lt;/p&gt;
&lt;p&gt;Similarly, the GetWebPageAsync method calls the &lt;a href="http://msdn.microsoft.com/en-us/library/system.net.http.httpclient.getstringasync(v=vs.110)"&gt;GetStringAsync&lt;/a&gt; method, which is defined with a return type of &lt;strong&gt;Task&amp;lt;string&amp;gt;&lt;/strong&gt;. The call to &lt;a href="http://msdn.microsoft.com/en-us/library/system.net.http.httpclient.getstringasync(v=vs.110)"&gt;GetStringAsync&lt;/a&gt; returns a &lt;strong&gt;Task&amp;lt;string&amp;gt;&lt;/strong&gt; and the evaluation of the await expression produces a &lt;strong&gt;string&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;The startButton_Click method has a return type of void.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt;&amp;nbsp; An async method can have a return type of &lt;a href="http://msdn.microsoft.com/en-us/library/dd321424(v=vs.110).aspx"&gt;Task&amp;lt;TResult&amp;gt;&lt;/a&gt;, &lt;a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.task(v=vs.110).aspx"&gt;Task&lt;/a&gt;, or &lt;strong&gt;void.&lt;/strong&gt;&amp;nbsp;&amp;nbsp; The &lt;strong&gt;void&lt;/strong&gt; return type is used primarily to define event handlers, where a &lt;strong&gt;void&lt;/strong&gt; return type is required. An async method that returns void can't be awaited, and the caller of a void-returning method can't catch exceptions that are thrown by the method.&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;&lt;strong&gt;Simplified Code&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;In the above example, the GetWebPageAsync method includes the following two statements:&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Consolas;" face="Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Task&amp;lt;string&amp;gt; getStringTask = (new HttpClient()).GetStringAsync(url); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; string webText = await getStringTask;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;The above two statements can be condensed into one statement, as follows:&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Consolas;" face="Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; string webText = await (new HttpClient()).GetStringAsync(url);&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;The condensed statement also returns a task and awaits the task, however the task is not stored in a variable. While the condensed statement is more concise, the uncondensed code facilitates a greater understanding of the control flow of your code, and of the associated tasks.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Exception Handling&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The following example illustrates exception handling for an async method. To catch an exception that applies to an async task, the &lt;strong&gt;await&lt;/strong&gt; expression is in a &lt;strong&gt;try&lt;/strong&gt; block, and the exception is caught in a &lt;strong&gt;catch&lt;/strong&gt; block.&lt;/p&gt;
&lt;p&gt;Uncomment the &amp;ldquo;throw new Exception&amp;rdquo; line in the example to demonstrate exception handling. The exception is caught in the &lt;strong&gt;catch&lt;/strong&gt; block, and the task's &lt;strong&gt;IsFaulted&lt;/strong&gt; property is set to &lt;strong&gt;true&lt;/strong&gt;, and the task's &lt;strong&gt;Exception.InnerException&lt;/strong&gt; property is set to the exception.&lt;/p&gt;
&lt;p&gt;Uncomment the &amp;ldquo;throw new OperationCancelledException&amp;rdquo; line to demonstrate what happens when you cancel an asynchronous process. The exception is caught in the &lt;strong&gt;catch&lt;/strong&gt; block and the task's &lt;strong&gt;IsCanceled&lt;/strong&gt; property is set to &lt;strong&gt;true&lt;/strong&gt;. Under some conditions that don't apply to this example, &lt;strong&gt;IsFaulted &lt;/strong&gt;is set to true and &lt;strong&gt;IsCanceled&lt;/strong&gt; is set to false. &lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Consolas;" face="Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; private async void testExceptionButton_Click(object sender, RoutedEventArgs e) &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Task&amp;lt;string&amp;gt; theTask = DelayAsync();&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Consolas;" face="Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; try&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&amp;nbsp; &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; string result = await theTask;&amp;nbsp; &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; Debug.WriteLine("Result: " + result);&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; catch (Exception ex)&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&amp;nbsp; &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; Debug.WriteLine("Exception Message: " + ex.Message);&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Debug.WriteLine("Task IsCanceled: " + theTask.IsCanceled);&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Debug.WriteLine("Task IsFaulted:&amp;nbsp; " + theTask.IsFaulted);&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (theTask.Exception != null)&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&amp;nbsp; &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; Debug.WriteLine("Task Exception Message: "&amp;nbsp; &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; + theTask.Exception.Message);&amp;nbsp; &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; Debug.WriteLine("Task Inner Exception Message: "&amp;nbsp; &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; + theTask.Exception.InnerException.Message);&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Consolas;" face="Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; private async Task&amp;lt;string&amp;gt; DelayAsync() &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; { &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; await Task.Delay(100);&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Consolas;" face="Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Uncomment each of the following lines to &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // demonstrate exception handling. &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Consolas;" face="Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //throw new OperationCanceledException("canceled"); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //throw new Exception("Something happened."); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return "Done"; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Consolas;" face="Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Output when no exception is thrown in the awaited method: &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; //&amp;nbsp;&amp;nbsp; Result: Done &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; //&amp;nbsp;&amp;nbsp; Task IsCanceled: False &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; //&amp;nbsp;&amp;nbsp; Task IsFaulted:&amp;nbsp; False &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Consolas;" face="Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Output when an Exception is thrown in the awaited method: &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; //&amp;nbsp;&amp;nbsp; Exception Message: Something happened. &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; //&amp;nbsp;&amp;nbsp; Task IsCanceled: False &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; //&amp;nbsp;&amp;nbsp; Task IsFaulted:&amp;nbsp; True &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; //&amp;nbsp;&amp;nbsp; Task Exception Message: One or more errors occurred. &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; //&amp;nbsp;&amp;nbsp; Task Inner Exception Message: Something happened. &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Consolas;" face="Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Output when a OperationCanceledException or TaskCanceledException &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // is thrown in the awaited method: &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; //&amp;nbsp;&amp;nbsp; Exception Message: canceled &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; //&amp;nbsp;&amp;nbsp; Task IsCanceled: True &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; //&amp;nbsp;&amp;nbsp; Task IsFaulted:&amp;nbsp; False&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;See Also&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="http://blogs.msdn.com/b/vbteam/archive/2011/04/13/async-feature-control-flow.aspx"&gt;Async Feature Control Flow blog&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://blogs.msdn.com/b/vbteam/archive/2011/05/10/simultaneous-async-tasks.aspx"&gt;Simultaneous Async Tasks blog&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://blogs.msdn.com/b/csharpfaq/archive/2012/01/23/using-async-for-file-access-alan-berman.aspx"&gt;Using Async for File Access blog&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Resources&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-US/vstudio/async"&gt;Details and Download Page&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://social.msdn.microsoft.com/Forums/en-US/async/threads"&gt;Forum for Feedback and Questions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://connect.microsoft.com/"&gt;Microsoft Connect for Bugs and Suggestions&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10323000" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/async/">async</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/await/">await</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/asynchronous+programming/">asynchronous programming</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/C_2300_+5-0/">C# 5.0</category></item><item><title>Announcing Microsoft “Roslyn” June 2012 CTP</title><link>http://blogs.msdn.com/b/csharpfaq/archive/2012/06/05/announcing-microsoft-roslyn-june-2012-ctp.aspx</link><pubDate>Tue, 05 Jun 2012 19:54:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10314556</guid><dc:creator>CSharpFAQ</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/csharpfaq/rsscomments.aspx?WeblogPostID=10314556</wfw:commentRss><comments>http://blogs.msdn.com/b/csharpfaq/archive/2012/06/05/announcing-microsoft-roslyn-june-2012-ctp.aspx#comments</comments><description>&lt;p&gt;Today, we're excited to announce that the Roslyn June 2012 CTP is now &lt;a href="http://www.msdn.com/roslyn"&gt;available&lt;/a&gt; for &lt;a href="http://go.microsoft.com/fwlink/?LinkID=228400"&gt;download&lt;/a&gt;!&lt;/p&gt;
&lt;p&gt;Since the &lt;a href="http://blogs.msdn.com/b/somasegar/archive/2011/10/19/roslyn-ctp-available-now.aspx"&gt;first public release of Roslyn&lt;/a&gt;, we&amp;rsquo;ve been hard at work implementing new &lt;a href="http://go.microsoft.com/fwlink/?LinkID=254240&amp;amp;clcid=0x409"&gt;language features&lt;/a&gt;, addressing top customer feedback from the &lt;a href="http://blogs.msdn.com/b/csharpfaq/archive/2011/10/19/introducing-the-microsoft-roslyn-ctp.aspx"&gt;Oct CTP&lt;/a&gt;, iterating on our API design and improving performance across our IDE and compiler scenarios.&amp;nbsp; With the recent release of &lt;a href="http://www.microsoft.com/visualstudio/11/en-us/downloads"&gt;Visual Studio 2012 RC&lt;/a&gt;, we have updated the Roslyn CTP to work with Visual Studio 2012 RC, in addition to Visual Studio 2010 SP1.&amp;nbsp; There&amp;rsquo;s never been a better time to grab the VS 2012 RC and the &lt;a href="http://www.msdn.com/roslyn"&gt;Roslyn CTP&lt;/a&gt; and start exploring with the new Roslyn &lt;a href="http://blogs.msdn.com/b/csharpfaq/archive/2012/01/30/roslyn-ctp-introduces-interactive-code-for-c.aspx"&gt;Interactive window&lt;/a&gt;!&lt;/p&gt;
&lt;p&gt;You can find an in-depth look at what&amp;rsquo;s new in the Roslyn June 2012 CTP on &lt;a href="http://blogs.msdn.com/b/jasonz/archive/2012/06/05/announcing-microsoft-roslyn-june-2012-ctp.aspx"&gt;Jason Zander&amp;rsquo;s blog&lt;/a&gt; and the &lt;a href="http://blogs.msdn.com/b/visualstudio/archive/2012/06/05/announcing-microsoft-roslyn-june-2012-ctp.aspx"&gt;Visual Studio blog&lt;/a&gt;.&amp;nbsp; The CTP ships with a number of documents that provide an excellent way to get started &amp;ndash; start with the &lt;a href="http://go.microsoft.com/fwlink/?LinkID=230702"&gt;overview&lt;/a&gt; document, and then move on to the &lt;a href="http://www.microsoft.com/en-us/download/details.aspx?id=27745"&gt;walkthroughs&lt;/a&gt;.&amp;nbsp; We also encourage you to check out our previous series on how to use the Roslyn APIs &amp;ndash; the &lt;a href="http://blogs.msdn.com/b/visualstudio/archive/2011/10/19/roslyn-syntax-visualizers.aspx"&gt;syntax visualizer&lt;/a&gt; is an invaluable tool, the &lt;a href="http://blogs.msdn.com/b/csharpfaq/archive/2011/11/03/using-the-roslyn-syntax-api.aspx"&gt;Syntax API&lt;/a&gt;, the &lt;a href="http://blogs.msdn.com/b/csharpfaq/archive/2011/11/23/using-the-roslyn-symbol-api.aspx"&gt;Symbol API&lt;/a&gt;, the &lt;a href="http://blogs.msdn.com/b/csharpfaq/archive/2011/12/02/introduction-to-the-roslyn-scripting-api.aspx"&gt;Scripting AP&lt;/a&gt;I, and how to &lt;a href="http://blogs.msdn.com/b/csharpfaq/archive/2012/02/06/implementing-a-code-action-using-roslyn.aspx"&gt;write a code action&lt;/a&gt;.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Feedback is a critical part of our design process!&amp;nbsp; Because it&amp;rsquo;s early in the project, we&amp;rsquo;re most interested in feedback around the API and understanding what scenarios you&amp;rsquo;d like to accomplish. Please get involved in the discussion about Roslyn on our &lt;a href="http://social.msdn.microsoft.com/forums/en-us/roslyn"&gt;forums&lt;/a&gt;. You can also file bugs on our &lt;a href="http://connect.microsoft.com/visualstudio"&gt;Connect site&lt;/a&gt;. Some of the team members are also on Twitter, and will be keeping an eye on the &lt;a href="http://twitter.com/#!/search/realtime/%23RoslynCTP"&gt;#RoslynCTP&lt;/a&gt; hashtag.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Happy Coding!&lt;/p&gt;
&lt;p&gt;Karen Ng&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;About the Author&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://twitter.com/#!/karenkayliu"&gt;Karen Ng&lt;/a&gt; is the Lead Program Manager responsible for the compilers, IDEs, and Languages for C#, Visual Basic, and F# at Microsoft.&amp;nbsp; Karen works on both the Visual Studio 2012 release and the Roslyn project.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10314556" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/IDE/">IDE</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/C_2300_/">C#</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/C_2300_+compiler/">C# compiler</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/roslyn/">roslyn</category></item><item><title>Async Targeting Pack for Visual Studio 11 now available for .NET 4 and Silverlight 5</title><link>http://blogs.msdn.com/b/csharpfaq/archive/2012/04/26/async-targeting-pack-for-visual-studio-11-now-available-for-net-4-and-silverlight-5.aspx</link><pubDate>Thu, 26 Apr 2012 20:39:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10298160</guid><dc:creator>CSharpFAQ</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/csharpfaq/rsscomments.aspx?WeblogPostID=10298160</wfw:commentRss><comments>http://blogs.msdn.com/b/csharpfaq/archive/2012/04/26/async-targeting-pack-for-visual-studio-11-now-available-for-net-4-and-silverlight-5.aspx#comments</comments><description>&lt;p&gt;If you&amp;rsquo;ve held off on trying Visual Studio 11 Beta because your .NET 4 or Silverlight 5 app uses the Async CTP, your wait is over!&amp;nbsp; Today we&amp;rsquo;ve published the &lt;a href="http://go.microsoft.com/fwlink/?LinkId=250980"&gt;Async Targeting Pack for Visual Studio 11&lt;/a&gt;, a NuGet package that lets your Visual Studio 11 projects target .NET 4 and Silverlight 5 while taking advantage of C#&amp;rsquo;s new &lt;b&gt;await&lt;/b&gt; keyword.&amp;nbsp; While Visual Studio 11 provides built-in support for &lt;b&gt;await&lt;/b&gt; in projects that target .NET 4.5 or .NET for Metro style apps, this targeting pack provides the API support to expand the set of target platforms to include .NET 4 and Silverlight 5 as well.&lt;/p&gt;
&lt;p&gt;Check out our &lt;a href="http://go.microsoft.com/fwlink/?LinkId=250980"&gt;release notes&lt;/a&gt; for more details on how to install NuGet and add the package reference in Visual Studio 11 Beta.&lt;/p&gt;
&lt;p&gt;Happy testing!&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Alex Turner&lt;br /&gt;Program Manager, VB/C# Compilers&lt;/em&gt;&lt;b&gt;&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=10298160" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/-NET+Framework+4/">.NET Framework 4</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/async/">async</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/await/">await</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/asynchronous+programming/">asynchronous programming</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/Silverlight/">Silverlight</category></item><item><title>Visual Studio 11 Beta Is Here!</title><link>http://blogs.msdn.com/b/csharpfaq/archive/2012/02/29/visual-studio-11-beta-is-here.aspx</link><pubDate>Wed, 29 Feb 2012 14:55:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10274205</guid><dc:creator>CSharpFAQ</dc:creator><slash:comments>20</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/csharpfaq/rsscomments.aspx?WeblogPostID=10274205</wfw:commentRss><comments>http://blogs.msdn.com/b/csharpfaq/archive/2012/02/29/visual-studio-11-beta-is-here.aspx#comments</comments><description>&lt;p&gt;As you may have seen in &lt;a href="http://blogs.msdn.com/b/jasonz/archive/2012/02/29/welcome-to-the-beta-of-visual-studio-11-and-net-framework-4-5.aspx"&gt;Jason&amp;rsquo;s blog&lt;/a&gt;, Visual Studio 11 Beta is available for &lt;a href="http://go.microsoft.com/fwlink/p/?LinkId=240162"&gt;download&lt;/a&gt; today, including the Beta version of C# 5.0!&amp;nbsp; If you&amp;rsquo;ve tried out the Visual Studio 11 Developer Preview, you already know that this is an exciting release, with two huge new features for C# developers: &lt;b&gt;async methods &lt;/b&gt;and support for creating &lt;b&gt;Windows 8 Metro style apps&lt;/b&gt;.&amp;nbsp; However, with Beta comes even more goodness, with support for &lt;b&gt;caller info attributes&lt;/b&gt;, as&lt;b&gt; &lt;/b&gt;well as a host of &lt;b&gt;async perf improvements&lt;/b&gt; and &lt;b&gt;async unit-testing support&lt;/b&gt;.&lt;/p&gt;
&lt;p&gt;The best way to see what&amp;rsquo;s new in Visual Studio 11 is to dive in and &lt;a href="http://go.microsoft.com/fwlink/p/?LinkId=240162"&gt;try it yourself&lt;/a&gt;.&amp;nbsp; However, if you&amp;rsquo;re curious to know what you&amp;rsquo;re getting, read on!&lt;/p&gt;
&lt;h2&gt;Async methods (the await keyword)&lt;/h2&gt;
&lt;p&gt;As you migrate your desktop applications to mobile phones and tablets, you&amp;rsquo;re increasingly faced with the realities of network latency, especially given a reliance on cloud services running in distant datacenters.&amp;nbsp; At the same time, your users expect far more responsiveness from these applications, with the bar set by smooth touch UIs that never drop a frame.&lt;/p&gt;
&lt;p&gt;The way out of this conundrum up to now has been asynchronous programming and the use of callbacks.&amp;nbsp; However, while callbacks can work for very simple methods, you quickly fall into the pit of despair for code that&amp;rsquo;s of any reasonable length or complexity, giving up the core control flow constructs you&amp;rsquo;ve trusted for years &amp;ndash; chained callback methods and for loops don&amp;rsquo;t mix!&lt;/p&gt;
&lt;p&gt;There has to be a way out of this mess, and there is &amp;ndash; &lt;b&gt;async methods &lt;/b&gt;in C# 5.0, powered by the &lt;b&gt;await&lt;/b&gt; keyword:&lt;/p&gt;
&lt;p&gt;&lt;span style="background: white; color: blue; font-family: consolas; font-size: 10pt;"&gt;public&lt;/span&gt;&lt;span style="background: white; color: black; font-family: consolas; font-size: 10pt;"&gt; &lt;/span&gt;&lt;span style="background: white; color: blue; font-family: consolas; font-size: 10pt;"&gt;async&lt;/span&gt;&lt;span style="background: white; color: black; font-family: consolas; font-size: 10pt;"&gt; &lt;/span&gt;&lt;span style="background: white; color: #2b91af; font-family: consolas; font-size: 10pt;"&gt;Task&lt;/span&gt;&lt;span style="background: white; color: black; font-family: consolas; font-size: 10pt;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background: white; color: blue; font-family: consolas; font-size: 10pt;"&gt;int&lt;/span&gt;&lt;span style="background: white; color: black; font-family: consolas; font-size: 10pt;"&gt;?&amp;gt; SumPageSizesAsync(&lt;/span&gt;&lt;span style="background: white; color: #2b91af; font-family: consolas; font-size: 10pt;"&gt;IList&lt;/span&gt;&lt;span style="background: white; color: black; font-family: consolas; font-size: 10pt;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background: white; color: #2b91af; font-family: consolas; font-size: 10pt;"&gt;Uri&lt;/span&gt;&lt;span style="background: white; color: black; font-family: consolas; font-size: 10pt;"&gt;&amp;gt; uris) { &lt;br /&gt;&lt;/span&gt;&lt;span style="background: white; color: black; font-family: consolas; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="background: white; color: blue; font-family: consolas; font-size: 10pt;"&gt;try &lt;/span&gt;&lt;span style="background: white; color: black; font-family: consolas; font-size: 10pt;"&gt;{ &lt;br /&gt;&lt;/span&gt;&lt;span style="background: white; color: black; font-family: consolas; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="background: white; color: blue; font-family: consolas; font-size: 10pt;"&gt;int&lt;/span&gt;&lt;span style="background: white; color: black; font-family: consolas; font-size: 10pt;"&gt; total = 0; &lt;br /&gt;&lt;/span&gt;&lt;span style="background: white; color: black; font-family: consolas; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="background: white; color: blue; font-family: consolas; font-size: 10pt;"&gt;var&lt;/span&gt;&lt;span style="background: white; color: black; font-family: consolas; font-size: 10pt;"&gt; client = &lt;/span&gt;&lt;span style="background: white; color: blue; font-family: consolas; font-size: 10pt;"&gt;new&lt;/span&gt;&lt;span style="background: white; color: black; font-family: consolas; font-size: 10pt;"&gt; &lt;/span&gt;&lt;span style="background: white; color: #2b91af; font-family: consolas; font-size: 10pt;"&gt;HttpClient&lt;/span&gt;&lt;span style="background: white; color: black; font-family: consolas; font-size: 10pt;"&gt;(); &lt;br /&gt;&lt;/span&gt;&lt;span style="background: white; color: black; font-family: consolas; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; client.MaxResponseContentBufferSize = 10000000; &lt;br /&gt;&lt;/span&gt;&lt;span style="background: white; color: black; font-family: consolas; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="background: white; color: blue; font-family: consolas; font-size: 10pt;"&gt;foreach&lt;/span&gt;&lt;span style="background: white; color: black; font-family: consolas; font-size: 10pt;"&gt; (&lt;/span&gt;&lt;span style="background: white; color: blue; font-family: consolas; font-size: 10pt;"&gt;var&lt;/span&gt;&lt;span style="background: white; color: black; font-family: consolas; font-size: 10pt;"&gt; uri &lt;/span&gt;&lt;span style="background: white; color: blue; font-family: consolas; font-size: 10pt;"&gt;in&lt;/span&gt;&lt;span style="background: white; color: black; font-family: consolas; font-size: 10pt;"&gt; uris) { &lt;br /&gt;&lt;/span&gt;&lt;span style="background: white; color: black; font-family: consolas; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; statusText.Text = &lt;/span&gt;&lt;span style="background: white; color: blue; font-family: consolas; font-size: 10pt;"&gt;string&lt;/span&gt;&lt;span style="background: white; color: black; font-family: consolas; font-size: 10pt;"&gt;.Format(&lt;/span&gt;&lt;span style="background: white; color: maroon; font-family: consolas; font-size: 10pt;"&gt;"Found {0} bytes ..."&lt;/span&gt;&lt;span style="background: white; color: black; font-family: consolas; font-size: 10pt;"&gt;, total); &lt;br /&gt;&lt;/span&gt;&lt;span style="background: white; color: black; font-family: consolas; font-size: 10pt;"&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: white; color: blue; font-family: consolas; font-size: 10pt;"&gt;var&lt;/span&gt;&lt;span style="background: white; color: black; font-family: consolas; font-size: 10pt;"&gt; data = &lt;/span&gt;&lt;span style="background: white; color: blue; font-family: consolas; font-size: 10pt;"&gt;await&lt;/span&gt;&lt;span style="background: white; color: black; font-family: consolas; font-size: 10pt;"&gt; client.GetByteArrayAsync(uri); &lt;br /&gt;&lt;/span&gt;&lt;span style="background: white; color: black; font-family: consolas; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; total += data.Length; &lt;br /&gt;&lt;/span&gt;&lt;span style="background: white; color: black; font-family: consolas; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; } &lt;br /&gt;&lt;/span&gt;&lt;span style="background: white; color: black; font-family: consolas; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; statusText.Text = &lt;/span&gt;&lt;span style="background: white; color: blue; font-family: consolas; font-size: 10pt;"&gt;string&lt;/span&gt;&lt;span style="background: white; color: black; font-family: consolas; font-size: 10pt;"&gt;.Format(&lt;/span&gt;&lt;span style="background: white; color: maroon; font-family: consolas; font-size: 10pt;"&gt;"Found {0} bytes total"&lt;/span&gt;&lt;span style="background: white; color: black; font-family: consolas; font-size: 10pt;"&gt;, total); &lt;br /&gt;&lt;/span&gt;&lt;span style="background: white; color: black; font-family: consolas; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="background: white; color: blue; font-family: consolas; font-size: 10pt;"&gt;return&lt;/span&gt;&lt;span style="background: white; color: black; font-family: consolas; font-size: 10pt;"&gt; total; &lt;br /&gt;&lt;/span&gt;&lt;span style="background: white; color: black; font-family: consolas; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; } &lt;br /&gt;&lt;/span&gt;&lt;span style="background: white; color: black; font-family: consolas; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="background: white; color: blue; font-family: consolas; font-size: 10pt;"&gt;catch&lt;/span&gt;&lt;span style="background: white; color: black; font-family: consolas; font-size: 10pt;"&gt; (&lt;/span&gt;&lt;span style="background: white; color: #2b91af; font-family: consolas; font-size: 10pt;"&gt;Exception&lt;/span&gt;&lt;span style="background: white; color: black; font-family: consolas; font-size: 10pt;"&gt;) { &lt;br /&gt;&lt;/span&gt;&lt;span style="background: white; color: black; font-family: consolas; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; statusText.Text = &lt;/span&gt;&lt;span style="background: white; color: maroon; font-family: consolas; font-size: 10pt;"&gt;"Error!"&lt;/span&gt;&lt;span style="background: white; color: black; font-family: consolas; font-size: 10pt;"&gt;; &lt;br /&gt;&lt;/span&gt;&lt;span style="background: white; color: black; font-family: consolas; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="background: white; color: blue; font-family: consolas; font-size: 10pt;"&gt;return&lt;/span&gt;&lt;span style="background: white; color: black; font-family: consolas; font-size: 10pt;"&gt; &lt;/span&gt;&lt;span style="background: white; color: blue; font-family: consolas; font-size: 10pt;"&gt;null&lt;/span&gt;&lt;span style="background: white; color: black; font-family: consolas; font-size: 10pt;"&gt;; &lt;br /&gt;&lt;/span&gt;&lt;span style="background: white; color: black; font-family: consolas; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; } &lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: consolas; font-size: 10pt;"&gt;}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;By marking your method with the &lt;b&gt;async&lt;/b&gt; keyword, the C# compiler transforms it into a resumable &lt;b&gt;async method&lt;/b&gt; that can pause execution whenever it sees the &lt;b&gt;await&lt;/b&gt; keyword by returning control to the caller.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If this is a &lt;strong&gt;client app&lt;/strong&gt;, control returns to your caller at each &lt;b&gt;await&lt;/b&gt;, and then to its caller, and so on, until you&amp;rsquo;ve freed up the UI thread, which keeps your app responsive. &lt;br /&gt;&lt;span style="font-size: xx-small;" size="1"&gt;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;If you&amp;rsquo;re writing a &lt;strong&gt;server&lt;/strong&gt;, returning to the caller frees up the thread handling the current request, ensuring that thread is available to process other requests and increasing your scalability.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In either case, once the operation you&amp;rsquo;re awaiting completes, your method wakes up, resuming execution right where it left off, even if you were within a foreach loop or a try-catch block.&amp;nbsp; When execution is paused, you can even hit F10 to Step Over at the &lt;b&gt;await&lt;/b&gt;, with the debugger returning you to the following line after the awaited operation completes.&lt;/p&gt;
&lt;p&gt;This is just a brief peek at Async &amp;ndash; if you&amp;rsquo;ve yet to try it, be sure to check out the &lt;a href="http://msdn.microsoft.com/async"&gt;Asynchronous Programming Developer Center&lt;/a&gt; for samples, videos and more to get started.&amp;nbsp; However, if you have been using Async for a while now through our previous preview releases, you&amp;rsquo;ll be excited to see what&amp;rsquo;s new in Beta!&lt;/p&gt;
&lt;p&gt;Since the Developer Preview, we&amp;rsquo;ve done work to &lt;b&gt;further improve the perf of async methods&lt;/b&gt;, reducing allocations to ensure you can feel comfortable using async methods throughout your application, wherever you need them.&amp;nbsp; However, such comfort also requires that you can trust your async methods to continue working as expected as you continue to make changes elsewhere in your app.&amp;nbsp; To this end, &lt;b&gt;MSTest and xUnit.net now support async methods directly&lt;/b&gt;, ensuring that the test runner waits for the async method to fully complete:&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: consolas; font-size: 10pt;"&gt;[&lt;span style="color: #2b91af;"&gt;TestMethod&lt;/span&gt;] &lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue; font-family: consolas; font-size: 10pt;"&gt;public&lt;/span&gt;&lt;span style="font-family: consolas; font-size: 10pt;"&gt; &lt;span style="color: blue;"&gt;async &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Task&lt;/span&gt; Test1() { &lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: consolas; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;var&lt;/span&gt; x = &lt;span style="color: blue;"&gt;await&lt;/span&gt; Engine.GetSevenAsync(); &lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: consolas; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;.AreEqual(x, 6); &lt;br /&gt;} &lt;br /&gt; &lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: consolas; font-size: 10pt;"&gt;[Xunit.&lt;span style="color: #2b91af;"&gt;Fact&lt;/span&gt;] &lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue; font-family: consolas; font-size: 10pt;"&gt;public&lt;/span&gt;&lt;span style="font-family: consolas; font-size: 10pt;"&gt; &lt;span style="color: blue;"&gt;async &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Task&lt;/span&gt; Test2() { &lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: consolas; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;var&lt;/span&gt; y = &lt;span style="color: blue;"&gt;await&lt;/span&gt; Engine.GetSevenAsync(); &lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: consolas; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;.AreEqual(x, 6); &lt;br /&gt;}&lt;/span&gt;&lt;/p&gt;
&lt;h2&gt;Caller Info Attributes&lt;/h2&gt;
&lt;p&gt;One language feature that&amp;rsquo;s making its debut in Visual Studio 11 Beta is &lt;b&gt;caller info attributes&lt;/b&gt;.&amp;nbsp; These attributes let a method accept implicit optional parameters intended to receive a line number, a file path or a member name.&amp;nbsp; Then, at the call site, the compiler will know to automatically fill in those argument values, based on the exact location where that method was called.&lt;/p&gt;
&lt;p&gt;One great use for caller info attributes is for &lt;strong&gt;logging methods&lt;/strong&gt;.&amp;nbsp; The code below defines a LogMessage method that accepts 4 parameters to output a structured log message.&amp;nbsp; However, when we call LogMessage within ProcessItem, we only need to specify one argument, message &amp;ndash; the compiler will do the work for us to pass in the name of the method calling LogMessage, along with the file name and line number of that call site.&amp;nbsp; Even better, if ProcessItem moves around in the file, changes files, or is renamed, the log message will be updated automatically.&lt;/p&gt;
&lt;p&gt;&lt;span style="color: blue; font-family: consolas; font-size: 10pt;"&gt;public&lt;/span&gt;&lt;span style="font-family: consolas; font-size: 10pt;"&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; LogMessage(&lt;span style="color: blue;"&gt;string&lt;/span&gt; message = "", &lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: consolas; font-size: 10pt;"&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 style="color: #2b91af;"&gt;CallerMemberName&lt;/span&gt;] &lt;span style="color: blue;"&gt;string&lt;/span&gt; member = &lt;span style="color: #a31515;"&gt;""&lt;/span&gt;, &lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: consolas; font-size: 10pt;"&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 style="color: #2b91af;"&gt;CallerFilePath&lt;/span&gt;] &lt;span style="color: blue;"&gt;string&lt;/span&gt; file = &lt;span style="color: #a31515;"&gt;"", &lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: consolas; font-size: 10pt;"&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 style="color: #2b91af;"&gt;CallerLineNumber&lt;/span&gt;] &lt;span style="color: blue;"&gt;int&lt;/span&gt; line = 0) &lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: consolas; font-size: 10pt;"&gt;{ &lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: consolas; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;var&lt;/span&gt; s = &lt;span style="color: #2b91af;"&gt;String&lt;/span&gt;.Format(&lt;span style="color: #a31515;"&gt;"{1} ({2}:{3}) - {0}"&lt;/span&gt;, message, member, file, line); &lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: consolas; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af;"&gt;Debug&lt;/span&gt;.WriteLine(s); &lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: consolas; font-size: 10pt;"&gt;} &lt;br /&gt; &lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue; font-family: consolas; font-size: 10pt;"&gt;public&lt;/span&gt;&lt;span style="font-family: consolas; font-size: 10pt;"&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; ProcessItem(&lt;span style="color: blue;"&gt;int&lt;/span&gt; index) { &lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: consolas; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; LogMessage(&lt;span style="color: #2b91af;"&gt;String&lt;/span&gt;.Format("&lt;span style="color: #a31515;"&gt;Processing item {0}&lt;/span&gt;", index)); &lt;br /&gt; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-family: consolas; font-size: 10pt;"&gt;DoSomeWork(); &lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: consolas; font-size: 10pt;"&gt;}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Another benefit is when you&amp;rsquo;re &lt;b&gt;implementing INotifyPropertyChanged&lt;/b&gt;.&amp;nbsp; Today, it&amp;rsquo;s common to write a helper method that invokes the PropertyChanged event, to which you pass the name of the property being set.&amp;nbsp; However, you must then pass that property name as a string argument within each property setter.&amp;nbsp; This adds risk when refactoring inside such classes &amp;ndash; if you use VS to change the name of the property and all its references, you&amp;rsquo;ll have to manually update that string.&lt;/p&gt;
&lt;p&gt;With caller info attributes, you can just call your NotifyPropertyChanged helper as below, without arguments.&amp;nbsp; The compiler will pass in the name of the property for you, which will always be up to date, even if the property is renamed:&lt;/p&gt;
&lt;p&gt;&lt;span style="color: blue; font-family: consolas; font-size: 10pt;"&gt;public&lt;/span&gt;&lt;span style="font-family: consolas; font-size: 10pt;"&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Widget&lt;/span&gt; : &lt;span style="color: #2b91af;"&gt;INotifyPropertyChanged &lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: consolas; font-size: 10pt;"&gt;{ &lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: consolas; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;string&lt;/span&gt; statusText; &lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: consolas; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;string&lt;/span&gt; StatusText &lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: consolas; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; { &lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: consolas; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;get&lt;/span&gt; { &lt;span style="color: blue;"&gt;return&lt;/span&gt; statusText; } &lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: consolas; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;set&lt;/span&gt; { statusText = &lt;span style="color: blue;"&gt;value&lt;/span&gt;; NotifyPropertyChanged(); } &lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: consolas; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; } &lt;br /&gt; &lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: consolas; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; NotifyPropertyChanged([&lt;span style="color: #2b91af;"&gt;CallerMemberName&lt;/span&gt;] &lt;span style="color: blue;"&gt;string&lt;/span&gt; property = &lt;span style="color: blue;"&gt;null&lt;/span&gt;) &lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: consolas; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; { &lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: consolas; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;if&lt;/span&gt; (PropertyChanged != &lt;span style="color: blue;"&gt;null&lt;/span&gt;) &lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: consolas; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; { &lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: consolas; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; PropertyChanged(&lt;span style="color: blue;"&gt;this&lt;/span&gt;, &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;PropertyChangedEventArgs&lt;/span&gt;(property)); &lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: consolas; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; } &lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: consolas; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; } &lt;br /&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-family: consolas; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;event&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;PropertyChangedEventHandler&lt;/span&gt; PropertyChanged; &lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: consolas; font-size: 10pt;"&gt;}&lt;/span&gt;&lt;/p&gt;
&lt;h2&gt;Call to Action&lt;/h2&gt;
&lt;p&gt;If you&amp;rsquo;re excited to try out Metro style app development, async methods, caller info attributes and all the other performance and productivity improvements in Visual Studio 11 Beta, there&amp;rsquo;s a set of great resources available to explore:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;b&gt;Get the bits!&lt;/b&gt;&amp;nbsp; The first step is to &lt;a href="http://go.microsoft.com/fwlink/p/?LinkId=240162"&gt;download Visual Studio 11 Beta&lt;/a&gt; &amp;ndash; this page also has links to download Team Foundation Server 11 Beta and .NET Framework 4.5 Beta.&amp;nbsp; You can install Visual Studio 11 Beta on Windows 7 to get up and running quickly, or set it up on top of the &lt;a href="http://go.microsoft.com/fwlink/?LinkID=239723"&gt;Windows 8 Consumer Preview&lt;/a&gt; to start writing Metro style apps. &lt;br /&gt;&lt;span style="font-size: xx-small;" size="1"&gt;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Learn and explore!&lt;/b&gt;&amp;nbsp; Check out &lt;a href="http://blogs.msdn.com/b/jasonz/archive/2012/02/29/welcome-to-the-beta-of-visual-studio-11-and-net-framework-4-5.aspx"&gt;Jason's blog post on Visual Studio 11 Beta and .NET Framework 4.5 Beta&lt;/a&gt;, and then dig deeper into &lt;a href="http://msdn.microsoft.com/en-us/library/bb386063(VS.110).aspx"&gt;what&amp;rsquo;s new in Visual Studio 11 Beta&lt;/a&gt;.&amp;nbsp; To explore what&amp;rsquo;s new in Windows 8, first read the &lt;a href="http://blogs.msdn.com/b/b8"&gt;Building Windows 8 blog post announcing the Windows 8 Consumer Preview&lt;/a&gt;, and then browse the new &lt;a href="http://blogs.msdn.com/b/windowsappdev"&gt;Windows 8 app developer blog&lt;/a&gt;. &lt;br /&gt;&lt;span style="font-size: xx-small;" size="1"&gt;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Share your feedback!&lt;/b&gt;&amp;nbsp; As you build apps in Visual Studio 11 Beta, let us know what you think!&amp;nbsp; Discuss what&amp;rsquo;s working well and what could be better on the &lt;a href="http://social.msdn.microsoft.com/Forums/en-US/category/vsvnext"&gt;Visual Studio&lt;/a&gt; and &lt;a href="http://social.msdn.microsoft.com/Forums/en-us/category/windowsapps"&gt;Windows 8&lt;/a&gt; forums, and be sure to file any bugs you find through the &lt;a href="http://connect.microsoft.com/visualstudio"&gt;Visual Studio&lt;/a&gt;, &lt;a href="http://connect.microsoft.com/lightswitch"&gt;LightSwitch&lt;/a&gt;, and &lt;a href="http://connect.microsoft.com/expression"&gt;Blend&lt;/a&gt; Connect sites.&amp;nbsp; Also, vote on the features you&amp;rsquo;d like to see in Visual Studio moving forward on our &lt;a href="http://visualstudio.uservoice.com/forums/121579-visual-studio/category/35066-ide"&gt;UserVoice&lt;/a&gt; site.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Most importantly, have fun with the Beta and build some awesome apps!&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Alex Turner &lt;br /&gt;Program Manager, VB/C# Compilers&lt;/em&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10274205" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/async/">async</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/await/">await</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/asynchronous+programming/">asynchronous programming</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/C_2300_+5-0/">C# 5.0</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/-NET+Framework+4-5/">.NET Framework 4.5</category></item><item><title>Implementing a Code Action using Roslyn</title><link>http://blogs.msdn.com/b/csharpfaq/archive/2012/02/06/implementing-a-code-action-using-roslyn.aspx</link><pubDate>Mon, 06 Feb 2012 16:38:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10264465</guid><dc:creator>CSharpFAQ</dc:creator><slash:comments>9</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/csharpfaq/rsscomments.aspx?WeblogPostID=10264465</wfw:commentRss><comments>http://blogs.msdn.com/b/csharpfaq/archive/2012/02/06/implementing-a-code-action-using-roslyn.aspx#comments</comments><description>&lt;p&gt;&lt;em&gt;&amp;nbsp;By Brian Rasmussen&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;The Roslyn Services API makes it easy to implement extensions that detect and fix code issues directly in Visual Studio. The Roslyn Services API is available as part of the &lt;a title="Roslyn CTP" href="http://www.microsoft.com/download/en/details.aspx?id=27746"&gt;Roslyn CTP&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;In this post we implement a Visual Studio extension that identifies calls to the extension method Count() on Enumerable, where the result is compared to greater than zero, e.g. someSequence.Count() &amp;gt; 0. The problem, with that code construct, is that Count() may have to enumerate the entire sequence to evaluate the result. A much better approach in this case is to call Enumerable.Any() instead.&lt;/p&gt;
&lt;p&gt;To address this we implement a CodeIssueProvider, which detects the problem and a CodeAction, which replaces the condition with a call to Enumerable.Any() as appropriate. E.g. our code action will change someSequence.Count() &amp;gt; 0 to someSequence.Any().&lt;/p&gt;
&lt;p&gt;There are a couple additional scenarios, we want to handle as well: First of all, the expression could be reversed and written like 0 &amp;lt; someSequence.Count(). The other special case is that the comparison could be &amp;gt;= 1 instead of &amp;gt; 0, which is essentially the same comparison as before. We want the extension to be able to handle both of these cases.&lt;/p&gt;
&lt;p&gt;Obviously we do not want to change calls to methods called Count() unless they bind to the IEnumerable&amp;lt;T&amp;gt; extension method defined on Enumerable.&lt;/p&gt;
&lt;h2&gt;Getting Started&lt;/h2&gt;
&lt;p&gt;The Roslyn CTP ships with a number of templates designed to make it easy to get started using the Roslyn APIs. To get started we create a new project and select Code Issue from the Roslyn templates under the Visual C# section. Let&amp;rsquo;s call the project ReplaceCountWithAny.&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-33-48/3554.NewProject.png"&gt;&lt;img border="0" alt="" src="http://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-33-48/3554.NewProject.png" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The template generates a small, working code issue provider that will highlight words with the letter &amp;ldquo;a&amp;rdquo;. To see the sample in action let&amp;rsquo;s build and run the project created by the template. This launches a new instance of Visual Studio with the extension enabled. From the newly launched Visual Studio we create a console application and notice how the namespace and class keywords and so on are underlined by our extension.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-33-48/4201.SampleCodeIssue.png"&gt;&lt;img border="0" alt="" src="http://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-33-48/4201.SampleCodeIssue.png" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;While the sample may not be very useful as an extension to Visual Studio, it neatly sets up everything we need to get started implementing our own extension. All we have to do is replace the contents of the generated GetIssues method. Notice that there are three overloads for GetIssues. We&amp;rsquo;ll implement the overload that takes a CommonSyntaxNode as input. The remaining two overloads can be left as they are in this case.&lt;/p&gt;
&lt;p&gt;The generated CodeIssueProvider class implements ICodeIssueProvider and is decorated with the ExportSyntaxNodeCodeIssueProvider attribute. This allows Visual Studio to import this type as an extension, which will handle the contract established by the ICodeIssueProvider interface.&lt;/p&gt;
&lt;h2&gt;Implementing GetIssues&lt;/h2&gt;
&lt;p&gt;Our GetIssues method will be invoked for every syntax node, so the first thing we need to do is filter out all the nodes we don&amp;rsquo;t want to handle. Since we are looking for expressions like someSequence.Count() &amp;gt; 0, we&amp;rsquo;re only interested in nodes of the type BinaryExpressionSyntax. We can instruct Visual Studio to only invoke our provider for nodes of specific type(s) by providing a list of types through the ExportSyntaxNodeCodeIssueProvider attribute. Let&amp;rsquo;s update the attribute as follows:&lt;/p&gt;
&lt;pre style="background: white;"&gt;&lt;span style="color: black; font-family: Consolas; font-size: 8pt;"&gt;[&lt;/span&gt;&lt;span style="color: #2b91af; font-family: Consolas; font-size: 8pt;"&gt;ExportSyntaxNodeCodeIssueProvider&lt;/span&gt;&lt;span style="color: black; font-family: Consolas; font-size: 8pt;"&gt;(&lt;/span&gt;&lt;span style="color: #a31515; font-family: Consolas; font-size: 8pt;"&gt;"ReplaceCountWithAny"&lt;/span&gt;&lt;span style="color: black; font-family: Consolas; font-size: 8pt;"&gt;,&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="background: white;"&gt;&lt;span style="color: #2b91af; font-family: Consolas; font-size: 8pt;"&gt;LanguageNames&lt;/span&gt;&lt;span style="color: black; font-family: Consolas; font-size: 8pt;"&gt;.CSharp,&amp;nbsp;&lt;/span&gt;&lt;span style="color: blue; font-family: Consolas; font-size: 8pt;"&gt;typeof&lt;/span&gt;&lt;span style="color: black; font-family: Consolas; font-size: 8pt;"&gt;(&lt;/span&gt;&lt;span style="color: #2b91af; font-family: Consolas; font-size: 8pt;"&gt;BinaryExpressionSyntax&lt;/span&gt;&lt;span style="color: black; font-family: Consolas; font-size: 8pt;"&gt;))]&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="background: white;"&gt;&lt;span style="color: blue; font-family: Consolas; font-size: 8pt;"&gt;class&lt;/span&gt;&lt;span style="color: black; font-family: Consolas; font-size: 8pt;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="color: #2b91af; font-family: Consolas; font-size: 8pt;"&gt;CodeIssueProvider&lt;/span&gt;&lt;span style="color: black; font-family: Consolas; font-size: 8pt;"&gt;&amp;nbsp;:&amp;nbsp;&lt;/span&gt;&lt;span style="color: #2b91af; font-family: Consolas; font-size: 8pt;"&gt;ICodeIssueProvider ...&lt;/span&gt;&lt;span style="color: black; font-family: Consolas; font-size: 8pt;"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;This allows us to safely cast the provided CommonSyntaxNode to BinaryExpressionSyntax in GetIssues.&lt;/p&gt;
&lt;p&gt;To identify the cases we want to handle we have to figure out if one part of the expression is a call to Enumerable.Count() and the other part is a relevant comparison. We&amp;rsquo;ll abstract those checks into a couple helper methods, so with that our implementation of GetIssues looks as follows.&lt;/p&gt;
&lt;pre style="background: white; color: black; font-family: Consolas;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af;"&gt;CodeIssue&lt;/span&gt;&amp;gt;&amp;nbsp;GetIssues(&lt;span style="color: #2b91af;"&gt;IDocument&lt;/span&gt;&amp;nbsp;document,&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;CommonSyntaxNode&lt;/span&gt;&amp;nbsp;node,&amp;nbsp;&lt;span style="color: #2b91af;"&gt;CancellationToken&lt;/span&gt;&amp;nbsp;cancellationToken)
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;var&lt;/span&gt;&amp;nbsp;binaryExpression&amp;nbsp;=&amp;nbsp;(&lt;span style="color: #2b91af;"&gt;BinaryExpressionSyntax&lt;/span&gt;)node;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;var&lt;/span&gt;&amp;nbsp;left&amp;nbsp;=&amp;nbsp;binaryExpression.Left;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;var&lt;/span&gt;&amp;nbsp;right&amp;nbsp;=&amp;nbsp;binaryExpression.Right;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;var&lt;/span&gt;&amp;nbsp;kind&amp;nbsp;=&amp;nbsp;binaryExpression.Kind;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;if&lt;/span&gt;&amp;nbsp;(IsCallToEnumerableCount(document,&amp;nbsp;left,&amp;nbsp;cancellationToken)&amp;nbsp;&amp;amp;&amp;amp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;IsRelevantRightSideComparison(document,&amp;nbsp;right,&amp;nbsp;kind,&amp;nbsp;cancellationToken)&amp;nbsp;||
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;IsCallToEnumerableCount(document,&amp;nbsp;right,&amp;nbsp;cancellationToken)&amp;nbsp;&amp;amp;&amp;amp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;IsRelevantLeftSideComparison(document,&amp;nbsp;left,&amp;nbsp;kind,&amp;nbsp;cancellationToken))
&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 style="color: blue;"&gt;yield&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;return&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;CodeIssue&lt;/span&gt;(&lt;span style="color: #2b91af;"&gt;CodeIssue&lt;/span&gt;.&lt;span style="color: #2b91af;"&gt;Severity&lt;/span&gt;.Info,&amp;nbsp;binaryExpression.Span,
&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 style="color: blue;"&gt;string&lt;/span&gt;.Format(&lt;span style="color: #a31515;"&gt;"Change&amp;nbsp;{0}&amp;nbsp;to&amp;nbsp;use&amp;nbsp;Any()&amp;nbsp;instead&amp;nbsp;of&amp;nbsp;Count()&amp;nbsp;to&amp;nbsp;avoid&amp;nbsp;"&lt;/span&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 style="color: #a31515;"&gt;"possible&amp;nbsp;enumeration&amp;nbsp;of&amp;nbsp;entire&amp;nbsp;sequence."&lt;/span&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;binaryExpression));
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
}
&lt;/pre&gt;
&lt;pre style="background: white; color: black; font-family: Consolas;"&gt;&amp;nbsp;&lt;/pre&gt;
&lt;p&gt;The instance of CodeIssue we return specifies a severity level, which can be Error, Warning or Info, a span, which is used to highlight the part of the source code the issue applies to, and a text describing the identified issue to the user.&lt;/p&gt;
&lt;h2&gt;Helper Methods&lt;/h2&gt;
&lt;p&gt;Let&amp;rsquo;s turn our attention to the helper methods used in GetIssues. IsCallToEnumerableCount returns true if the part of the expression we&amp;rsquo;re looking at is in fact a call to Count() on some sequence. Once again we&amp;rsquo;ll start by filtering the undesired expressions.&lt;/p&gt;
&lt;p&gt;First of all the expression must be an invocation. If that&amp;rsquo;s the case, we&amp;rsquo;ll get the actual method call from the Expression property of the invocation. So if the expression we&amp;rsquo;re looking at is someSequence.Count() &amp;gt; 0 we now have the Count() part, but how do we figure out if this binds to the Enumerable type?&lt;/p&gt;
&lt;p&gt;To answer questions like that we need to query the semantic model. Fortunately part of the input to GetIssues is an IDocument, which represents a single document in a project and solution. We can get the semantic model via the document and from there we can get the SymbolInfo we need.&lt;/p&gt;
&lt;p&gt;With the SymbolInfo present we can check if our call binds to the desired method. Because Count() is an extension method we&amp;rsquo;ll need to handle it a bit differently. Recall that C# allows extension methods to be called as if they were part of the calling type. The semantic model represents this as a MethodSymbol with a ConstructedFrom property set to the original type. This could possibly be handled slightly better, so look out for changes in the API here.&lt;/p&gt;
&lt;p&gt;All that remains is to figure out the declaring type for our constructed extension method. If that matches Enumerable we have found an invocation of Enumerable.Count().&lt;/p&gt;
&lt;p&gt;The implementation looks like this:&amp;nbsp;&lt;/p&gt;
&lt;pre style="background: white; color: black; font-family: Consolas;"&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;bool&lt;/span&gt;&amp;nbsp;IsCallToEnumerableCount(&lt;span style="color: #2b91af;"&gt;IDocument&lt;/span&gt;&amp;nbsp;document,&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;ExpressionSyntax&lt;/span&gt;&amp;nbsp;expression,&amp;nbsp;&lt;span style="color: #2b91af;"&gt;CancellationToken&lt;/span&gt;&amp;nbsp;cancellationToken)
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;var&lt;/span&gt;&amp;nbsp;invocation&amp;nbsp;=&amp;nbsp;expression&amp;nbsp;&lt;span style="color: blue;"&gt;as&lt;/span&gt;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;InvocationExpressionSyntax&lt;/span&gt;;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;if&lt;/span&gt;&amp;nbsp;(invocation&amp;nbsp;==&amp;nbsp;&lt;span style="color: blue;"&gt;null&lt;/span&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 style="color: blue;"&gt;return&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;false&lt;/span&gt;;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;var&lt;/span&gt;&amp;nbsp;call&amp;nbsp;=&amp;nbsp;invocation.Expression&amp;nbsp;&lt;span style="color: blue;"&gt;as&lt;/span&gt;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;MemberAccessExpressionSyntax&lt;/span&gt;;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;if&lt;/span&gt;&amp;nbsp;(call&amp;nbsp;==&amp;nbsp;&lt;span style="color: blue;"&gt;null&lt;/span&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 style="color: blue;"&gt;return&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;false&lt;/span&gt;;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;var&lt;/span&gt;&amp;nbsp;semanticModel&amp;nbsp;=&amp;nbsp;document.GetSemanticModel(cancellationToken);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;var&lt;/span&gt;&amp;nbsp;methodSymbol&amp;nbsp;=&amp;nbsp;semanticModel.GetSemanticInfo(call,&amp;nbsp;cancellationToken).Symbol&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;as&lt;/span&gt;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;MethodSymbol&lt;/span&gt;;&lt;/pre&gt;
&lt;pre style="background: white; color: black; font-family: Consolas;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;if&lt;/span&gt;&amp;nbsp;(methodSymbol&amp;nbsp;==&amp;nbsp;&lt;span style="color: blue;"&gt;null&lt;/span&gt;&amp;nbsp;||&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;methodSymbol.Name&amp;nbsp;!=&amp;nbsp;&lt;span style="color: #a31515;"&gt;"Count"&lt;/span&gt;&amp;nbsp;||&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;methodSymbol.ConstructedFrom&amp;nbsp;==&amp;nbsp;&lt;span style="color: blue;"&gt;null&lt;/span&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 style="color: blue;"&gt;return&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;false&lt;/span&gt;;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
&lt;/pre&gt;
&lt;pre style="background: white; color: black; font-family: Consolas;"&gt; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;var&lt;/span&gt;&amp;nbsp;enumerable&amp;nbsp;=&amp;nbsp;semanticModel.Compilation.GetTypeByMetadataName(
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af;"&gt;Enumerable&lt;/span&gt;).FullName);
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;if&lt;/span&gt;&amp;nbsp;(enumerable&amp;nbsp;==&amp;nbsp;&lt;span style="color: blue;"&gt;null&lt;/span&gt;&amp;nbsp;||&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;!methodSymbol.ConstructedFrom.ContainingType.Equals(enumerable))
&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 style="color: blue;"&gt;return&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;false&lt;/span&gt;;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;return&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;true&lt;/span&gt;;
} 
&lt;/pre&gt;
&lt;p&gt;With that settled the next thing we need to look for is a relevant comparison on the other side of the binary expression and that&amp;rsquo;s the job of our IsRelevantRightSideComparison and IsRelevantLeftSideComparison helper methods.&lt;/p&gt;
&lt;p&gt;Here are&amp;nbsp;the implementations for those:&lt;/p&gt;
&lt;pre style="background: white; color: black; font-family: Consolas;"&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;bool&lt;/span&gt;&amp;nbsp;IsRelevantRightSideComparison(&lt;span style="color: #2b91af;"&gt;IDocument&lt;/span&gt;&amp;nbsp;document,&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;ExpressionSyntax&lt;/span&gt;&amp;nbsp;expression,&amp;nbsp;&lt;span style="color: #2b91af;"&gt;SyntaxKind&lt;/span&gt;&amp;nbsp;kind,&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;CancellationToken&lt;/span&gt;&amp;nbsp;cancellationToken)
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;var&lt;/span&gt;&amp;nbsp;semanticInfo&amp;nbsp;=&amp;nbsp;document.GetSemanticModel(cancellationToken).
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;GetSemanticInfo(expression);
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;int&lt;/span&gt;?&amp;nbsp;value;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;if&lt;/span&gt;&amp;nbsp;(!semanticInfo.IsCompileTimeConstant&amp;nbsp;||&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;(value&amp;nbsp;=&amp;nbsp;semanticInfo.ConstantValue&amp;nbsp;&lt;span style="color: blue;"&gt;as&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;int&lt;/span&gt;?)&amp;nbsp;==&amp;nbsp;&lt;span style="color: blue;"&gt;null&lt;/span&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 style="color: blue;"&gt;return&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;false&lt;/span&gt;;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;if&lt;/span&gt;&amp;nbsp;(kind&amp;nbsp;==&amp;nbsp;&lt;span style="color: #2b91af;"&gt;SyntaxKind&lt;/span&gt;.GreaterThanExpression&amp;nbsp;&amp;amp;&amp;amp;&amp;nbsp;value&amp;nbsp;==&amp;nbsp;0&amp;nbsp;||
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;kind&amp;nbsp;==&amp;nbsp;&lt;span style="color: #2b91af;"&gt;SyntaxKind&lt;/span&gt;.GreaterThanOrEqualExpression&amp;nbsp;&amp;amp;&amp;amp;&amp;nbsp;value&amp;nbsp;==&amp;nbsp;1)
&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 style="color: blue;"&gt;return&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;true&lt;/span&gt;;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;return&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;false&lt;/span&gt;;
}
 
&lt;/pre&gt;
&lt;pre style="background: white; color: black; font-family: Consolas;"&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;bool&lt;/span&gt;&amp;nbsp;IsRelevantLeftSideComparison(&lt;span style="color: #2b91af;"&gt;IDocument&lt;/span&gt;&amp;nbsp;document,&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;ExpressionSyntax&lt;/span&gt;&amp;nbsp;expression,&amp;nbsp;&lt;span style="color: #2b91af;"&gt;SyntaxKind&lt;/span&gt;&amp;nbsp;kind,&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;CancellationToken&lt;/span&gt;&amp;nbsp;cancellationToken)
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;var&lt;/span&gt;&amp;nbsp;semanticInfo&amp;nbsp;=&amp;nbsp;document.GetSemanticModel(cancellationToken).
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;GetSemanticInfo(expression);
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;int&lt;/span&gt;?&amp;nbsp;value;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;if&lt;/span&gt;&amp;nbsp;(!semanticInfo.IsCompileTimeConstant&amp;nbsp;||
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;(value&amp;nbsp;=&amp;nbsp;semanticInfo.ConstantValue&amp;nbsp;&lt;span style="color: blue;"&gt;as&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;int&lt;/span&gt;?)&amp;nbsp;==&amp;nbsp;&lt;span style="color: blue;"&gt;null&lt;/span&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 style="color: blue;"&gt;return&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;false&lt;/span&gt;;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;if&lt;/span&gt;&amp;nbsp;(kind&amp;nbsp;==&amp;nbsp;&lt;span style="color: #2b91af;"&gt;SyntaxKind&lt;/span&gt;.LessThanExpression&amp;nbsp;&amp;amp;&amp;amp;&amp;nbsp;value&amp;nbsp;==&amp;nbsp;0&amp;nbsp;||
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;kind&amp;nbsp;==&amp;nbsp;&lt;span style="color: #2b91af;"&gt;SyntaxKind&lt;/span&gt;.LessThanOrEqualExpression&amp;nbsp;&amp;amp;&amp;amp;&amp;nbsp;value&amp;nbsp;==&amp;nbsp;1)
&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 style="color: blue;"&gt;return&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;true&lt;/span&gt;;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;return&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;false&lt;/span&gt;;
}&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;They are almost identical, but it is important that we get the both the comparison and the value correct so we don&amp;rsquo;t highlight something like Count() &amp;gt;= 0.&lt;/p&gt;
&lt;h2&gt;Testing the CodeIssueProvider&lt;/h2&gt;
&lt;p&gt;&lt;o:p&gt;At this point our code issue provider can detect the issues we&amp;rsquo;re interested in handling. Compile and run the project to launch a new instance of Visual Studio with our extension enabled. Add some code and notice that calls to Enumerable.Count() are correctly underlined while method calls to other methods named Count() are not. &lt;/o:p&gt;&lt;/p&gt;
&lt;p&gt;&lt;o:p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-33-48/4274.CodeIssue.png"&gt;&lt;img border="0" alt="" src="http://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-33-48/4274.CodeIssue.png" /&gt;&lt;/a&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p&gt;The next step is to provide a code action for our code issue.&lt;/p&gt;
&lt;h2&gt;CodeAction&lt;/h2&gt;
&lt;p&gt;&lt;o:p&gt;To implement a code action we need a type which implements ICodeAction. ICodeAction is a simple interface that defines a description and an icon for the action and a single method called GetEdit, which returns an edit that will transform the current syntax tree. Let&amp;rsquo;s start by looking at the constructor for our CodeAction class.&lt;/o:p&gt;&lt;/p&gt;
&lt;pre style="background: white; color: black; font-family: Consolas;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;CodeAction(&lt;span style="color: #2b91af;"&gt;ICodeActionEditFactory&lt;/span&gt;&amp;nbsp;editFactory,&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;IDocument&lt;/span&gt;&amp;nbsp;document,&amp;nbsp;&lt;span style="color: #2b91af;"&gt;BinaryExpressionSyntax&lt;/span&gt;&amp;nbsp;binaryExpression)
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;this&lt;/span&gt;.editFactory&amp;nbsp;=&amp;nbsp;editFactory;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;this&lt;/span&gt;.document&amp;nbsp;=&amp;nbsp;document;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;this&lt;/span&gt;.binaryExpression&amp;nbsp;=&amp;nbsp;binaryExpression;
}
&lt;/pre&gt;
&lt;p&gt;An instance of CodeAction will be created for every code issue identified, so for convenience sake we just pass the arguments we need to implement the change to the constructor itself. We need an ICodeActionEditFactory to create a transformation for our newly created syntax tree. As syntax trees are immutable in Roslyn, returning a new tree is the only way we can make any changes. Fortunately, Roslyn will reuse as much of the original tree as possible and thus avoid creating unnecessary nodes.&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Furthermore we need a document to let us access both the syntax tree and the project and solution for our source code and a reference to the syntax node we want to replace.&lt;/p&gt;
&lt;p&gt;This brings us to the GetEdit method. This is where we create a transformation, which will replace the identified binary expression node with a newly created invocation node for the call to Any(). The creation of the new node is handled by a small helper method called GetNewNode. Both are listed below.&lt;/p&gt;
&lt;pre style="background: white; color: black; font-family: Consolas;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;ICodeActionEdit&lt;/span&gt;&amp;nbsp;GetEdit(&lt;span style="color: #2b91af;"&gt;CancellationToken&lt;/span&gt;&amp;nbsp;cancellationToken)
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;var&lt;/span&gt;&amp;nbsp;syntaxTree&amp;nbsp;=&amp;nbsp;(&lt;span style="color: #2b91af;"&gt;SyntaxTree&lt;/span&gt;)document.GetSyntaxTree(cancellationToken);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;var&lt;/span&gt;&amp;nbsp;newExpression&amp;nbsp;=&amp;nbsp;GetNewNode(binaryExpression).
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;WithLeadingTrivia(binaryExpression.GetLeadingTrivia()).
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;WithTrailingTrivia(binaryExpression.GetTrailingTrivia());
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;var&lt;/span&gt;&amp;nbsp;newRoot&amp;nbsp;=&amp;nbsp;syntaxTree.Root.ReplaceNode(binaryExpression,&amp;nbsp;newExpression);
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;return&lt;/span&gt;&amp;nbsp;editFactory.CreateTreeTransformEdit(
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;document.Project.Solution,
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;syntaxTree,
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;newRoot,
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cancellationToken:&amp;nbsp;cancellationToken);
}
&lt;/pre&gt;
&lt;pre style="background: white; color: black; font-family: Consolas;"&gt; 
&lt;span style="color: blue;"&gt;private&lt;/span&gt;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;ExpressionSyntax&lt;/span&gt;&amp;nbsp;GetNewNode(&lt;span style="color: #2b91af;"&gt;BinaryExpressionSyntax&lt;/span&gt;&amp;nbsp;node)
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;var&lt;/span&gt;&amp;nbsp;invocation&amp;nbsp;=&amp;nbsp;node.DescendentNodes().
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;OfType&amp;lt;&lt;span style="color: #2b91af;"&gt;InvocationExpressionSyntax&lt;/span&gt;&amp;gt;().Single();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;var&lt;/span&gt;&amp;nbsp;caller&amp;nbsp;=&amp;nbsp;invocation.DescendentNodes().
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;OfType&amp;lt;&lt;span style="color: #2b91af;"&gt;MemberAccessExpressionSyntax&lt;/span&gt;&amp;gt;().Single();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;return&lt;/span&gt;&amp;nbsp;invocation.Update(
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;caller.Update(caller.Expression,&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;caller.OperatorToken,&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;Syntax&lt;/span&gt;.IdentifierName(&lt;span style="color: #a31515;"&gt;"Any"&lt;/span&gt;)),
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;invocation.ArgumentList);
}
&lt;/pre&gt;
&lt;p&gt;The Roslyn syntax tree maintains full fidelity with the original source code, so each node in the tree may have both leading and trailing trivia representing white space and comments. I.e. we need to preserve the trivia from the original node to maintain comments and layout of the code when we exchange the nodes. We&amp;nbsp;call&amp;nbsp;the WithLeadingTrivia and WithTrailingTrivia extension methods&amp;nbsp;to handle&amp;nbsp;that.&lt;/p&gt;
&lt;p&gt;Also, notice that GetNewNode preserves the argument list from Count(), so if the extension method was invoked with a lambda to count specific items in the sequence so will Any().&lt;/p&gt;
&lt;h2&gt;Wrapping Up&lt;/h2&gt;
&lt;p&gt;To enable our code action we need to update GetIssues in our CodeIssueProvider to return a CodeAction with each CodeIssue. Each code issue may provide a number of code actions to let the user pick between different resolutions. In this case we will just return a single code action as outlined above.&lt;/p&gt;
&lt;p&gt;The updated part of GetIssues looks like this:&lt;/p&gt;
&lt;pre style="background: white; color: black; font-family: Consolas;"&gt;&lt;span style="color: blue;"&gt;yield&lt;/span&gt; &lt;span style="color: blue;"&gt;return&lt;/span&gt; &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;CodeIssue&lt;/span&gt;(&lt;span style="color: #2b91af;"&gt;CodeIssue&lt;/span&gt;.&lt;span style="color: #2b91af;"&gt;Severity&lt;/span&gt;.Info, binaryExpression.Span,
    &lt;span style="color: blue;"&gt;string&lt;/span&gt;.Format(&lt;span style="color: #a31515;"&gt;"Change {0} to use Any() instead of Count() to avoid "&lt;/span&gt; +
                  &lt;span style="color: #a31515;"&gt;"possible enumeration of entire sequence."&lt;/span&gt;, binaryExpression),
    &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;CodeAction&lt;/span&gt;(editFactory, document, binaryExpression));
    &lt;/pre&gt;
&lt;p&gt;Rebuild and run the project to launch a new instance of Visual Studio with our extension loaded. Notice that the code issue now provides a drop down with the option to invoke our code action to fix the issue.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-33-48/8422.CodeAction.png"&gt;&lt;img border="0" alt="" src="http://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-33-48/8422.CodeAction.png" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;We have implemented an extension to Visual Studio that will help us improve our code.&lt;/p&gt;
&lt;h2&gt;About the author&lt;/h2&gt;
&lt;p&gt;Brian is a Senior SDET at Microsoft working on the C# and VB language services in Roslyn. Before joining Microsoft Brian was a Microsoft MVP for Visual C# for four years. Brian can be found on Twitter (&lt;a title="@kodehoved" href="http://twitter.com/kodehoved"&gt;@kodehoved&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=10264465" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/C_2300_/">C#</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/Visual+Studio/">Visual Studio</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/roslyn/">roslyn</category></item><item><title>Start Coding for the Kinect</title><link>http://blogs.msdn.com/b/csharpfaq/archive/2012/02/06/start-coding-for-the-kinect.aspx</link><pubDate>Mon, 06 Feb 2012 15:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10263396</guid><dc:creator>CSharpFAQ</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/csharpfaq/rsscomments.aspx?WeblogPostID=10263396</wfw:commentRss><comments>http://blogs.msdn.com/b/csharpfaq/archive/2012/02/06/start-coding-for-the-kinect.aspx#comments</comments><description>&lt;p&gt;&lt;em&gt;by Alan Berman&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;I'm intrigued by all the interesting industry apps being developed for the Kinect.&amp;nbsp; I started wondering how easy it is to start programming the Kinect using Visual Studio on a PC.&amp;nbsp; It is very easy to get started, especially since the Kinect SDK has sample applications with sample code.&lt;/p&gt;
&lt;p&gt;Because I had bought a Kinect bundled with my Xbox and not a standalone Kinect, I needed to buy a Kinect Sensor Power Supply, which can be purchased online.&amp;nbsp; This product has a cable with a USB connector that can be plugged into a PC.&lt;/p&gt;
&lt;p&gt;I downloaded the Kinect SDK from the &lt;a title="Kinect for Windows" href="http://www.microsoft.com/en-us/kinectforwindows/develop/overview.aspx"&gt;Kinect for Windows&lt;/a&gt; download site.&amp;nbsp; After installation of the Kinect SDK, this appears in Start / All Programs.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-33-48/8713.KinectMenu.jpg"&gt;&lt;img border="0" alt="" src="http://blogs.msdn.com/resized-image.ashx/__size/200x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-33-48/8713.KinectMenu.jpg" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;When you choose "Kinect SDK Sample Browser", the following Sample Browser window appears.&amp;nbsp; You can run the samples and download the associated C# projects from the window.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-33-48/4863.SampleBrowser.jpg"&gt;&lt;img border="0" alt="" src="http://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-33-48/4863.SampleBrowser.jpg" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;Here's what the Kinect Explorer looks like.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-33-48/8688.KinectExplorer.jpg"&gt;&lt;img border="0" alt="" src="http://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-33-48/8688.KinectExplorer.jpg" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;In the above window, the image at the left demonstrates skeletal tracking. The same image also shows Norm Estabrook as seen from the RGB camera.&amp;nbsp; For the image at the right, depth camera data with the distance from the Kinect is translated into an image with different colors.&lt;/p&gt;
&lt;p&gt;You can change settings by clicking the arrow at the lower left.&lt;/p&gt;
&lt;p&gt;Here's what the Shape Game looks like.&amp;nbsp; Norm is trying to change the direction of shapes falling from the sky.&amp;nbsp; The app also supports vocal commands.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-33-48/0564.ShapeGame.jpg"&gt;&lt;img border="0" alt="" src="http://blogs.msdn.com/resized-image.ashx/__size/350x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-33-48/0564.ShapeGame.jpg" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The Kinect Audio Demo looks like this.&amp;nbsp; It shows the direction of the sound source and recognizes spoken colors.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-33-48/7711.KinectAudioDemo.jpg"&gt;&lt;img border="0" alt="" src="http://blogs.msdn.com/resized-image.ashx/__size/450x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-33-48/7711.KinectAudioDemo.jpg" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The Kinect SDK includes the C# code for these sample apps, which will help you start coding.&lt;/p&gt;
&lt;p&gt;Another important resource is&amp;nbsp;the phenomenal &lt;strong&gt;Getting Started&lt;/strong&gt; videos at the&amp;nbsp;&lt;a title="Kinect for Windows Quickstart Series" href="http://channel9.msdn.com/Series/KinectQuickstart"&gt;Kinect for Windows&amp;nbsp;Quickstart Series&lt;/a&gt;.&amp;nbsp; Happy coding!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10263396" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/Visual+Studio+2010/">Visual Studio 2010</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/C_2300_/">C#</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/Kinect/">Kinect</category></item><item><title>Roslyn CTP Introduces Interactive Code for C#</title><link>http://blogs.msdn.com/b/csharpfaq/archive/2012/01/30/roslyn-ctp-introduces-interactive-code-for-c.aspx</link><pubDate>Tue, 31 Jan 2012 00:22:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10262014</guid><dc:creator>CSharpFAQ</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/csharpfaq/rsscomments.aspx?WeblogPostID=10262014</wfw:commentRss><comments>http://blogs.msdn.com/b/csharpfaq/archive/2012/01/30/roslyn-ctp-introduces-interactive-code-for-c.aspx#comments</comments><description>&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;There's been a lot of posts on using the Roslyn CTP APIs for syntax trees, semantic binding, and IDE smart editor features. The Roslyn CTP also introduces a set of features for C# we refer to as "Interactive". These features won't be new to VB, but we plan to bring the same feature set to VB. Interactive features comprise three goals:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&amp;middot; &lt;b&gt;Read-eval-print loop (long known as a REPL)&lt;/b&gt; -- to support exploring code, learning about APIs, and iterative development&lt;/p&gt;
&lt;/blockquote&gt;
&lt;blockquote&gt;
&lt;p&gt;&amp;middot; &lt;b&gt;Keep simple programs simple&lt;/b&gt; -- just write some top-level variable declarations, statements, and function declarations, then run the code as a text-based asset without needing a solution, project, class, Main, etc.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;blockquote&gt;
&lt;p&gt;&amp;middot; &lt;b&gt;Roslyn Scripting APIs&lt;/b&gt; -- to support .NET apps hosting a C# engine to execute snippets or files of code to script the application&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;This post talks about the Interactive features in the Roslyn CTP, which is only supported for C# right now, but we are working on VB support for a future release.&lt;/p&gt;
&lt;p&gt;Interactive code is a set of language and tooling features that enable customers to interactively explore and discover C# and VB readily. You do not have to create a solution, then a project, then a class, then a Main method and finally then get to write the few lines you care about. Users can open a tool window in VS and start typing code snippets to see what they do. You can build up execution context cumulatively by defining variables and functions, executing statement, defining types, and building up data structures. You can also seed the REPL's execution context from a project so that you can explore instantiating types from the project and trying out new snippets of code or develop a new function or integrity check on the data structures used in the project. This sort of development has been a hallmark of dynamic languages for decades and considered to be one of the most productive features related to dynamic languages.&lt;/p&gt;
&lt;p&gt;Some getting started walkthroughs:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;a href="http://msdn.microsoft.com/en-us/vstudio/hh543923" target="_blank"&gt;Executing Code in the Interactive Window&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://msdn.microsoft.com/en-us/vstudio/hh543924" target="_blank"&gt;Seeding the Interactive Window from a WPF Project&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;For a quick example, using View-&amp;gt;Other Windows-&amp;gt;C# Interactive and starting to type a reference, you get completion:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-33-48-metablogapi/0458.clip_5F00_image002_5F00_70EBFF56.jpg"&gt;&lt;img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border-width: 0px;" title="clip_image002" border="0" alt="clip_image002" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-33-48-metablogapi/4251.clip_5F00_image002_5F00_thumb_5F00_49B1B621.jpg" width="376" height="204" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;After finishing the previous input and hitting enter to execute it, if you start to type MessageBox, you get a quick fix for inserting the 'using' and then completion to help with enter a call to Show().&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-33-48-metablogapi/3678.clip_5F00_image004_5F00_5B8E49EE.jpg"&gt;&lt;img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border-width: 0px;" title="clip_image004" border="0" alt="clip_image004" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-33-48-metablogapi/5635.clip_5F00_image004_5F00_thumb_5F00_1B583074.jpg" width="381" height="196" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;When using the REPL to iteratively develop code, you may start with simple expression or statement snippets of code. You might be just checking how to call a .NET function correctly, or making a change to live code and data structures, then writing a snippet to check whether the change occurred as expected. You might build up helper functions that, for example, either help you recreate execution context for experimenting in another REPL session or help you repeat integrity checks as you experiment with an algorithm. You can save helper code away in .csx files or in your project for later evaluation and use in the REPL. Your code snippets might grow into a little program or utility that you want to run now and then, which you can save in a .csx file. You can #load .csx files in the REPL to re-execute the same code at a later time.&lt;/p&gt;
&lt;p&gt;An important aspect of the Interactive feature set is enabling you to keep simple programs simple. If you have a quick, one-off task that just needs, say, 10-20 lines of code to open a file, manipulate some data, or test some code, you can open a .csx file and write the code you need. Then run the code with csi.exe. You do not have to jump through the VS ceremonial hoops to create a program, compile it, then execute the resulting .exe. By default, csi.exe uses the same references and 'using's that a console application uses, but you can use #r to add other references in your script file. A script file is a self-contained simple program that may have started as little snippets of code in the REPL. When you invoke csi.exe, you can pass multiple .csx files (and even .cs files) on the command line which is useful if you have a library of top-level functions you commonly use with different main scripts that use the library. The whole idea is to let you work in a simple way when simple text-based code assets are easy to manage or execute as part of a build script, file utility, etc.&lt;/p&gt;
&lt;p&gt;Lastly, with the ability to execute snippets of code and cumulatively build up execution context, it is very natural to want to host the C# execution engine as a way to enable scripting of your .NET applications. You can then take snippets of code or files from users and execute them in a context where free identifiers are bound to public members on a host object model instance. The host application can decide what to expose to user script code for controlling the application, adding commands, or hooking events.&lt;/p&gt;
&lt;p&gt;Scripting walkthroughs:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;a href="http://msdn.microsoft.com/en-us/vstudio/hh830430" target="_blank"&gt;Introduction to Scripting&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;a href="http://msdn.microsoft.com/en-us/vstudio/hh543925" target="_blank"&gt;Scripting a Paint-like Application&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;You can &lt;a href="http://www.microsoft.com/download/en/details.aspx?id=27746" target="_blank"&gt;download the &amp;ldquo;Roslyn&amp;rdquo; CTP&lt;/a&gt; and try it out today!&amp;nbsp; The REPL installs as a new tool window in VS 2010.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10262014" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/C_2300_/">C#</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/roslyn/">roslyn</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/REPL/">REPL</category></item><item><title>Using Async for File Access</title><link>http://blogs.msdn.com/b/csharpfaq/archive/2012/01/23/using-async-for-file-access-alan-berman.aspx</link><pubDate>Mon, 23 Jan 2012 17:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10258951</guid><dc:creator>CSharpFAQ</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/csharpfaq/rsscomments.aspx?WeblogPostID=10258951</wfw:commentRss><comments>http://blogs.msdn.com/b/csharpfaq/archive/2012/01/23/using-async-for-file-access-alan-berman.aspx#comments</comments><description>&lt;p&gt;&lt;em&gt;by Alan Berman&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;The new &lt;a href="http://msdn.microsoft.com/en-us/library/hh191443(v=VS.110).aspx"&gt;Async&lt;/a&gt; feature in Visual Studio makes it easy to code asynchronous method calls. To make synchronous code asynchronous, you can simply call an asynchronous method instead of a synchronous method and add a few keywords to the code, as shown in the examples below.&amp;nbsp; You no longer need to define continuations to capture what happens when the asynchronous operation finishes, which can otherwise complicate the code.&lt;/p&gt;
&lt;p&gt;Even with the simplicity, why make file access calls asynchronous?&amp;nbsp; Aren't they fast enough already?&amp;nbsp; Here are reasons to consider:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Asynchrony makes UI applications more responsive. It allows the UI thread that launches the operation to be used for other things. If the UI thread is blocked by executing code that takes a long time (i.e. more than, say, 50 milliseconds), the UI may freeze until the I/O is complete and the UI thread is again able to process keyboard and mouse input and other events.&lt;/li&gt;
&lt;li&gt;Asynchrony improves the scalability of ASP.NET and other server-based applications by reducing the need for threads.&amp;nbsp; If file operations are synchronous and a hundred file access operations are happening at once, then a hundred threads are needed.&amp;nbsp; Asynchronous operations often do not require use of a thread during the wait.&amp;nbsp; They use the existing I/O completion thread briefly at the end.&lt;/li&gt;
&lt;li&gt;Even though a file access operation has very low latency now, it's possible that latency may greatly increase in the future. For example, a file may later be moved to a server that's across the world.&lt;/li&gt;
&lt;li&gt;The added overhead of using the Async feature is small.&lt;/li&gt;
&lt;li&gt;Asynchronous tasks can easily be run in parallel.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Running the Examples&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; The examples in this blog do not apply to Metro style apps, which are Windows 8 applications that are tailored for touch interaction and are full screen.&amp;nbsp; For information on using Async for file access in Metro style apps, see &lt;a href="http://msdn.microsoft.com/en-us/library/br230302(v=VS.110).aspx"&gt;.NET for Metro style apps overview&lt;/a&gt; and &lt;a href="http://msdn.microsoft.com/en-us/library/k3352a4t(VS.110).aspx"&gt;File and Stream I/O&lt;/a&gt;.&amp;nbsp; For some good examples of Metro file I/O, you can download the &lt;a href="http://code.msdn.microsoft.com/windowsapps/File-access-sample-d723e597"&gt;File Access Sample&lt;/a&gt; provided on the Dev Center.&lt;/p&gt;
&lt;p&gt;The examples below run in either Visual Studio 11 Developer Preview or the Async CTP for Visual Studio 2010 SP1.&amp;nbsp; Both can be accessed from &lt;a href="http://msdn.microsoft.com/en-US/vstudio/async"&gt;Visual Studio Asynchronous Programming&lt;/a&gt;. If you use the Async CTP for Visual Studio 2010, add a reference to AsyncCtpLibrary.dll, which is in My Documents\Microsoft Visual Studio Async CTP\Samples.&lt;/p&gt;
&lt;p&gt;Include the following using statements in the console examples below.&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;using &lt;/span&gt;System;
&lt;span style="color: blue;"&gt;using &lt;/span&gt;System.Collections.Generic;
&lt;span style="color: blue;"&gt;using &lt;/span&gt;System.IO;
&lt;span style="color: blue;"&gt;using &lt;/span&gt;System.Text;
&lt;span style="color: blue;"&gt;using &lt;/span&gt;System.Threading.Tasks;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Use of the FileStream Class&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The examples below use the &lt;a href="http://msdn.microsoft.com/en-us/library/system.io.filestream(v=VS.110).aspx"&gt;FileStream&lt;/a&gt; class, which has an option that causes asynchronous I/O to occur at the operating system level.&amp;nbsp; In many cases, this will avoid blocking a ThreadPool thread.&amp;nbsp; To enable this option, you must specify the useAsync=true or options=FileOptions.Asynchronous argument in the constructor call.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/system.io.streamreader(v=VS.110).aspx"&gt;StreamReader&lt;/a&gt; and &lt;a href="http://msdn.microsoft.com/en-us/library/system.io.streamwriter(v=VS.110).aspx"&gt;StreamWriter&lt;/a&gt; do not have this option if you open them directly by specifying a file path. StreamReader/Writer do have this option if you provide them a Stream that was opened by the &lt;a href="http://msdn.microsoft.com/en-us/library/system.io.filestream(v=VS.110).aspx"&gt;FileStream&lt;/a&gt; class. Note that asynchrony provides a responsiveness advantage in UI apps even if a thread pool thread is blocked, since the UI thread is not blocked during the wait.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Writing Text&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The following example writes text to a file.&amp;nbsp; At each await statement, the method immediately exits.&amp;nbsp; When the file I/O is complete, the method resumes at the statement following the await statement.&amp;nbsp; Note that the async modifier is in the definition of methods that use the await statement.&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;static void &lt;/span&gt;Main(&lt;span style="color: blue;"&gt;string&lt;/span&gt;[] args)
{
    ProcessWrite().Wait();
    &lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;.Write(&lt;span style="color: #a31515;"&gt;"Done "&lt;/span&gt;);
    &lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;.ReadKey();
}

&lt;span style="color: blue;"&gt;static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Task &lt;/span&gt;ProcessWrite()
{
    &lt;span style="color: blue;"&gt;string &lt;/span&gt;filePath = &lt;span style="color: #a31515;"&gt;@"c:\temp2\temp2.txt"&lt;/span&gt;;
    &lt;span style="color: blue;"&gt;string &lt;/span&gt;text = &lt;span style="color: #a31515;"&gt;"Hello World\r\n"&lt;/span&gt;;

    &lt;span style="color: blue;"&gt;return &lt;/span&gt;WriteTextAsync(filePath, text);
}

&lt;span style="color: blue;"&gt;static async &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Task &lt;/span&gt;WriteTextAsync(&lt;span style="color: blue;"&gt;string &lt;/span&gt;filePath, &lt;span style="color: blue;"&gt;string &lt;/span&gt;text)
{
    &lt;span style="color: blue;"&gt;byte&lt;/span&gt;[] encodedText = &lt;span style="color: #2b91af;"&gt;Encoding&lt;/span&gt;.Unicode.GetBytes(text);

    &lt;span style="color: blue;"&gt;using &lt;/span&gt;(&lt;span style="color: #2b91af;"&gt;FileStream &lt;/span&gt;sourceStream = &lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;FileStream&lt;/span&gt;(filePath,
        &lt;span style="color: #2b91af;"&gt;FileMode&lt;/span&gt;.Append, &lt;span style="color: #2b91af;"&gt;FileAccess&lt;/span&gt;.Write, &lt;span style="color: #2b91af;"&gt;FileShare&lt;/span&gt;.None,
        bufferSize: 4096, useAsync: &lt;span style="color: blue;"&gt;true&lt;/span&gt;))
    {
        &lt;span style="color: blue;"&gt;await &lt;/span&gt;sourceStream.WriteAsync(encodedText, 0, encodedText.Length);
    };
}&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Reading Text&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The following example reads text from a file.&amp;nbsp; The text is buffered and, in this case, placed into a &lt;a href="http://msdn.microsoft.com/en-us/library/system.text.stringbuilder.aspx"&gt;StringBuilder&lt;/a&gt;.&amp;nbsp; Unlike in the previous example, the evaluation of the await produces a value.&amp;nbsp; The &lt;a href="http://msdn.microsoft.com/en-us/library/hh137813(v=vs.110).aspx"&gt;ReadAsync&lt;/a&gt; method returns a Task&amp;lt;Int32&amp;gt;, so the evaluation of the await produces an Int32 value (numRead) that is returned after the operation completes..&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;static void &lt;/span&gt;Main(&lt;span style="color: blue;"&gt;string&lt;/span&gt;[] args)
{
    ProcessRead().Wait();
    &lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;.Write(&lt;span style="color: #a31515;"&gt;"Done "&lt;/span&gt;);
    &lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;.ReadKey();
}

&lt;span style="color: blue;"&gt;static async &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Task &lt;/span&gt;ProcessRead()
{
    &lt;span style="color: blue;"&gt;string &lt;/span&gt;filePath = &lt;span style="color: #a31515;"&gt;@"c:\temp2\temp2.txt"&lt;/span&gt;;

    &lt;span style="color: blue;"&gt;if &lt;/span&gt;(&lt;span style="color: #2b91af;"&gt;File&lt;/span&gt;.Exists(filePath) == &lt;span style="color: blue;"&gt;false&lt;/span&gt;)
    {
        &lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515;"&gt;"file not found: " &lt;/span&gt;+ filePath);
    }
    &lt;span style="color: blue;"&gt;else &lt;/span&gt;{
        &lt;span style="color: blue;"&gt;try &lt;/span&gt;{
            &lt;span style="color: blue;"&gt;string &lt;/span&gt;text = &lt;span style="color: blue;"&gt;await &lt;/span&gt;ReadTextAsync(filePath);
            &lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;.WriteLine(text);
        }
        &lt;span style="color: blue;"&gt;catch &lt;/span&gt;(&lt;span style="color: #2b91af;"&gt;Exception &lt;/span&gt;ex)
        {
            &lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;.WriteLine(ex.Message);
        }
    }
}

&lt;span style="color: blue;"&gt;static async &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Task&lt;/span&gt;&amp;lt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;gt; ReadTextAsync(&lt;span style="color: blue;"&gt;string &lt;/span&gt;filePath)
{
    &lt;span style="color: blue;"&gt;using &lt;/span&gt;(&lt;span style="color: #2b91af;"&gt;FileStream &lt;/span&gt;sourceStream = &lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;FileStream&lt;/span&gt;(filePath,
        &lt;span style="color: #2b91af;"&gt;FileMode&lt;/span&gt;.Open, &lt;span style="color: #2b91af;"&gt;FileAccess&lt;/span&gt;.Read, &lt;span style="color: #2b91af;"&gt;FileShare&lt;/span&gt;.Read,
        bufferSize: 4096, useAsync: &lt;span style="color: blue;"&gt;true&lt;/span&gt;))
    {
        &lt;span style="color: #2b91af;"&gt;StringBuilder &lt;/span&gt;sb = &lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;StringBuilder&lt;/span&gt;();

        &lt;span style="color: blue;"&gt;byte&lt;/span&gt;[] buffer = &lt;span style="color: blue;"&gt;new byte&lt;/span&gt;[0x1000];
        &lt;span style="color: blue;"&gt;int &lt;/span&gt;numRead;
        &lt;span style="color: blue;"&gt;while &lt;/span&gt;((numRead = &lt;span style="color: blue;"&gt;await &lt;/span&gt;sourceStream.ReadAsync(buffer, 0, buffer.Length)) != 0)
        {
            &lt;span style="color: blue;"&gt;string &lt;/span&gt;text = &lt;span style="color: #2b91af;"&gt;Encoding&lt;/span&gt;.Unicode.GetString(buffer, 0, numRead);
            sb.Append(text);
        }

        &lt;span style="color: blue;"&gt;return &lt;/span&gt;sb.ToString();
    }
}&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Control Flow&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;While it's easy to make method calls asynchronous, it helps to know what actually happens when the program reaches an await statement.&lt;/p&gt;
&lt;p&gt;The original example had the statement "await sourceStream.WriteAsync...".&amp;nbsp; That's a contraction of&amp;nbsp; these two statements:&amp;nbsp; "Task theTask = sourceStream.WriteAsync&amp;hellip;" and "await theTask".&amp;nbsp; The first statement causes a task to be returned and file processing to start.&amp;nbsp; The second statement with the await causes the method to immediately exit (returning a different task).&amp;nbsp; When the file processing later completes, execution returns to the statement following the await.&lt;/p&gt;
&lt;p&gt;The following example does the same thing as the "Writing Text" example, however it splits the await statement into two statements and adds console output to assist in understanding the sequence.&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;static void &lt;/span&gt;Main(&lt;span style="color: blue;"&gt;string&lt;/span&gt;[] args)
{
    ProcessWrite2().Wait();
    &lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;.Write(&lt;span style="color: #a31515;"&gt;"Done "&lt;/span&gt;);
    &lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;.ReadKey();
}

&lt;span style="color: blue;"&gt;static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Task &lt;/span&gt;ProcessWrite2()
{
    &lt;span style="color: blue;"&gt;string &lt;/span&gt;filePath = &lt;span style="color: #a31515;"&gt;@"c:\temp2\temp2.txt"&lt;/span&gt;;
    &lt;span style="color: blue;"&gt;string &lt;/span&gt;text = &lt;span style="color: #a31515;"&gt;"Hello World\r\n"&lt;/span&gt;;

    &lt;span style="color: #2b91af;"&gt;Task &lt;/span&gt;theTask = WriteTextAsync2(filePath, text);

    ShowInfo(theTask, &lt;span style="color: #a31515;"&gt;"In ProcessWrite2 before wait"&lt;/span&gt;);
    &lt;span style="color: blue;"&gt;return &lt;/span&gt;theTask;
}

&lt;span style="color: blue;"&gt;static async &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Task &lt;/span&gt;WriteTextAsync2(&lt;span style="color: blue;"&gt;string &lt;/span&gt;filePath, &lt;span style="color: blue;"&gt;string &lt;/span&gt;text)
{
    &lt;span style="color: blue;"&gt;byte&lt;/span&gt;[] encodedText = &lt;span style="color: #2b91af;"&gt;Encoding&lt;/span&gt;.Unicode.GetBytes(text);

    &lt;span style="color: blue;"&gt;using &lt;/span&gt;(&lt;span style="color: #2b91af;"&gt;FileStream &lt;/span&gt;sourceStream = &lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;FileStream&lt;/span&gt;(filePath,
        &lt;span style="color: #2b91af;"&gt;FileMode&lt;/span&gt;.Append, &lt;span style="color: #2b91af;"&gt;FileAccess&lt;/span&gt;.Write, &lt;span style="color: #2b91af;"&gt;FileShare&lt;/span&gt;.None,
        bufferSize: 4096, useAsync: &lt;span style="color: blue;"&gt;true&lt;/span&gt;))
    {
        &lt;span style="color: #2b91af;"&gt;Task &lt;/span&gt;theTask = sourceStream.WriteAsync(encodedText, 0, encodedText.Length);

        ShowInfo(theTask, &lt;span style="color: #a31515;"&gt;"In WriteTextAsync2 before await"&lt;/span&gt;);
        &lt;span style="color: blue;"&gt;await &lt;/span&gt;theTask;
        ShowInfo(theTask, &lt;span style="color: #a31515;"&gt;"In WriteTextAsync2 after await"&lt;/span&gt;);
    };
}

&lt;span style="color: blue;"&gt;static void &lt;/span&gt;ShowInfo(&lt;span style="color: #2b91af;"&gt;Task &lt;/span&gt;theTask, &lt;span style="color: blue;"&gt;string &lt;/span&gt;message)
{
    &lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;.Write(message.PadRight(32) + &lt;span style="color: #a31515;"&gt;" "&lt;/span&gt;);
    &lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;.Write(&lt;span style="color: #a31515;"&gt;"task " &lt;/span&gt;+ theTask.Id.ToString() + &lt;span style="color: #a31515;"&gt;" "&lt;/span&gt;);
    &lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;.Write(&lt;span style="color: #a31515;"&gt;"\n"&lt;/span&gt;);
}

&lt;/pre&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="color: #008040;" color="#008040"&gt;// Console output:&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008040;" color="#008040"&gt;// In WriteTextAsync2 before await task 1&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008040;" color="#008040"&gt;// In ProcessWrite2 before wait task 2&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008040;" color="#008040"&gt;// In WriteTextAsync2 after await task 1&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008040;" color="#008040"&gt;// Done&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="color: #008040;" color="#008040"&gt;&lt;/span&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Parallel Asynchronous I/O&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The following example demonstrates parallel processing by writing 10 text files.&amp;nbsp; For each file, the WriteAsync method returns a task.&amp;nbsp; Each task is added to a list of tasks.&amp;nbsp; The "await Task.WhenAll(tasks)" statement exits the method and resumes within the method when file processing is complete for all of the tasks.&lt;/p&gt;
&lt;p&gt;For the Visual Studio 2010 Async CTP, use TaskEx.WhenAll instead of Task.WhenAll.&lt;/p&gt;
&lt;p&gt;The example closes all FileStream instances in a finally block after the tasks are complete. If each FileStream was instead created in a using statement, the FileStream might be disposed of before the task was complete.&lt;/p&gt;
&lt;p&gt;Note that any speedup is almost entirely from the parallel processing and not the asynchronous processing.&amp;nbsp; The advantages of asynchrony are that it does not tie up multiple threads, and that it does not tie up the user interface thread.&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;static void &lt;/span&gt;Main(&lt;span style="color: blue;"&gt;string&lt;/span&gt;[] args)
{
    ProcessWriteMult().Wait();
    &lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;.Write(&lt;span style="color: #a31515;"&gt;"Done "&lt;/span&gt;);
    &lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;.ReadKey();
}

&lt;span style="color: blue;"&gt;static async &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Task &lt;/span&gt;ProcessWriteMult()
{
    &lt;span style="color: blue;"&gt;string &lt;/span&gt;folder = &lt;span style="color: #a31515;"&gt;@"c:\temp2\"&lt;/span&gt;;
    &lt;span style="color: #2b91af;"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af;"&gt;Task&lt;/span&gt;&amp;gt; tasks = &lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af;"&gt;Task&lt;/span&gt;&amp;gt;();
    &lt;span style="color: #2b91af;"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af;"&gt;FileStream&lt;/span&gt;&amp;gt; sourceStreams = &lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af;"&gt;FileStream&lt;/span&gt;&amp;gt;();

    &lt;span style="color: blue;"&gt;try &lt;/span&gt;{
        &lt;span style="color: blue;"&gt;for &lt;/span&gt;(&lt;span style="color: blue;"&gt;int &lt;/span&gt;index = 1; index &amp;lt;= 10; index++)
        {
            &lt;span style="color: blue;"&gt;string &lt;/span&gt;text = &lt;span style="color: #a31515;"&gt;"In file " &lt;/span&gt;+ index.ToString() + &lt;span style="color: #a31515;"&gt;"\r\n"&lt;/span&gt;;

            &lt;span style="color: blue;"&gt;string &lt;/span&gt;fileName = &lt;span style="color: #a31515;"&gt;"thefile" &lt;/span&gt;+ index.ToString(&lt;span style="color: #a31515;"&gt;"00"&lt;/span&gt;) + &lt;span style="color: #a31515;"&gt;".txt"&lt;/span&gt;;
            &lt;span style="color: blue;"&gt;string &lt;/span&gt;filePath = folder + fileName;

            &lt;span style="color: blue;"&gt;byte&lt;/span&gt;[] encodedText = &lt;span style="color: #2b91af;"&gt;Encoding&lt;/span&gt;.Unicode.GetBytes(text);

            &lt;span style="color: #2b91af;"&gt;FileStream &lt;/span&gt;sourceStream = &lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;FileStream&lt;/span&gt;(filePath,
                &lt;span style="color: #2b91af;"&gt;FileMode&lt;/span&gt;.Append, &lt;span style="color: #2b91af;"&gt;FileAccess&lt;/span&gt;.Write, &lt;span style="color: #2b91af;"&gt;FileShare&lt;/span&gt;.None,
                bufferSize: 4096, useAsync: &lt;span style="color: blue;"&gt;true&lt;/span&gt;);

            &lt;span style="color: #2b91af;"&gt;Task &lt;/span&gt;theTask = sourceStream.WriteAsync(encodedText, 0, encodedText.Length);
            sourceStreams.Add(sourceStream);

            tasks.Add(theTask);
       }
    
        &lt;span style="color: blue;"&gt;await &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Task&lt;/span&gt;.WhenAll(tasks);
    }

    &lt;span style="color: blue;"&gt;finally &lt;/span&gt;{
        &lt;span style="color: blue;"&gt;foreach &lt;/span&gt;(&lt;span style="color: #2b91af;"&gt;FileStream &lt;/span&gt;sourceStream &lt;span style="color: blue;"&gt;in &lt;/span&gt;sourceStreams)
        {
            sourceStream.Close();
        }
    }
}
&lt;/pre&gt;
&lt;p&gt;The WriteAsync and ReadAsync methods allow you to specify a &lt;a href="http://msdn.microsoft.com/en-us/library/system.threading.cancellationtoken(VS.110).aspx"&gt;CancellationToken&lt;/a&gt;, which can be used to cancel the operation mid-stream.&amp;nbsp; The &lt;a href="http://blogs.msdn.com/b/vbteam/archive/2011/05/10/simultaneous-async-tasks.aspx"&gt;Simultaneous Async Tasks&lt;/a&gt; blog has an example of cancellation.&amp;nbsp; Additional information is provided in the &lt;a href="http://msdn.microsoft.com/en-us/library/dd997364.aspx"&gt;Cancellation&lt;/a&gt; topic.&lt;/p&gt;
&lt;p&gt;Thanks to Stephen Toub for providing technical guidance.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;See Also&lt;/strong&gt;&amp;nbsp;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/kztecsys(vs.110).aspx"&gt;Asynchronous File I/O&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/hh191443(v=VS.110).aspx"&gt;Asynchronous Programming with Async and Await (C# and Visual Basic)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://blogs.msdn.com/b/vbteam/archive/2011/04/13/async-feature-control-flow.aspx"&gt;Async Feature Control Flow blog&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://blogs.msdn.com/b/vbteam/archive/2011/05/10/simultaneous-async-tasks.aspx"&gt;Simultaneous Async Tasks blog&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Resources&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-US/vstudio/async"&gt;Details and Download Page&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://social.msdn.microsoft.com/Forums/en-US/async/threads"&gt;Forum for Feedback and Questions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://connect.microsoft.com/"&gt;Microsoft Connect for Bugs and Suggestions&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10258951" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/-NET+Framework/">.NET Framework</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/C_2300_/">C#</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/parallel_2D00_programming/">parallel-programming</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/task+cancellation/">task cancellation</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/Visual+Studio+Async+CTP/">Visual Studio Async CTP</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/async/">async</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/await/">await</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/asynchronous+programming/">asynchronous programming</category></item><item><title>Introduction to the Roslyn Scripting API</title><link>http://blogs.msdn.com/b/csharpfaq/archive/2011/12/02/introduction-to-the-roslyn-scripting-api.aspx</link><pubDate>Fri, 02 Dec 2011 20:56:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10243830</guid><dc:creator>CSharpFAQ</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/csharpfaq/rsscomments.aspx?WeblogPostID=10243830</wfw:commentRss><comments>http://blogs.msdn.com/b/csharpfaq/archive/2011/12/02/introduction-to-the-roslyn-scripting-api.aspx#comments</comments><description>&lt;p&gt;&lt;em&gt;by Brian Rasmussen&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;In this post we take a look at how the Roslyn Scripting API can enable applications to evaluate code at runtime. While this has been possible since the dawn of .NET through the use of Reflection, Lightweight Code Generation, CodeDom, etc., it has never been particularly easy. All of these mechanisms are either hard to use, require MSIL knowledge, or just have inherent limitations.&lt;/p&gt;
&lt;p&gt;Roslyn not only makes dynamic code evaluation a lot easier, it also provides the necessary APIs to implement a fully featured scripting experience. This post covers the basics of the Scripting API in Roslyn. For an example of how Roslyn can be used to implement a rich environment for entering and executing code at runtime take a look at the C# Interactive window implemented in the Roslyn CTP. The C# Interactive window provides syntax highlighting, intellisense, and even refactoring for code supplied at runtime.&lt;/p&gt;
&lt;p&gt;This post uses C# as an example because that is what Roslyn supports for scripting in this first CTP.&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;The Basics&lt;/h2&gt;
&lt;p&gt;Let&amp;rsquo;s start by looking at a very simple example. Let&amp;rsquo;s just execute some code dynamically. To do that all we need is an instance of the ScriptEngine class. Specifically we need the C# version defined in Roslyn.Scripting.CSharp. All we have to do is create an instance of ScriptEngine and call the Execute method with the code to be evaluated. The Roslyn scripting version of Hello World is as simple as:&lt;/p&gt;
&lt;pre style="background: white; color: black; font-family: Consolas;"&gt;&lt;span style="color: blue;"&gt;using&lt;/span&gt;&amp;nbsp;Roslyn.Scripting.CSharp;
 
&lt;span style="color: blue;"&gt;namespace&lt;/span&gt;&amp;nbsp;RoslynScriptingDemo
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;class&lt;/span&gt;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;Program&lt;/span&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 style="color: blue;"&gt;static&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;void&lt;/span&gt;&amp;nbsp;Main(&lt;span style="color: blue;"&gt;string&lt;/span&gt;[]&amp;nbsp;args)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&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 style="color: blue;"&gt;var&lt;/span&gt;&amp;nbsp;engine&amp;nbsp;=&amp;nbsp;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;ScriptEngine&lt;/span&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;engine.Execute(&lt;span style="color: #a31515;"&gt;@"System.Console.WriteLine(""Hello&amp;nbsp;Roslyn"");"&lt;/span&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;/pre&gt;
&lt;p&gt;The solution needs references to Roslyn.Compilers and Roslyn.Compilers.CSharp.&lt;/p&gt;
&lt;p&gt;Notice, we did not have to use APIs that take MSIL or that carefully build up a tree of code that then needs to be compiled and executed. We can simply send the ScriptEngine text snippets of the code we write all the time. The ScriptEngine takes care of the rest to execute it.&lt;/p&gt;
&lt;h2&gt;Execution Context&lt;/h2&gt;
&lt;p&gt;Unfortunately the code above does have a few limitations. The first of which is that there&amp;rsquo;s no cumulative execution context, so if we define a variable as part of one code snippet and then try to access the variable from another code snippet, we will get an exception. For example, if we change the body of Main to something like this:&lt;/p&gt;
&lt;pre style="background: white; color: black; font-family: Consolas;"&gt;&lt;span style="color: blue;"&gt;var&lt;/span&gt;&amp;nbsp;engine&amp;nbsp;=&amp;nbsp;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;ScriptEngine&lt;/span&gt;();
 
engine.Execute(&lt;span style="color: #a31515;"&gt;@"var&amp;nbsp;a&amp;nbsp;=&amp;nbsp;42;"&lt;/span&gt;);
engine.Execute(&lt;span style="color: #a31515;"&gt;@"System.Console.WriteLine(a);"&lt;/span&gt;);&lt;/pre&gt;
&lt;p&gt;We will get a compilation error from the scripting engine as the statements are treated as separate compilations. Fortunately, the Scripting API provides the Session type that can be used as an execution context when calling Execute. A session stores the context of a series of submissions, so by providing a session we can execute the code above and get the expected result.&lt;/p&gt;
&lt;pre style="background: white; color: black; font-family: Consolas;"&gt;&lt;span style="color: blue;"&gt;using&lt;/span&gt;&amp;nbsp;Roslyn.Scripting.CSharp;
&lt;span style="color: blue;"&gt;using&lt;/span&gt;&amp;nbsp;Roslyn.Scripting;
 
&lt;span style="color: blue;"&gt;namespace&lt;/span&gt;&amp;nbsp;RoslynScriptingDemo
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;class&lt;/span&gt;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;Program&lt;/span&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 style="color: blue;"&gt;static&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;void&lt;/span&gt;&amp;nbsp;Main(&lt;span style="color: blue;"&gt;string&lt;/span&gt;[]&amp;nbsp;args)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&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 style="color: blue;"&gt;var&lt;/span&gt;&amp;nbsp;engine&amp;nbsp;=&amp;nbsp;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;ScriptEngine&lt;/span&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 style="color: blue;"&gt;var&lt;/span&gt;&amp;nbsp;session&amp;nbsp;=&amp;nbsp;&lt;span style="color: #2b91af;"&gt;Session&lt;/span&gt;.Create();
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;engine.Execute(&lt;span style="color: #a31515;"&gt;@"var&amp;nbsp;a&amp;nbsp;=&amp;nbsp;42;"&lt;/span&gt;,&amp;nbsp;session);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;engine.Execute(&lt;span style="color: #a31515;"&gt;@"System.Console.WriteLine(a);"&lt;/span&gt;,&amp;nbsp;session);
&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;/pre&gt;
&lt;p&gt;We can even define a new method, store it in the session, and then invoke it like this.&lt;/p&gt;
&lt;pre style="background: white; color: black; font-family: Consolas;"&gt;engine.Execute(&lt;span style="color: #a31515;"&gt;@"void&amp;nbsp;Foo()&amp;nbsp;{&amp;nbsp;System.Console.WriteLine(""Foo"");&amp;nbsp;}"&lt;/span&gt;,&amp;nbsp;session);
engine.Execute(&lt;span style="color: #a31515;"&gt;@"Foo();"&lt;/span&gt;,&amp;nbsp;session);&lt;/pre&gt;
&lt;h2&gt;Interacting with the Host Application&lt;/h2&gt;
&lt;p&gt;However, we still cannot interact with the hosting application. Obviously scripting is of limited use if the user supplied code cannot interact with the application somehow. To address this we need to provide a host object to the session.&lt;/p&gt;
&lt;p&gt;The code submissions will automatically have access to any public members of the host object. To supply a host object, we pass in an instance of any type we would like to use as an interface between the host application and the session. Furthermore, we have to make the ScriptEngine aware of this type as well. To do that we pass in a reference to the assembly defining our host object type. With that the setup code now looks as follows:&lt;/p&gt;
&lt;pre style="background: white; color: black; font-family: Consolas;"&gt;&lt;span style="color: blue;"&gt;var&lt;/span&gt;&amp;nbsp;hostObject&amp;nbsp;=&amp;nbsp;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;HostObject&lt;/span&gt;();
&lt;span style="color: blue;"&gt;var&lt;/span&gt;&amp;nbsp;engine&amp;nbsp;=&amp;nbsp;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;ScriptEngine&lt;/span&gt;(&lt;span style="color: blue;"&gt;new&lt;/span&gt;[]&amp;nbsp;{&amp;nbsp;hostObject.GetType().Assembly.Location&amp;nbsp;});
&lt;span style="color: blue;"&gt;var&lt;/span&gt;&amp;nbsp;session&amp;nbsp;=&amp;nbsp;&lt;span style="color: #2b91af;"&gt;Session&lt;/span&gt;.Create(hostObject);&lt;/pre&gt;
&lt;p&gt;The host object can be of any type. The ScriptEngine binds any free identifier to public members of the type, as if the type's member names were globally accessible from the script. For the sake of this demo, let&amp;rsquo;s just make it as simple as possible.&lt;/p&gt;
&lt;pre style="background: white; color: black; font-family: Consolas;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;class&lt;/span&gt;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;HostObject&lt;/span&gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&amp;nbsp;Value&amp;nbsp;=&amp;nbsp;0;
}&lt;/pre&gt;
&lt;p&gt;With the code above, we can now access the Value field on our hostObject instance like this:&lt;/p&gt;
&lt;pre style="background: white; color: black; font-family: Consolas;"&gt;engine.Execute(&lt;span style="color: #a31515;"&gt;@"Value&amp;nbsp;=&amp;nbsp;42;"&lt;/span&gt;,&amp;nbsp;session);
engine.Execute(&lt;span style="color: #a31515;"&gt;@"System.Console.WriteLine(Value);"&lt;/span&gt;,&amp;nbsp;session);&lt;/pre&gt;
&lt;p&gt;Notice how the instance's members are implicitly available within the session. We simply access any public member on the instance.&lt;/p&gt;
&lt;p&gt;The examples we have been looking at so far are really simple, so let&amp;rsquo;s round off this introduction by adding some functionality that resembles what an application might do with the scripting API. Imagine we have a Report type like the one listed below.&lt;/p&gt;
&lt;pre style="background: white; color: black; font-family: Consolas;"&gt;&lt;span style="color: green;"&gt;//&amp;nbsp;This&amp;nbsp;will&amp;nbsp;act&amp;nbsp;as&amp;nbsp;our&amp;nbsp;host&amp;nbsp;object&lt;/span&gt;
&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;class&lt;/span&gt;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;Report&lt;/span&gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: green;"&gt;//&amp;nbsp;Internal&amp;nbsp;state&lt;/span&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;private&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;readonly&lt;/span&gt;&amp;nbsp;List&amp;lt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&amp;gt;&amp;nbsp;values&amp;nbsp;=&amp;nbsp;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&amp;nbsp;List&amp;lt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&amp;gt;();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;private&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&amp;nbsp;result;
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: green;"&gt;//&amp;nbsp;Encapsulate&amp;nbsp;the&amp;nbsp;internal&amp;nbsp;data&amp;nbsp;structure&lt;/span&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;void&lt;/span&gt;&amp;nbsp;AddValue(&lt;span style="color: blue;"&gt;int&lt;/span&gt;&amp;nbsp;value)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;values.Add(value);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: green;"&gt;//&amp;nbsp;Allow&amp;nbsp;read-only&amp;nbsp;enumeration&amp;nbsp;of&amp;nbsp;values&lt;/span&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;IEnumerable&amp;lt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&amp;gt;&amp;nbsp;Values&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;return&lt;/span&gt;&amp;nbsp;values;&amp;nbsp;}&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: green;"&gt;//&amp;nbsp;User&amp;nbsp;code&amp;nbsp;will&amp;nbsp;provide&amp;nbsp;the&amp;nbsp;implementation&amp;nbsp;for&amp;nbsp;these&amp;nbsp;methods&lt;/span&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;Action&amp;nbsp;GetValues;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;Func&amp;lt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&amp;gt;&amp;nbsp;CalculateResult;
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: green;"&gt;//&amp;nbsp;This&amp;nbsp;method&amp;nbsp;may&amp;nbsp;be&amp;nbsp;called&amp;nbsp;by&amp;nbsp;both&amp;nbsp;the&amp;nbsp;host&amp;nbsp;application&amp;nbsp;and&amp;nbsp;the&amp;nbsp;user&amp;nbsp;code&lt;/span&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;void&lt;/span&gt;&amp;nbsp;PrintResult()
&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 style="color: blue;"&gt;var&lt;/span&gt;&amp;nbsp;get&amp;nbsp;=&amp;nbsp;GetValues;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;if&lt;/span&gt;&amp;nbsp;(get&amp;nbsp;!=&amp;nbsp;&lt;span style="color: blue;"&gt;null&lt;/span&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;get();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&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 style="color: blue;"&gt;var&lt;/span&gt;&amp;nbsp;calc&amp;nbsp;=&amp;nbsp;CalculateResult;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;if&lt;/span&gt;&amp;nbsp;(calc&amp;nbsp;!=&amp;nbsp;&lt;span style="color: blue;"&gt;null&lt;/span&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;result&amp;nbsp;=&amp;nbsp;calc();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Console.WriteLine(&lt;span style="color: #a31515;"&gt;"The&amp;nbsp;result&amp;nbsp;of&amp;nbsp;the&amp;nbsp;calculation&amp;nbsp;is&amp;nbsp;{0}"&lt;/span&gt;,&amp;nbsp;result);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
}&lt;/pre&gt;
&lt;p&gt;The class stores an internal list of numbers and a result. It provides an AddValue method to populate the list and a read-only iterator. The result can be printed by calling the PrintResult method. User-supplied code determines how the list is actually populated and how the result is calculated.&lt;/p&gt;
&lt;p&gt;The Report class exposes two delegate types, GetValues and CalculateResult for this purpose. When PrintResult is called it invokes the delegates to populate the list of values and calculate the result. Our simple application may look something like this:&lt;/p&gt;
&lt;pre style="background: white; color: black; font-family: Consolas;"&gt;&lt;span style="color: blue;"&gt;static&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;void&lt;/span&gt;&amp;nbsp;Main(&lt;span style="color: blue;"&gt;string&lt;/span&gt;[]&amp;nbsp;args)
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;var&lt;/span&gt;&amp;nbsp;report&amp;nbsp;=&amp;nbsp;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;Report&lt;/span&gt;();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;var&lt;/span&gt;&amp;nbsp;engine&amp;nbsp;=&amp;nbsp;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;ScriptEngine&lt;/span&gt;(&lt;span style="color: blue;"&gt;new&lt;/span&gt;[]&amp;nbsp;{&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: #a31515;"&gt;"System.Core"&lt;/span&gt;,&amp;nbsp;report.GetType().Assembly.Location&amp;nbsp;});
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;var&lt;/span&gt;&amp;nbsp;session&amp;nbsp;=&amp;nbsp;&lt;span style="color: #2b91af;"&gt;Session&lt;/span&gt;.Create(report);
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;engine.Execute(&lt;span style="color: #a31515;"&gt;@"using&amp;nbsp;System.Linq;"&lt;/span&gt;,&amp;nbsp;session);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;engine.Execute(
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: #a31515;"&gt;@"GetValues&amp;nbsp;=&amp;nbsp;()&amp;nbsp;=&amp;gt;&amp;nbsp;{&amp;nbsp;for(int&amp;nbsp;i&amp;nbsp;=&amp;nbsp;1;&amp;nbsp;i&amp;nbsp;&amp;lt;=&amp;nbsp;3;&amp;nbsp;i++)&amp;nbsp;AddValue(i);&amp;nbsp;};"&lt;/span&gt;,
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;session);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;engine.Execute(&lt;span style="color: #a31515;"&gt;@"CalculateResult&amp;nbsp;=&amp;nbsp;()&amp;nbsp;=&amp;gt;&amp;nbsp;Values.Sum();"&lt;/span&gt;,&amp;nbsp;session);
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;report.PrintResult();
}&lt;/pre&gt;
&lt;p&gt;Admittedly this is still a simple example, but it does illustrate how script code can be used to customize our host application.&lt;/p&gt;
&lt;p&gt;Notice how we can assign anonymous methods to the delegate members, and the host application can call them. Also, notice how we can access the LINQ extension method Sum in the user supplied code. To enable this we need to let ScriptEngine reference System.Core and import the System.Linq namespace.&lt;/p&gt;
&lt;p&gt;That completes the overview of how to use Roslyn to add scripting support to an application. For more information on the Roslyn Scripting API please see the &lt;a href="http://www.microsoft.com/download/en/details.aspx?displaylang=en&amp;amp;id=27745"&gt;walkthroughs&lt;/a&gt; and &lt;a href="http://www.microsoft.com/download/en/details.aspx?id=27744"&gt;The Roslyn Project Overview&lt;/a&gt;.&lt;/p&gt;
&lt;h2&gt;About the author&lt;/h2&gt;
&lt;p&gt;Brian is a Senior SDET at Microsoft working on the C# and VB language services in Roslyn. Before joining Microsoft Brian was a Microsoft MVP for Visual C# for four years. Brian can be found on Twitter (&lt;a href="http://twitter.com/kodehoved"&gt;@kodehoved&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=10243830" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/C_2300_/">C#</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/roslyn/">roslyn</category></item><item><title>Using the Roslyn Symbol API</title><link>http://blogs.msdn.com/b/csharpfaq/archive/2011/11/23/using-the-roslyn-symbol-api.aspx</link><pubDate>Wed, 23 Nov 2011 23:10:38 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10241117</guid><dc:creator>CSharpFAQ</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/csharpfaq/rsscomments.aspx?WeblogPostID=10241117</wfw:commentRss><comments>http://blogs.msdn.com/b/csharpfaq/archive/2011/11/23/using-the-roslyn-symbol-api.aspx#comments</comments><description>&lt;blockquote&gt;   &lt;p&gt;&lt;em&gt;by Kevin Pilch-Bisson&lt;/em&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;I’m back again, to move along to the next stage of the compiler pipeline, and take a look at working with Symbols in the using the &lt;a href="http://msdn.com/roslyn/"&gt;Roslyn CTP&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;The Roslyn CTP’s symbol API provides a top-down view of all the symbols available. Before we get to symbols though, we need to start looking at how to give the compiler enough context to tell us about symbols. While syntax trees are mostly context free and can stand on their own, in order to learn about symbols we need to have a collection of syntax trees, the references passed to the compiler, and any options in effect. In Roslyn, we use the Compilation type to group these things together. Conceptually, a Compilation represents one invocation of the csc.exe command line, or a single project in Visual Studio.&lt;/p&gt;  &lt;p&gt;Let’s start by creating a compilation that includes something like the simple file we used &lt;a href="http://blogs.msdn.com/b/csharpfaq/archive/2011/11/03/using-the-roslyn-syntax-api.aspx"&gt;last time&lt;/a&gt; when talking about syntax trees:&lt;/p&gt;  &lt;table border="1" cellspacing="0" cellpadding="2" width="1"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td valign="top" width="1"&gt;         &lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;using &lt;/span&gt;System;
&lt;span style="color: blue;"&gt;using &lt;/span&gt;System.IO;
&lt;span style="color: blue;"&gt;using &lt;/span&gt;System.Linq;
&lt;span style="color: blue;"&gt;using &lt;/span&gt;Roslyn.Compilers;
&lt;span style="color: blue;"&gt;using &lt;/span&gt;Roslyn.Compilers.CSharp;

&lt;span style="color: blue;"&gt;class &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Program
&lt;/span&gt;{
    &lt;span style="color: blue;"&gt;static void &lt;/span&gt;Main(&lt;span style="color: blue;"&gt;string&lt;/span&gt;[] args)
    {
        &lt;span style="color: blue;"&gt;var &lt;/span&gt;syntaxTree = &lt;span style="color: rgb(43, 145, 175);"&gt;SyntaxTree&lt;/span&gt;.ParseCompilationUnit(&lt;span style="color: rgb(163, 21, 21);"&gt;@&amp;quot;
class C
{
    static void Main()
    {
        if (true)
            Console.WriteLine(&amp;quot;&amp;quot;Hello, World!&amp;quot;&amp;quot;);
    }
}&amp;quot;&lt;/span&gt;);

        &lt;span style="color: blue;"&gt;var &lt;/span&gt;compilation = &lt;span style="color: rgb(43, 145, 175);"&gt;Compilation&lt;/span&gt;.Create(&lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;test.dll&amp;quot;&lt;/span&gt;,
            syntaxTrees: &lt;span style="color: blue;"&gt;new&lt;/span&gt;[] { syntaxTree },
            references: &lt;span style="color: blue;"&gt;new &lt;/span&gt;[]
                {
                    &lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;AssemblyFileReference&lt;/span&gt;(&lt;span style="color: blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color: blue;"&gt;object&lt;/span&gt;).Assembly.Location),
                    &lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;AssemblyFileReference&lt;/span&gt;(&lt;span style="color: blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color: rgb(43, 145, 175);"&gt;Enumerable&lt;/span&gt;).Assembly.Location),
                });
    }
}
        &lt;/pre&gt;
        &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;

&lt;p&gt;Aside: You’ll find that many parts of the Roslyn APIs use optional arguments, and it’s convenient to call them with named parameters. The reason for this is that we’re exposing an immutable API, so we want to allow you to specify all the options right at creation time, but we don’t want to force you to type them all. In a mutable model, we’d probably leave many of these things out of the constructors/factory methods and allow you to set them via properties.&lt;/p&gt;

&lt;p&gt;Now that we’ve got a Compilation, what are the interesting things to do with it? Well, there are a few.&lt;/p&gt;

&lt;h1&gt;“Change” it&lt;/h1&gt;

&lt;p&gt;Like SyntaxTrees and many other parts of the Roslyn API, Compilations are immutable, so we can’t really change them. However, there are methods like AddReferences() and RemoveSyntaxTrees(), that will return a new Compilation object with those changes (just like string.Append)&lt;/p&gt;

&lt;h1&gt;Find errors and warnings&lt;/h1&gt;

&lt;p&gt;The Compilation object makes it possible for us to programmatically determine what errors and warnings exist in a piece of code. There are two different APIs for this on the Compilation object. First of all you can call GetDiagnostics, which returns all of the errors for the Compilation. This requires doing a significant amount of work (almost as much as just compiling), so it can take a while. &lt;/p&gt;

&lt;p&gt;In addition to GetDiagnostics, the Compilation has a method named “GetDeclarationDiagnostics”. This is meant to be a somewhat less expensive call, and one that doesn’t throw away the results of its work. You can think of the declaration diagnostics as all the errors that happen outside the curly braces of method bodies and property accessors. Finding them requires completely filling out the symbol table of the Compilation, but it doesn’t require looking at method bodies. In contrast, GetDiagnostics first calculates the declaration diagnostics, and then binds each method body in turn. Keeping the fully bound form of every method would take a lot of memory for a large Compilation, and so the bound information is released once the diagnostics have been collected.&lt;/p&gt;

&lt;table border="1" cellspacing="0" cellpadding="2" width="1"&gt;&lt;tbody&gt;
    &lt;tr&gt;
      &lt;td valign="top" width="1"&gt;
        &lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;var &lt;/span&gt;diagnostics = compilation.GetDiagnostics();
&lt;span style="color: blue;"&gt;foreach &lt;/span&gt;(&lt;span style="color: blue;"&gt;var &lt;/span&gt;d &lt;span style="color: blue;"&gt;in &lt;/span&gt;diagnostics)
{
    &lt;span style="color: blue;"&gt;var &lt;/span&gt;lineSpan = d.Location.GetLineSpan(usePreprocessorDirectives: &lt;span style="color: blue;"&gt;true&lt;/span&gt;);
    &lt;span style="color: blue;"&gt;var &lt;/span&gt;startLine = lineSpan.StartLinePosition.Line;
    &lt;span style="color: rgb(43, 145, 175);"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;Line {0}: {1}&amp;quot;&lt;/span&gt;, startLine, d.Info.GetMessage());
}&lt;/pre&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;

&lt;h1&gt;Compile it!&lt;/h1&gt;

&lt;p&gt;We can write out the bytes to an assembly using Emit(). In case we want to Emit if possible, and display diagnostics if not, the Emit call returns a result that includes the diagnostics. This means that we can skip a call to GetDiagnostics before trying Emit, which means that nothing has to look at each method body twice.&lt;/p&gt;

&lt;table border="1" cellspacing="0" cellpadding="2" width="1"&gt;&lt;tbody&gt;
    &lt;tr&gt;
      &lt;td valign="top" width="1"&gt;
        &lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;using &lt;/span&gt;(&lt;span style="color: blue;"&gt;var &lt;/span&gt;file = &lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;FileStream&lt;/span&gt;(&lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;test.dll&amp;quot;&lt;/span&gt;, FileMode.Create))
{
    &lt;span style="color: blue;"&gt;var &lt;/span&gt;result = compilation.Emit(file);
}
        &lt;/pre&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;

&lt;h1&gt;Examine the symbol table&lt;/h1&gt;

&lt;p&gt;One of the most interesting things we can do is look at the symbol table and examine the types and members inside of it, sort of the way you would do with Reflection.&lt;/p&gt;

&lt;p&gt;To examine every type in the Compilation, you can start with the GlobalNamespace property of the Compilation. One thing to note is that the Compilation’s GlobalNamespace is what the compiler uses when doing lookups, so it includes everything defined in the Compilation &lt;b&gt;and&lt;/b&gt; in its references. If you want to see only things that are actually defined in the Compilation, you can look at the “Assembly.GlobalNamespace” instead. Let’s look at an example where we find every member that contains the letter “a” in its name:&lt;/p&gt;

&lt;table border="1" cellspacing="0" cellpadding="2" width="1"&gt;&lt;tbody&gt;
    &lt;tr&gt;
      &lt;td valign="top" width="1"&gt;
        &lt;pre class="code"&gt;ReportMethods(compilation.Assembly.GlobalNamespace);
        &lt;/pre&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;

&lt;p&gt;and&lt;/p&gt;

&lt;table border="1" cellspacing="0" cellpadding="2" width="1"&gt;&lt;tbody&gt;
    &lt;tr&gt;
      &lt;td valign="top" width="1"&gt;
        &lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;private static void &lt;/span&gt;ReportMethods(&lt;span style="color: rgb(43, 145, 175);"&gt;NamespaceSymbol &lt;/span&gt;namespaceSymbol)
{
    &lt;span style="color: blue;"&gt;foreach &lt;/span&gt;(&lt;span style="color: blue;"&gt;var &lt;/span&gt;type &lt;span style="color: blue;"&gt;in &lt;/span&gt;namespaceSymbol.GetTypeMembers())
    {
        ReportMethods(type);
    }

    &lt;span style="color: blue;"&gt;foreach &lt;/span&gt;(&lt;span style="color: blue;"&gt;var &lt;/span&gt;childNs &lt;span style="color: blue;"&gt;in &lt;/span&gt;namespaceSymbol.GetNamespaceMembers())
    {
        ReportMethods(childNs);
    }
}

&lt;span style="color: blue;"&gt;private static void &lt;/span&gt;ReportMethods(&lt;span style="color: rgb(43, 145, 175);"&gt;NamedTypeSymbol &lt;/span&gt;type)
{
    &lt;span style="color: blue;"&gt;foreach &lt;/span&gt;(&lt;span style="color: blue;"&gt;var &lt;/span&gt;member &lt;span style="color: blue;"&gt;in &lt;/span&gt;type.GetMembers())
    {
        &lt;span style="color: blue;"&gt;if &lt;/span&gt;(member.CanBeReferencedByName &amp;amp;&amp;amp;
            member.Name.Contains(&lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;a&amp;quot;&lt;/span&gt;))
        {
            &lt;span style="color: rgb(43, 145, 175);"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;Found {0}&amp;quot;&lt;/span&gt;, member.ToDisplayString());
        }
    }

    &lt;span style="color: blue;"&gt;foreach &lt;/span&gt;(&lt;span style="color: blue;"&gt;var &lt;/span&gt;nested &lt;span style="color: blue;"&gt;in &lt;/span&gt;type.GetTypeMembers())
    {
        ReportMethods(nested);
    }
}
        &lt;/pre&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;

&lt;p&gt;Note the handy “CanBeReferencedByName” property, which saves us from including things like accessor methods for a property, that exist in the symbol table, but which users aren’t allowed to actually name. Also, take note of the “ToDisplayString()” extension method, which allows you to generate various forms of symbol names, including some human readable ones.&lt;/p&gt;

&lt;p&gt;Alternatively, if there is a specific type you want to find, you can use the GetTypeByMetadataName method. We needed a way to represent fully qualified names type names that include generic arity and nestedness. Rather than define our own format, we decided to use the metadata format, which means that to use this API, you’ll need to use + to separate nested types, and encode the generic arity with `n where n is the number of type arguments. For example, to look up the nested Enumerator type in List&amp;lt;T&amp;gt;, we’d use a string like “System.Collections.Generic.List`1+Enumerator”.&lt;/p&gt;

&lt;p&gt;One question we get is that if this model is similar to reflection, why isn’t it easier to get to the System.Type for a TypeSymbol? The reason is that the compiler may be targeting a different runtime than the one that your code is running on – consider a compilation for a Silverlight project for example. There is no way to load a System.Type for some type in the reference assembly to mscorlib.dll. At best you would find the corresponding System.Type in the desktop CLR, which may or may not be what you need.&lt;/p&gt;

&lt;h1&gt;Analyze specific parts of it&lt;/h1&gt;

&lt;p&gt;It may be interesting to pick a particular expression and see what method is going to be invoked, or similar semantic questions. We’ll look at this in more detail in the next post, but if you want to get a head start, take a look at the GetSemanticModel method on Compilation, and it’s resulting SemanticModel object.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10241117" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/roslyn/">roslyn</category></item><item><title>Roslyn Syntax Visualizers</title><link>http://blogs.msdn.com/b/csharpfaq/archive/2011/11/18/roslyn-syntax-visualizers.aspx</link><pubDate>Sat, 19 Nov 2011 02:20:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10238726</guid><dc:creator>CSharpFAQ</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/csharpfaq/rsscomments.aspx?WeblogPostID=10238726</wfw:commentRss><comments>http://blogs.msdn.com/b/csharpfaq/archive/2011/11/18/roslyn-syntax-visualizers.aspx#comments</comments><description>&lt;p&gt;Hi All! A few weeks ago, we &lt;a href="http://blogs.msdn.com/b/csharpfaq/archive/2011/10/19/introducing-the-microsoft-roslyn-ctp.aspx"&gt;announced the Microsoft "Roslyn" CTP&lt;/a&gt;. I hope many of you have had a chance to download the CTP and take it for a spin :) If you haven&amp;rsquo;t, do &lt;a href="http://www.msdn.com/roslyn"&gt;give it a try&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;To recap &amp;ndash; in the Roslyn CTP, we are previewing a powerful set of language services APIs that you can use within your apps to reason about C# and VB code. We are also previewing IDE extensibility points for C# and VB that will allow you to build rich code refactorings, quick fixes etc. that run inside Visual Studio.&lt;/p&gt;
&lt;p&gt;In this post, I just wanted to point out a couple of tools that you may be interested in if you are working with the CTP &amp;ndash; the &lt;em&gt;Roslyn Syntax Visualizers&lt;/em&gt;. You can read more about these visualizers and how to install and use them on the following blog post &amp;ndash; &lt;a href="http://blogs.msdn.com/b/visualstudio/archive/2011/10/19/roslyn-syntax-visualizers.aspx"&gt;http://blogs.msdn.com/b/visualstudio/archive/2011/10/19/roslyn-syntax-visualizers.aspx&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The visualizers ship as samples in the CTP and allow you to visually inspect and explore Roslyn Syntax Trees (below screenshot shows an example). You can use these visualizers as debugging aids when you develop your own applications / code refactorings using the Roslyn APIs.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-33-48-metablogapi/0638.image_5F00_041BEEE7.png"&gt;&lt;img style="display: inline; background-image: none;" title="image" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-33-48-metablogapi/1727.image_5F00_thumb_5F00_38BC4B22.png" width="549" height="580" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Happy Coding! :)&lt;/p&gt;
&lt;p&gt;Shyam Namboodiripad &lt;br /&gt;Software Development Engineer in Test (Roslyn Compilers Team)&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10238726" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/C_2300_/">C#</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/Visual+Studio/">Visual Studio</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/C_2300_+compiler/">C# compiler</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/roslyn/">roslyn</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/Shyam+Namboodiripad/">Shyam Namboodiripad</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/Visual+Basic/">Visual Basic</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/Syntax+Visualizer/">Syntax Visualizer</category></item><item><title>Using the Roslyn Syntax API</title><link>http://blogs.msdn.com/b/csharpfaq/archive/2011/11/03/using-the-roslyn-syntax-api.aspx</link><pubDate>Thu, 03 Nov 2011 21:51:42 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10233785</guid><dc:creator>CSharpFAQ</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/csharpfaq/rsscomments.aspx?WeblogPostID=10233785</wfw:commentRss><comments>http://blogs.msdn.com/b/csharpfaq/archive/2011/11/03/using-the-roslyn-syntax-api.aspx#comments</comments><description>&lt;p&gt;&lt;i&gt;By Kevin Pilch-Bisson&lt;/i&gt;&lt;/p&gt;  &lt;p&gt;As &lt;a href="http://blogs.msdn.com/b/csharpfaq/archive/2011/10/19/introducing-the-microsoft-roslyn-ctp.aspx"&gt;promised&lt;/a&gt; back when we released the Roslyn CTP, here is the first of a series of blog posts that help explain the various parts of the Roslyn API. If you don’t have the Roslyn CTP, you can get it from our &lt;a href="http://msdn.com/roslyn/"&gt;MSDN page&lt;/a&gt;, and install it on top of Visual Studio SP1 and the VSSDK. If you want to get into using the Roslyn CTP seriously, I highly recommend taking a look at the overview document, and the various walkthroughs that we’ve put together.&lt;/p&gt;  &lt;h1&gt;Properties&lt;/h1&gt;  &lt;p&gt;The Syntax API in Roslyn has three two key properties. First, it is a full-fidelity model. Every character of a source file is represented in the API, which means that it also round trips. You can parse a source file, call ToString on the resulting SyntaxTree and get the original file text back.&lt;/p&gt;  &lt;h1&gt;Key Types&lt;/h1&gt;  &lt;p&gt;The key types in the Syntax API of the Roslyn CTP are: SyntaxNode, SyntaxToken and SyntaxTrivia. SyntaxNode represents a non-terminal node in the language grammar, and is an immutable object. There are a variety of different derived types for different language elements, and also a Kind property that identifies what a particular SyntaxNode represents. SyntaxNodes are arranged into a tree, connected by the Parent and ChildNodes properties. Each type derived from SyntaxNode also includes named properties for element of that particular node type. For example, an IfStatementSyntax has properties for the IfToken, OpenParenToken, Condition, CloseParenToken, Statement, and ElseClauseOpt.&lt;/p&gt;  &lt;p&gt;SyntaxToken objects represet the terminals of the language grammar. They never have children, but do have a Parent property that is always a SyntaxNode. For performance reasons, SyntaxToken is a struct type. You can use the Kind property to determine what sort of token something is.&lt;/p&gt;  &lt;p&gt;Finally, things like whitespace and comments that can appear anywhere in a source file are represented as SyntaxTrivia. Each SyntaxTrivia is attached to a single token, as either LeadingTrivia or TrailingTrivia. SyntaxTrivia does not have a parent property, but it is possible to get the associated token via the Token property. Like SyntaxToken, SyntaxTrivia is a struct for performance reasons.&lt;/p&gt;  &lt;h1&gt;Parsing&lt;/h1&gt;  &lt;p&gt;Okay, enough talk, let’s see some code. First off, let’s see how we can get a hold of the SyntaxTree, and what it looks like. To start off with, you can parse some text using SyntaxTree.ParseCompilationUnit:&lt;/p&gt;  &lt;table border="1" cellspacing="0" cellpadding="2" width="532"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td valign="top" width="530"&gt;         &lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;using &lt;/span&gt;System;
&lt;span style="color: blue;"&gt;using &lt;/span&gt;System.Linq;
&lt;span style="color: blue;"&gt;using &lt;/span&gt;Roslyn.Compilers;
&lt;span style="color: blue;"&gt;using &lt;/span&gt;Roslyn.Compilers.CSharp;

&lt;span style="color: blue;"&gt;class &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Program
&lt;/span&gt;{
    &lt;span style="color: blue;"&gt;static void &lt;/span&gt;Main(&lt;span style="color: blue;"&gt;string&lt;/span&gt;[] args)
    {
        &lt;span style="color: blue;"&gt;var &lt;/span&gt;syntaxTree = &lt;span style="color: rgb(43, 145, 175);"&gt;SyntaxTree&lt;/span&gt;.ParseCompilationUnit(&lt;span style="color: rgb(163, 21, 21);"&gt;@&amp;quot;
using System;
class C
{
    static void M()
    {
        if (true)
            Console.WriteLine(&amp;quot;&amp;quot;Hello, World!&amp;quot;&amp;quot;);
    }
}&amp;quot;&lt;/span&gt;);
        &lt;span style="color: blue;"&gt;var &lt;/span&gt;ifStatement = (&lt;span style="color: rgb(43, 145, 175);"&gt;IfStatementSyntax&lt;/span&gt;)syntaxTree.
            Root.
            DescendentNodes().
            First(n =&amp;gt; n.Kind == &lt;span style="color: rgb(43, 145, 175);"&gt;SyntaxKind&lt;/span&gt;.IfStatement);
        &lt;span style="color: rgb(43, 145, 175);"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;Condition is '{0}'.&amp;quot;&lt;/span&gt;, ifStatement.Statement);
    }
}
        &lt;/pre&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;

&lt;p&gt;In this example, we parse some simple text and look at the resulting SyntaxTree. In this case, we search through the syntax tree looking for IfStatements and grab the first one we find. We cast that the the more strongly typed IfStatementSyntax type, and then we can look at the properties specific to if statements. In this case, we looked at “Statement” which is a SyntaxNode that represents the statement to execute if the condition is true.&lt;/p&gt;

&lt;h1&gt;Construction&lt;/h1&gt;

&lt;p&gt;Sometimes you don’t want to parse existing source code to build up a SyntaxTree. Instead you want to create brand new SyntaxNodes. The most common case for this is that you want to add or change some existing syntax, and you need the new things to transform to. With the Roslyn CTP, the way you create new SyntaxNode objects is through the set of factory methods on the “Syntax” class.&lt;/p&gt;

&lt;table border="1" cellspacing="0" cellpadding="2" width="1"&gt;&lt;tbody&gt;
    &lt;tr&gt;
      &lt;td valign="top" width="1"&gt;
        &lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;var &lt;/span&gt;newStatement = &lt;span style="color: rgb(43, 145, 175);"&gt;Syntax&lt;/span&gt;.ExpressionStatement(
    &lt;span style="color: rgb(43, 145, 175);"&gt;Syntax&lt;/span&gt;.InvocationExpression(
        &lt;span style="color: rgb(43, 145, 175);"&gt;Syntax&lt;/span&gt;.MemberAccessExpression(
            &lt;span style="color: rgb(43, 145, 175);"&gt;SyntaxKind&lt;/span&gt;.MemberAccessExpression,
            &lt;span style="color: rgb(43, 145, 175);"&gt;Syntax&lt;/span&gt;.IdentifierName(&lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;Console&amp;quot;&lt;/span&gt;),
            name: &lt;span style="color: rgb(43, 145, 175);"&gt;Syntax&lt;/span&gt;.IdentifierName(&lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;WriteLine&amp;quot;&lt;/span&gt;)),
        &lt;span style="color: rgb(43, 145, 175);"&gt;Syntax&lt;/span&gt;.ArgumentList(
            arguments: &lt;span style="color: rgb(43, 145, 175);"&gt;Syntax&lt;/span&gt;.SeparatedList&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;ArgumentSyntax&lt;/span&gt;&amp;gt;(
                &lt;span style="color: rgb(43, 145, 175);"&gt;Syntax&lt;/span&gt;.Argument(
                    expression: &lt;span style="color: rgb(43, 145, 175);"&gt;Syntax&lt;/span&gt;.LiteralExpression(
                        &lt;span style="color: rgb(43, 145, 175);"&gt;SyntaxKind&lt;/span&gt;.StringLiteralExpression,
                        &lt;span style="color: rgb(43, 145, 175);"&gt;Syntax&lt;/span&gt;.Literal(
                            text: &lt;span style="color: rgb(163, 21, 21);"&gt;@&amp;quot;&amp;quot;&amp;quot;Goodbye everyone!&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;,
                            value: &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;Goodbye everyone!&amp;quot;&lt;/span&gt;)))))));
        &lt;/pre&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;

&lt;p&gt;Here, we’re constructing a new statement to represent a call to “Console.WriteLine(&amp;quot;Goodbye everyone!&amp;quot;);”. As you can see, this involves specifying quite a lot of detail. That’s one of the drawbacks of a full-fidelity model, it also means that you need specify all of those details when you create things. If you look closely, you can also see that in some places I’m using C# 4.0’s named arguments. The reason for this is that many parameters in the Syntax construction APIs are defined to be optional. The reason for optional parameters is that when possible, you don’t need to specify a detail. For example, many tokens are always exactly the same (like keywords, punctuation, etc). If you omit an argument for them, a default version will be constructed for you. If you’d like, you CAN specify every token, including triva for each one.&lt;/p&gt;

&lt;p&gt;One thing you can do to reduce the verbosity is to sneak in calls to the various Syntax.Parse?? methods for elements of structure you are building up. This way, you can inline the code you want to have in a string instead of specifying the entire structure.&lt;/p&gt;

&lt;h1&gt;Rewriting&lt;/h1&gt;

&lt;p&gt;The next major piece of functionality in the Syntax API is re-writing, or modifying syntax trees. There are two approaches to this. The simplest is to just use one of the Replace APIs on SyntaxNode. The idea is that you get back a new tree, but with one of the descendants replaced.&lt;/p&gt;



&lt;table border="1" cellspacing="0" cellpadding="2" width="1"&gt;&lt;tbody&gt;
    &lt;tr&gt;
      &lt;td valign="top" width="1"&gt;
        &lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;var &lt;/span&gt;newTree = syntaxTree.Root.ReplaceNode(
    ifStatement.Statement,
    newStatement).Format();

&lt;span style="color: rgb(43, 145, 175);"&gt;Console&lt;/span&gt;.WriteLine(newTree);
          &lt;/pre&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;
We’ve successfully replace the body of the if statement. Because the trees are immutable, the original “SyntaxTree.Root” is completely unaffected. The newTree variable really is a brand new tree, but under the covers we actually re-use a lot of the data in an invisible fashion to be more efficient both in time and memory. Of note in the sample is the call to “Format()” on the new tree, which will apply a default formatting, and ensure that there is whitespace where there needs to be for the tree to round trip. 



&lt;p&gt;In some cases where you want to re-write multiple nodes, you might want more control over how the tree gets changed. To support this, the Roslyn CTP includes an abstract SyntaxRewriter class. I’ll give you a simple example that rewrites the tree above so that “M” is named “Main”, but there are obviously more interesting things that can be done here:&lt;/p&gt;

&lt;table border="1" cellspacing="0" cellpadding="2" width="1"&gt;&lt;tbody&gt;
    &lt;tr&gt;
      &lt;td valign="top" width="1"&gt;
        &lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;class &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Rewriter&lt;/span&gt;: &lt;span style="color: rgb(43, 145, 175);"&gt;SyntaxRewriter
&lt;/span&gt;{
    &lt;span style="color: blue;"&gt;protected override &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;SyntaxToken &lt;/span&gt;VisitToken(&lt;span style="color: rgb(43, 145, 175);"&gt;SyntaxToken &lt;/span&gt;token)
    {
        &lt;span style="color: blue;"&gt;if &lt;/span&gt;(token.Kind != &lt;span style="color: rgb(43, 145, 175);"&gt;SyntaxKind&lt;/span&gt;.IdentifierToken ||
            token.GetText() != &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;M&amp;quot;&lt;/span&gt;)
        {
            &lt;span style="color: blue;"&gt;return &lt;/span&gt;token;
        }

        &lt;span style="color: blue;"&gt;return &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Syntax&lt;/span&gt;.Identifier(&lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;Main&amp;quot;&lt;/span&gt;);
    }
}
        &lt;/pre&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;

&lt;p&gt;And you can use it like this:&lt;/p&gt;

&lt;table border="1" cellspacing="0" cellpadding="2" width="1"&gt;&lt;tbody&gt;
    &lt;tr&gt;
      &lt;td valign="top" width="1"&gt;
        &lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;var &lt;/span&gt;rewriter = &lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Rewriter&lt;/span&gt;();
newTree = rewriter.Visit(newTree);
&lt;span style="color: rgb(43, 145, 175);"&gt;Console&lt;/span&gt;.WriteLine(newTree);
        &lt;/pre&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;

&lt;p&gt;In this example, I’ve only used “VisitToken”, but there are actually a TON of “Visit” methods that you can override to rewrite specific types of nodes in a tree.&lt;/p&gt;

&lt;h1&gt;Closing&lt;/h1&gt;

&lt;p&gt;We’ve taken a brief look at some of the key concepts and types of the Syntax portion of the API in the Roslyn CTP, including parsing, constructing node from scratch, and rewriting existing nodes. Next time, we’ll move to the next stage of the compilation pipeline, and take a look at how Symbols are exposed in Roslyn.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10233785" width="1" height="1"&gt;</description></item><item><title>Introducing the Microsoft “Roslyn” CTP</title><link>http://blogs.msdn.com/b/csharpfaq/archive/2011/10/19/introducing-the-microsoft-roslyn-ctp.aspx</link><pubDate>Wed, 19 Oct 2011 21:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10226696</guid><dc:creator>CSharpFAQ</dc:creator><slash:comments>5</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/csharpfaq/rsscomments.aspx?WeblogPostID=10226696</wfw:commentRss><comments>http://blogs.msdn.com/b/csharpfaq/archive/2011/10/19/introducing-the-microsoft-roslyn-ctp.aspx#comments</comments><description>&lt;blockquote&gt;
&lt;p&gt;&lt;i&gt;By Kevin Pilch-Bisson&lt;/i&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;As Soma &lt;a href="http://blogs.msdn.com/b/somasegar/archive/2011/10/19/roslyn-ctp-available-now.aspx"&gt;mentioned&lt;/a&gt; earlier, today we&amp;rsquo;ve made a Community Technology Preview (CTP) &lt;a href="http://www.msdn.com/roslyn"&gt;available&lt;/a&gt; for &lt;a href="http://download.microsoft.com/download/F/4/7/F4700C93-A150-48D8-A62C-7D075E161F38/RoslynSetup.exe"&gt;download&lt;/a&gt;! The Roslyn project is a forward looking effort to make the wealth of language understanding that the compiler generates available to developers in other scenarios. A number of us have been working hard on getting enough of these pieces put together to have something to share with you, and we&amp;rsquo;re thrilled to be able to do that today.&lt;/p&gt;
&lt;p&gt;The foundation of this work is a new C# compiler, written in C# (and a new VB compiler written in VB too, see the &lt;a href="http://blogs.msdn.com/b/vbteam/archive/2011/10/19/introducing-the-microsoft-roslyn-ctp.aspx"&gt;VB Team blog&lt;/a&gt; for details). This compiler is written as a library that exposes a rich public API. Next up is a new language service written purely using that public API and exposing its own extensibility points to allow 3&lt;sup&gt;rd&lt;/sup&gt; parties to do amazing things inside Visual Studio with that language understanding.&lt;/p&gt;
&lt;h3&gt;Resources&lt;/h3&gt;
&lt;p&gt;The &lt;a href="http://blogs.msdn.com/b/visualstudio/archive/2011/10/19/introducing-the-microsoft-roslyn-ctp.aspx"&gt;Visual Studio blog&lt;/a&gt; gives a good overview of the various pieces that the CTP installs, but I thought I&amp;rsquo;d use this space to look at a little more code that uses the API.&lt;/p&gt;
&lt;p&gt;The &lt;a href="http://www.msdn.com/roslyn"&gt;Roslyn MSDN page&lt;/a&gt; contains pointers to an &lt;a href="http://go.microsoft.com/fwlink/?LinkID=230702"&gt;overview&lt;/a&gt; document that explains the basic concepts in the Roslyn API in greater detail. There are also several guided &lt;a href="http://go.microsoft.com/fwlink/?LinkID=231190"&gt;walkthroughs&lt;/a&gt; that each dive deeper into a particular area of the API. Finally if you install the CTP, there are numerous samples installed to the &amp;ldquo;My Documents&amp;rdquo; folder that you can build, run and experiment with.&lt;/p&gt;
&lt;p&gt;Watch this space in the next few weeks for a series of posts that will each explore one bit of the API.&lt;/p&gt;
&lt;h3&gt;Feedback&lt;/h3&gt;
&lt;p&gt;We&amp;rsquo;re very interested in your feedback about the Roslyn project! Because it&amp;rsquo;s early in the project we&amp;rsquo;re most interested in feedback about the API and general direction. Please get involved in the discussion about Roslyn on our &lt;a href="http://social.msdn.microsoft.com/forums/en-us/roslyn"&gt;forums&lt;/a&gt;. You can also file bugs on our &lt;a href="http://connect.microsoft.com/visualstudio"&gt;Connect site&lt;/a&gt;. Finally, some of the team members are also on Twitter, and will be keeping an eye on the &lt;a href="http://twitter.com/#!/search/realtime/%23RoslynCTP"&gt;#RoslynCTP&lt;/a&gt; hashtag if that is more convenient for you.&lt;/p&gt;
&lt;h3&gt;Disclaimer&lt;/h3&gt;
&lt;p&gt;&lt;b&gt;Please note&lt;/b&gt;: This is a technology preview and there are known limitations. While the shape of the public API surface is complete, only a subset of the C# and Visual Basic languages have been implemented at this time in the current CTP.&lt;/p&gt;
&lt;h3&gt;About the author&lt;/h3&gt;
&lt;p&gt;&lt;a href="mailto:kevinpi@microsoft.com"&gt;Kevin&lt;/a&gt; is a Development lead at Microsoft responsible for the C# and VB &lt;a href="http://blogs.msdn.com/b/kevinpilchbisson/archive/2004/01/28/63914.aspx"&gt;language services&lt;/a&gt; in Roslyn. He&amp;rsquo;s been at Microsoft for 9 years, and has spent all of it working in the language and IDE space. Kevin is a graduate of the University of Waterloo. He used to blog at &lt;a href="http://blogs.msdn.com/b/kevinpilchbisson"&gt;http://blogs.msdn.com/b/kevinpilchbisson&lt;/a&gt;, but has been spending more time on Twitter (&lt;a href="http://twitter.com/#!/Pilchie"&gt;@Pilchie&lt;/a&gt;) and on getting Roslyn into your hands lately.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10226696" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/IDE/">IDE</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/C_2300_/">C#</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/C_2300_+compiler/">C# compiler</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/roslyn/">roslyn</category></item><item><title>C# 2010 Soup to Nuts Series</title><link>http://blogs.msdn.com/b/csharpfaq/archive/2011/08/31/c-2010-soup-to-nuts-series.aspx</link><pubDate>Thu, 01 Sep 2011 00:02:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10203983</guid><dc:creator>CSharpFAQ</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/csharpfaq/rsscomments.aspx?WeblogPostID=10203983</wfw:commentRss><comments>http://blogs.msdn.com/b/csharpfaq/archive/2011/08/31/c-2010-soup-to-nuts-series.aspx#comments</comments><description>&lt;p&gt;By Emily Gibson&lt;/p&gt;  &lt;p&gt;&lt;b&gt;New! C# 2010 Soup to Nuts Series&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Explore this webcast&lt;b&gt; &lt;/b&gt;series on Visual C# 2010 presented by Developer Evangelist, Bill Steele. Learn about class libraries, operator basics, branching and looping, and more as Bill dives deep into this fantastic language.&lt;/p&gt;    &lt;p&gt;&lt;a href="http://www.microsoft.com/events/series/msdnvs2010.aspx?tab=Webcasts&amp;amp;seriesid=156&amp;amp;webcastid=17803"&gt;Part 1: Introduction&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;script src="http://technet.microsoft.com/en-us/objectforward/default.aspx?type=VideoPlayer&amp;amp;video=mms%3A%2F%2Fwmbmodigital.microsoft.com%2Fa10125%2Fo9%2Fevents%2Fwebcasts%2F1032488222_Str.wmv&amp;amp;thumb=http%3A%2F%2Fwww.microsoft.com%2Fevents%2Fmedia%2fpreviewImage.jpg&amp;amp;title=&amp;amp;width=400&amp;amp;height=400" type="text/javascript"&gt;&lt;/script&gt;&lt;/p&gt;    &lt;p&gt;&lt;a href="http://dlbmodigital.microsoft.com/webcasts/wmv/1032488222_Dnl_L.wmv"&gt;WMV Download&lt;/a&gt; | &lt;a href="http://dlbmodigital.microsoft.com/webcasts/zune/1032488222_Dnl_S.wmv"&gt;Zune&lt;/a&gt; | &lt;a href="http://dlbmodigital.microsoft.com/audio/1032488222.wma"&gt;WMA&lt;/a&gt; | &lt;a href="http://dlbmodigital.microsoft.com/audio/1032488222.mp3"&gt;MP3&lt;/a&gt;&lt;/p&gt;   &lt;p&gt;&lt;b&gt;Watch this Entire Series:&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.microsoft.com/events/series/msdnvs2010.aspx?tab=Webcasts&amp;amp;seriesid=156&amp;amp;webcastid=17803"&gt;Part 1: Introduction&lt;/a&gt;    &lt;br /&gt;&lt;a href="http://www.microsoft.com/events/series/msdnvs2010.aspx?tab=Webcasts&amp;amp;seriesid=156&amp;amp;webcastid=17804"&gt;Part 2: Visual C# Express&lt;/a&gt;    &lt;br /&gt;&lt;a href="http://www.microsoft.com/events/series/msdnvs2010.aspx?tab=Webcasts&amp;amp;seriesid=156&amp;amp;webcastid=17805"&gt;Part 3: Class Libraries&lt;/a&gt;    &lt;br /&gt;&lt;a href="http://www.microsoft.com/events/series/msdnvs2010.aspx?tab=Webcasts&amp;amp;seriesid=156&amp;amp;webcastid=17806"&gt;Part 4: Visual C# Program Structure&lt;/a&gt;    &lt;br /&gt;&lt;a href="http://www.microsoft.com/events/series/msdnvs2010.aspx?tab=Webcasts&amp;amp;seriesid=156&amp;amp;webcastid=17807"&gt;Part 5: Language Fundamentals&lt;/a&gt;    &lt;br /&gt;&lt;a href="http://www.microsoft.com/events/series/msdnvs2010.aspx?tab=Webcasts&amp;amp;seriesid=156&amp;amp;webcastid=17808"&gt;Part 6: Branching and Looping&lt;/a&gt;    &lt;br /&gt;&lt;a href="http://www.microsoft.com/events/series/msdnvs2010.aspx?tab=Webcasts&amp;amp;seriesid=156&amp;amp;webcastid=17809"&gt;Part 7: Classes and Objects&lt;/a&gt;    &lt;br /&gt;&lt;a href="http://www.microsoft.com/events/series/msdnvs2010.aspx?tab=Webcasts&amp;amp;seriesid=156&amp;amp;webcastid=17810"&gt;Part 8: Operator Basics&lt;/a&gt;    &lt;br /&gt;&lt;a href="https://msevents.microsoft.com/CUI/EventDetail.aspx?EventID=1032488542&amp;amp;Culture=en-US"&gt;Part 9: Basic Debugging&lt;/a&gt;    &lt;br /&gt;&lt;a href="https://msevents.microsoft.com/CUI/EventDetail.aspx?EventID=1032488546&amp;amp;Culture=en-US"&gt;Part 10: Arrays and Collections&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=10203983" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/C_2300_/">C#</category></item><item><title>New Async Programming Videos</title><link>http://blogs.msdn.com/b/csharpfaq/archive/2011/08/15/new-async-programming-videos.aspx</link><pubDate>Mon, 15 Aug 2011 18:36:04 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10195781</guid><dc:creator>CSharpFAQ</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/csharpfaq/rsscomments.aspx?WeblogPostID=10195781</wfw:commentRss><comments>http://blogs.msdn.com/b/csharpfaq/archive/2011/08/15/new-async-programming-videos.aspx#comments</comments><description>&lt;p&gt;Some great new video's on MSDN showing how to do async programming using the Async CTP.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://msdn.microsoft.com/en-us/vstudio/hh378091.aspx"&gt;http://msdn.microsoft.com/en-us/vstudio/hh378091.aspx&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;There are different versions of video's for both VB and C#. This is a great opportunity to see the power of the new Async languages featuresand follow through a series of examples showing how easy it is to use the new feature.&lt;/p&gt;
&lt;p&gt;Also, the MSDN forums have one specifically dedicated to the Async CTP which compilers team member are monitoring to help answer any questions.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://social.msdn.microsoft.com/Forums/en-US/async/threads"&gt;http://social.msdn.microsoft.com/Forums/en-US/async/threads&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Spotty&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10195781" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/C_23002F00_VB-NET+Equivalents/">C#/VB.NET Equivalents</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/Visual+Studio+Async+CTP/">Visual Studio Async CTP</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/async/">async</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/await/">await</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/asynchronous+programming/">asynchronous programming</category></item><item><title>Async CTP (SP1 Refresh)</title><link>http://blogs.msdn.com/b/csharpfaq/archive/2011/04/13/async-ctp-sp1-refresh.aspx</link><pubDate>Wed, 13 Apr 2011 16:13:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10153383</guid><dc:creator>CSharpFAQ</dc:creator><slash:comments>15</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/csharpfaq/rsscomments.aspx?WeblogPostID=10153383</wfw:commentRss><comments>http://blogs.msdn.com/b/csharpfaq/archive/2011/04/13/async-ctp-sp1-refresh.aspx#comments</comments><description>&lt;p&gt;By Jeremy Meng&lt;/p&gt;
&lt;p&gt;&lt;a href="http://msdn.microsoft.com/en-US/vstudio/async"&gt;Visual Studio Async CTP (SP1 Refresh)&lt;/a&gt; is available now! You are welcome to &lt;a href="http://msdn.microsoft.com/en-US/vstudio/async"&gt;download&lt;/a&gt; and try it out!&lt;/p&gt;
&lt;p&gt;Thanks to the new Async feature coming in Visual Basic and C# it has never been so easy to write asynchronous code. With the newly introduced &lt;strong&gt;async&lt;/strong&gt; and &lt;strong&gt;await&lt;/strong&gt; keywords you can make asynchronous calls without having to create callback functions, sign them up to some ****Completed events, retrieve results from EventArgs by yourself. Visual Basic and C# compiler will do the plumbing work for you.&lt;/p&gt;
&lt;p&gt;I hope that the following example can help you get basic ideas on how the Async feature makes writing responsive applications easier.&lt;/p&gt;
&lt;p&gt;Here is a simple WPF application to get you started with Async in C#. This WPF app has a label, a button, and a checkbox on its UI. When the button is clicked, the application will perform a query over the network (for example, downloading files from the internet, querying data from a database, etc.) After the query is done, the label will be updated to show the length of the returned string data.&lt;/p&gt;
&lt;p&gt;The event handler for button click:&lt;/p&gt;
&lt;p style="line-height: normal; list-style-type: disc; margin-top: 0in; text-autospace: ; margin-left: 0in; margin-right: 0in; mso-layout-grid-align: none" class="MsoNormal"&gt;&lt;span style="font-family: Consolas;"&gt;&lt;span style="font-family: ; background: none transparent scroll repeat 0% 0%; color: ; mso-highlight: white"&gt;&lt;span style="color: #0000ff;"&gt;&lt;span style="font-size: 9.5pt;"&gt;private&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt;&lt;span style="font-family: ; background: none transparent scroll repeat 0% 0%; color: ; mso-highlight: white"&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: ; background: none transparent scroll repeat 0% 0%; color: ; mso-highlight: white"&gt;&lt;span style="color: #0000ff;"&gt;void&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: ; background: none transparent scroll repeat 0% 0%; color: ; mso-highlight: white"&gt;&lt;span style="color: #000000;"&gt; button1_Click(&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: ; background: none transparent scroll repeat 0% 0%; color: ; mso-highlight: white"&gt;&lt;span style="color: #0000ff;"&gt;object&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: ; background: none transparent scroll repeat 0% 0%; color: ; mso-highlight: white"&gt;&lt;span style="color: #000000;"&gt; sender, &lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: ; background: none transparent scroll repeat 0% 0%; color: ; mso-highlight: white"&gt;&lt;span style="color: #2b91af;"&gt;RoutedEventArgs&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: ; background: none transparent scroll repeat 0% 0%; color: ; mso-highlight: white"&gt;&lt;span style="font-size: 9.5pt; color: #000000;"&gt; e)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="line-height: normal; list-style-type: disc; margin-top: 0in; text-autospace: ; margin-left: 0in; margin-right: 0in; mso-layout-grid-align: none" class="MsoNormal"&gt;&lt;span style="font-family: ; background: none transparent scroll repeat 0% 0%; color: ; mso-highlight: white"&gt;&lt;span style="font-family: Consolas;"&gt;&lt;span style="color: #000000;"&gt;&lt;span style="font-size: 9.5pt;"&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="line-height: normal; list-style-type: disc; margin-top: 0in; text-autospace: ; margin-left: 0in; margin-right: 0in; mso-layout-grid-align: none" class="MsoNormal"&gt;&lt;span style="font-family: ; background: none transparent scroll repeat 0% 0%; color: ; mso-highlight: white"&gt;&lt;span style="font-family: Consolas;"&gt;&lt;span style="color: #000000;"&gt;&lt;span style="font-size: 9.5pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; UpdateLabel();&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="line-height: normal; list-style-type: disc; margin-top: 0in; text-autospace: ; margin-left: 0in; margin-right: 0in; mso-layout-grid-align: none" class="MsoNormal"&gt;&lt;span style="font-family: ; background: none transparent scroll repeat 0% 0%; color: ; mso-highlight: white"&gt;&lt;span style="font-family: Consolas;"&gt;&lt;span style="color: #000000;"&gt;&lt;span style="font-size: 9.5pt;"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="line-height: normal; list-style-type: disc; margin-top: 0in; text-autospace: ; margin-left: 0in; margin-right: 0in; mso-layout-grid-align: none" class="MsoNormal"&gt;&lt;span style="font-family: ; background: none transparent scroll repeat 0% 0%; color: ; mso-highlight: white"&gt;&lt;span style="font-family: Consolas;"&gt;&lt;span style="color: #000000;"&gt;&lt;span style="font-size: 9.5pt;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="line-height: normal; list-style-type: disc; margin-top: 0in; text-autospace: ; margin-left: 0in; margin-right: 0in; mso-layout-grid-align: none" class="MsoNormal"&gt;&lt;span style="font-family: Consolas;"&gt;&lt;span style="font-size: 9.5pt;"&gt;&lt;span style="font-family: ; background: none transparent scroll repeat 0% 0%; color: ; mso-highlight: white"&gt;&lt;span style="color: #0000ff;"&gt;async&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: ; background: none transparent scroll repeat 0% 0%; color: ; mso-highlight: white"&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: ; background: none transparent scroll repeat 0% 0%; color: ; mso-highlight: white"&gt;&lt;span style="color: #0000ff;"&gt;void&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: ; background: none transparent scroll repeat 0% 0%; color: ; mso-highlight: white"&gt;&lt;span style="font-size: 9.5pt; color: #000000;"&gt; UpdateLabel()&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="line-height: normal; list-style-type: disc; margin-top: 0in; text-autospace: ; margin-left: 0in; margin-right: 0in; mso-layout-grid-align: none" class="MsoNormal"&gt;&lt;span style="font-family: ; background: none transparent scroll repeat 0% 0%; color: ; mso-highlight: white"&gt;&lt;span style="font-family: Consolas;"&gt;&lt;span style="color: #000000;"&gt;&lt;span style="font-size: 9.5pt;"&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="line-height: normal; list-style-type: disc; margin-top: 0in; text-autospace: ; margin-left: 0in; margin-right: 0in; mso-layout-grid-align: none" class="MsoNormal"&gt;&lt;span style="font-family: Consolas;"&gt;&lt;span style="font-size: 9.5pt;"&gt;&lt;span style="font-family: ; background: none transparent scroll repeat 0% 0%; color: ; mso-highlight: white"&gt;&lt;span style="color: #2b91af;"&gt;&lt;span style="color: #000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;NetworkClass&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: ; background: none transparent scroll repeat 0% 0%; color: ; mso-highlight: white"&gt;&lt;span style="color: #000000;"&gt; nc = &lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: ; background: none transparent scroll repeat 0% 0%; color: ; mso-highlight: white"&gt;&lt;span style="color: #0000ff;"&gt;new&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: ; background: none transparent scroll repeat 0% 0%; color: ; mso-highlight: white"&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: ; background: none transparent scroll repeat 0% 0%; color: ; mso-highlight: white"&gt;&lt;span style="color: #2b91af;"&gt;NetworkClass&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: ; background: none transparent scroll repeat 0% 0%; color: ; mso-highlight: white"&gt;&lt;span style="font-size: 9.5pt; color: #000000;"&gt;();&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="line-height: normal; list-style-type: disc; margin-top: 0in; text-autospace: ; margin-left: 0in; margin-right: 0in; mso-layout-grid-align: none" class="MsoNormal"&gt;&lt;span style="font-family: Consolas;"&gt;&lt;span style="font-size: 9.5pt;"&gt;&lt;span style="font-family: ; background: none transparent scroll repeat 0% 0%; color: ; mso-highlight: white"&gt;&lt;span style="color: #0000ff;"&gt;&lt;span style="color: #000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;int&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: ; background: none transparent scroll repeat 0% 0%; color: ; mso-highlight: white"&gt;&lt;span style="font-size: 9.5pt; color: #000000;"&gt; i = 0;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="line-height: normal; list-style-type: disc; margin-top: 0in; text-autospace: ; margin-left: 0in; margin-right: 0in; mso-layout-grid-align: none" class="MsoNormal"&gt;&lt;span style="font-family: Consolas;"&gt;&lt;span style="font-family: ; background: none transparent scroll repeat 0% 0%; color: ; mso-highlight: white"&gt;&lt;span style="color: #000000;"&gt;&lt;span style="font-size: 9.5pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; i = &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt;&lt;span style="font-family: ; background: none transparent scroll repeat 0% 0%; color: ; mso-highlight: white"&gt;&lt;span style="color: #0000ff;"&gt;await&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: ; background: none transparent scroll repeat 0% 0%; color: ; mso-highlight: white"&gt;&lt;span style="font-size: 9.5pt; color: #000000;"&gt; nc.MethodQueryingNetworkAsync();&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="line-height: normal; list-style-type: disc; margin-top: 0in; text-autospace: ; margin-left: 0in; margin-right: 0in; mso-layout-grid-align: none" class="MsoNormal"&gt;&lt;span style="font-family: Consolas;"&gt;&lt;span style="font-family: ; background: none transparent scroll repeat 0% 0%; color: ; mso-highlight: white"&gt;&lt;span style="color: #000000;"&gt;&lt;span style="font-size: 9.5pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; label1.Content = &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt;&lt;span style="font-family: ; background: none transparent scroll repeat 0% 0%; color: ; mso-highlight: white"&gt;&lt;span style="color: #a31515;"&gt;"Length: "&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: ; background: none transparent scroll repeat 0% 0%; color: ; mso-highlight: white"&gt;&lt;span style="font-size: 9.5pt; color: #000000;"&gt; + i;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="line-height: normal; list-style-type: disc; margin-top: 0in; text-autospace: ; margin-left: 0in; margin-right: 0in; mso-layout-grid-align: none" class="MsoNormal"&gt;&lt;span style="font-family: ; background: none transparent scroll repeat 0% 0%; color: ; mso-highlight: white"&gt;&lt;span style="font-family: Consolas;"&gt;&lt;span style="color: #000000;"&gt;&lt;span style="font-size: 9.5pt;"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The code for MethodQueryingNetworkAsync() is like below:&lt;/p&gt;
&lt;p style="line-height: normal; list-style-type: disc; margin-top: 0in; text-autospace: ; margin-left: 0in; margin-right: 0in; mso-layout-grid-align: none" class="MsoNormal"&gt;&lt;span style="font-family: Consolas;"&gt;&lt;span style="font-family: ; background: none transparent scroll repeat 0% 0%; color: ; mso-highlight: white"&gt;&lt;span style="color: #0000ff;"&gt;&lt;span style="font-size: 9.5pt;"&gt;async&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt;&lt;span style="font-family: ; background: none transparent scroll repeat 0% 0%; color: ; mso-highlight: white"&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: ; background: none transparent scroll repeat 0% 0%; color: ; mso-highlight: white"&gt;&lt;span style="color: #0000ff;"&gt;public&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: ; background: none transparent scroll repeat 0% 0%; color: ; mso-highlight: white"&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: ; background: none transparent scroll repeat 0% 0%; color: ; mso-highlight: white"&gt;&lt;span style="color: #2b91af;"&gt;Task&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: ; background: none transparent scroll repeat 0% 0%; color: ; mso-highlight: white"&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: ; background: none transparent scroll repeat 0% 0%; color: ; mso-highlight: white"&gt;&lt;span style="color: #0000ff;"&gt;int&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: ; background: none transparent scroll repeat 0% 0%; color: ; mso-highlight: white"&gt;&lt;span style="font-size: 9.5pt; color: #000000;"&gt;&amp;gt; MethodQueryingNetworkAsync()&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="line-height: normal; list-style-type: disc; margin-top: 0in; text-autospace: ; margin-left: 0in; margin-right: 0in; mso-layout-grid-align: none" class="MsoNormal"&gt;&lt;span style="font-family: ; background: none transparent scroll repeat 0% 0%; color: ; mso-highlight: white"&gt;&lt;span style="font-family: Consolas;"&gt;&lt;span style="color: #000000;"&gt;&lt;span style="font-size: 9.5pt;"&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="line-height: normal; list-style-type: disc; margin-top: 0in; text-autospace: ; margin-left: 0in; margin-right: 0in; mso-layout-grid-align: none" class="MsoNormal"&gt;&lt;span style="font-family: Consolas;"&gt;&lt;span style="font-family: ; background: none transparent scroll repeat 0% 0%; color: ; mso-highlight: white"&gt;&lt;span style="font-size: 9.5pt; color: #008000;"&gt;&lt;span style="color: #000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;// Querying a slow website with network latency so that&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: ; background: none transparent scroll repeat 0% 0%; color: ; mso-highlight: white"&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="line-height: normal; list-style-type: disc; margin-top: 0in; text-autospace: ; margin-left: 0in; margin-right: 0in; mso-layout-grid-align: none" class="MsoNormal"&gt;&lt;span style="font-family: Consolas;"&gt;&lt;span style="font-family: ; background: none transparent scroll repeat 0% 0%; color: ; mso-highlight: white"&gt;&lt;span style="font-size: 9.5pt; color: #008000;"&gt;&lt;span style="color: #000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;// the result comes back after a certain amount of time&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: ; background: none transparent scroll repeat 0% 0%; color: ; mso-highlight: white"&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="line-height: normal; list-style-type: disc; margin-top: 0in; text-autospace: ; margin-left: 0in; margin-right: 0in; mso-layout-grid-align: none" class="MsoNormal"&gt;&lt;span style="font-family: Consolas;"&gt;&lt;span style="font-size: 9.5pt;"&gt;&lt;span style="font-family: ; background: none transparent scroll repeat 0% 0%; color: ; mso-highlight: white"&gt;&lt;span style="color: #2b91af;"&gt;&lt;span style="color: #000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;WebClient&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: ; background: none transparent scroll repeat 0% 0%; color: ; mso-highlight: white"&gt;&lt;span style="color: #000000;"&gt; wc = &lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: ; background: none transparent scroll repeat 0% 0%; color: ; mso-highlight: white"&gt;&lt;span style="color: #0000ff;"&gt;new&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: ; background: none transparent scroll repeat 0% 0%; color: ; mso-highlight: white"&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: ; background: none transparent scroll repeat 0% 0%; color: ; mso-highlight: white"&gt;&lt;span style="color: #2b91af;"&gt;WebClient&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: ; background: none transparent scroll repeat 0% 0%; color: ; mso-highlight: white"&gt;&lt;span style="font-size: 9.5pt; color: #000000;"&gt;();&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="line-height: normal; list-style-type: disc; margin-top: 0in; text-autospace: ; margin-left: 0in; margin-right: 0in; mso-layout-grid-align: none" class="MsoNormal"&gt;&lt;span style="font-family: Consolas;"&gt;&lt;span style="font-size: 9.5pt;"&gt;&lt;span style="font-family: ; background: none transparent scroll repeat 0% 0%; color: ; mso-highlight: white"&gt;&lt;span style="color: #0000ff;"&gt;&lt;span style="color: #000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;string&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: ; background: none transparent scroll repeat 0% 0%; color: ; mso-highlight: white"&gt;&lt;span style="color: #000000;"&gt; content = &lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: ; background: none transparent scroll repeat 0% 0%; color: ; mso-highlight: white"&gt;&lt;span style="color: #0000ff;"&gt;await&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: ; background: none transparent scroll repeat 0% 0%; color: ; mso-highlight: white"&gt;&lt;span style="font-size: 9.5pt; color: #000000;"&gt; wc.DownloadStringTaskAsync(
        &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: Consolas;"&gt;&lt;span style="font-size: 9.5pt;"&gt;&lt;span style="font-family: ; background: none transparent scroll repeat 0% 0%; color: ; mso-highlight: white"&gt;&lt;span style="color: #0000ff;"&gt;new&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: ; background: none transparent scroll repeat 0% 0%; color: ; mso-highlight: white"&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: ; background: none transparent scroll repeat 0% 0%; color: ; mso-highlight: white"&gt;&lt;span style="color: #2b91af;"&gt;Uri&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: ; background: none transparent scroll repeat 0% 0%; color: ; mso-highlight: white"&gt;&lt;span style="color: #000000;"&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: ; background: none transparent scroll repeat 0% 0%; color: ; mso-highlight: white"&gt;&lt;span style="color: #a31515;"&gt;"http://some.slow.website/"&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: ; background: none transparent scroll repeat 0% 0%; color: ; mso-highlight: white"&gt;&lt;span style="font-size: 9.5pt; color: #000000;"&gt;));&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="line-height: normal; list-style-type: disc; margin-top: 0in; text-autospace: ; margin-left: 0in; margin-right: 0in; mso-layout-grid-align: none" class="MsoNormal"&gt;&lt;span style="font-family: Consolas;"&gt;&lt;span style="font-size: 9.5pt;"&gt;&lt;span style="font-family: ; background: none transparent scroll repeat 0% 0%; color: ; mso-highlight: white"&gt;&lt;span style="color: #0000ff;"&gt;&lt;span style="color: #000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;return&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: ; background: none transparent scroll repeat 0% 0%; color: ; mso-highlight: white"&gt;&lt;span style="font-size: 9.5pt; color: #000000;"&gt; content.Length;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="line-height: normal; list-style-type: disc; margin-top: 0in; text-autospace: ; margin-left: 0in; margin-right: 0in; mso-layout-grid-align: none" class="MsoNormal"&gt;&lt;span style="font-family: ; background: none transparent scroll repeat 0% 0%; color: ; mso-highlight: white"&gt;&lt;span style="font-family: Consolas;"&gt;&lt;span style="color: #000000;"&gt;&lt;span style="font-size: 9.5pt;"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Run the application and click on the button, you will still be able to toggle the checkbox on UI while the network query is executing. When the downloading is finished, the label will be updated to show the length of the query result. During the query execution, the UI is completely responsive.&lt;/p&gt;
&lt;h3&gt;What&amp;rsquo;s new in Visual Studio Async CTP SP1 Refresh?&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;It is compatible with Visual Studio 2010 SP1 and Windows Phone 7. The previous Async CTP only works for Visual Studio 2010 RTM.&lt;/li&gt;
&lt;li&gt;More efficient Async methods!&lt;/li&gt;
&lt;/ul&gt;
&lt;blockquote&gt;
&lt;p&gt;We changed the old pattern of GetAwaiter/BeginAwait/EndAwait with the new pattern of GetAwaiter/IsCompleted/OnCompleted/GetResult. This provides better performance especially if the task we are awaiting had already completed. We also make exception behaviors uniform in void-returning async methods (Async Subs in Visual Basic).&lt;/p&gt;
&lt;/blockquote&gt;
&lt;ul&gt;
&lt;li&gt;Many bug fixes since the release of the previous Async CTP. Thanks for the feedbacks from the community! Please &lt;a href="http://social.msdn.microsoft.com/Forums/en-US/async/threads"&gt;let us know what you think&lt;/a&gt;!&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For more details, read on &lt;a title="http://blogs.msdn.com/b/lucian/" href="http://blogs.msdn.com/b/lucian/"&gt;Lucian&amp;rsquo;s blog&lt;/a&gt;. Welcome to Async and happy asynchronous coding!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10153383" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/C_2300_/">C#</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/Visual+Studio+Async+CTP/">Visual Studio Async CTP</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/async/">async</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/await/">await</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/asynchronous+programming/">asynchronous programming</category></item><item><title>How to programmatically launch Debugger in a remote machine</title><link>http://blogs.msdn.com/b/csharpfaq/archive/2011/03/28/how-to-programmatically-launch-debugger-in-a-remote-machine.aspx</link><pubDate>Mon, 28 Mar 2011 18:37:39 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10146795</guid><dc:creator>CSharpFAQ</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/csharpfaq/rsscomments.aspx?WeblogPostID=10146795</wfw:commentRss><comments>http://blogs.msdn.com/b/csharpfaq/archive/2011/03/28/how-to-programmatically-launch-debugger-in-a-remote-machine.aspx#comments</comments><description>&lt;p class="MsoNormal" style="margin: 0in 0in 10pt;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;This blog post explains how to kick off a debugger in a remote machine, programmatically. We are going to use WMI interfaces to achieve this. Use WMI, to start the debuggee process and attach a registered debugger to it using WMI. I have given a sample code below.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="text-indent: 0.5in; margin: 0in 0in 10pt;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Calibri;"&gt;This Code assumes that we have the ConsoleApplication1 process running in the Machine1(debuggee Machine). On executing this code , the registered Debugger instance will be attached to &amp;ldquo;ConsoleApplication1&amp;rdquo; process. If you have multiple debuggers registered in the debuggee machine, then &amp;ldquo;Select Debugger&amp;rdquo; dialog would come up. We can use this method to attach Debugger to any kind of processes/services.&lt;/span&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; background: white; color: #2b91af; font-size: 9.5pt; mso-highlight: white;"&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; background: white; color: #2b91af; font-size: 9.5pt; mso-highlight: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ConnectionOptions&lt;/span&gt;&lt;span style="font-family: Consolas; background: white; color: black; font-size: 9.5pt; mso-highlight: white;"&gt; options = &lt;/span&gt;&lt;span style="font-family: Consolas; background: white; color: blue; font-size: 9.5pt; mso-highlight: white;"&gt;new&lt;/span&gt;&lt;span style="font-family: Consolas; background: white; color: black; font-size: 9.5pt; mso-highlight: white;"&gt; &lt;/span&gt;&lt;span style="font-family: Consolas; background: white; color: #2b91af; font-size: 9.5pt; mso-highlight: white;"&gt;ConnectionOptions&lt;/span&gt;&lt;span style="font-family: Consolas; background: white; color: black; font-size: 9.5pt; mso-highlight: white;"&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; background: white; color: black; font-size: 9.5pt; mso-highlight: white;"&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&gt;&lt;span style="font-family: Consolas; background: white; color: #2b91af; font-size: 9.5pt; mso-highlight: white;"&gt;ManagementScope&lt;/span&gt;&lt;span style="font-family: Consolas; background: white; color: black; font-size: 9.5pt; mso-highlight: white;"&gt; scope = &lt;/span&gt;&lt;span style="font-family: Consolas; background: white; color: blue; font-size: 9.5pt; mso-highlight: white;"&gt;new&lt;/span&gt;&lt;span style="font-family: Consolas; background: white; color: black; font-size: 9.5pt; mso-highlight: white;"&gt; &lt;/span&gt;&lt;span style="font-family: Consolas; background: white; color: #2b91af; font-size: 9.5pt; mso-highlight: white;"&gt;ManagementScope&lt;/span&gt;&lt;span style="font-family: Consolas; background: white; color: black; font-size: 9.5pt; mso-highlight: white;"&gt;(&lt;/span&gt;&lt;span style="font-family: Consolas; background: white; color: #a31515; font-size: 9.5pt; mso-highlight: white;"&gt;"\\\\machine1\\root\\cimv2"&lt;/span&gt;&lt;span style="font-family: Consolas; background: white; color: black; font-size: 9.5pt; mso-highlight: white;"&gt;, options);&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; background: white; color: black; font-size: 9.5pt; mso-highlight: white;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;scope.Connect();&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; background: white; color: black; font-size: 9.5pt; mso-highlight: white;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&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; background: white; color: black; font-size: 9.5pt; mso-highlight: white;"&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&gt;&lt;span style="font-family: Consolas; background: white; color: #2b91af; font-size: 9.5pt; mso-highlight: white;"&gt;ObjectQuery&lt;/span&gt;&lt;span style="font-family: Consolas; background: white; color: black; font-size: 9.5pt; mso-highlight: white;"&gt; oQuery = &lt;/span&gt;&lt;span style="font-family: Consolas; background: white; color: blue; font-size: 9.5pt; mso-highlight: white;"&gt;new&lt;/span&gt;&lt;span style="font-family: Consolas; background: white; color: black; font-size: 9.5pt; mso-highlight: white;"&gt; &lt;/span&gt;&lt;span style="font-family: Consolas; background: white; color: #2b91af; font-size: 9.5pt; mso-highlight: white;"&gt;ObjectQuery&lt;/span&gt;&lt;span style="font-family: Consolas; background: white; color: black; font-size: 9.5pt; mso-highlight: white;"&gt;(&lt;/span&gt;&lt;span style="font-family: Consolas; background: white; color: #a31515; font-size: 9.5pt; mso-highlight: white;"&gt;"Select * from Win32_Process where Name Like 'ConsoleApp%'"&lt;/span&gt;&lt;span style="font-family: Consolas; background: white; color: black; font-size: 9.5pt; mso-highlight: white;"&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; background: white; color: black; font-size: 9.5pt; mso-highlight: white;"&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&gt;&lt;span style="font-family: Consolas; background: white; color: green; font-size: 9.5pt; mso-highlight: white;"&gt;//Execute the query &lt;/span&gt;&lt;span style="font-family: Consolas; background: white; color: black; font-size: 9.5pt; mso-highlight: white;"&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; background: white; color: black; font-size: 9.5pt; mso-highlight: white;"&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&gt;&lt;span style="font-family: Consolas; background: white; color: #2b91af; font-size: 9.5pt; mso-highlight: white;"&gt;ManagementObjectSearcher&lt;/span&gt;&lt;span style="font-family: Consolas; background: white; color: black; font-size: 9.5pt; mso-highlight: white;"&gt; objSearcher = &lt;/span&gt;&lt;span style="font-family: Consolas; background: white; color: blue; font-size: 9.5pt; mso-highlight: white;"&gt;new&lt;/span&gt;&lt;span style="font-family: Consolas; background: white; color: black; font-size: 9.5pt; mso-highlight: white;"&gt; &lt;/span&gt;&lt;span style="font-family: Consolas; background: white; color: #2b91af; font-size: 9.5pt; mso-highlight: white;"&gt;ManagementObjectSearcher&lt;/span&gt;&lt;span style="font-family: Consolas; background: white; color: black; font-size: 9.5pt; mso-highlight: white;"&gt;(scope,oQuery);&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; background: white; color: black; font-size: 9.5pt; mso-highlight: white;"&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; background: white; color: black; font-size: 9.5pt; mso-highlight: white;"&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&gt;&lt;span style="font-family: Consolas; background: white; color: green; font-size: 9.5pt; mso-highlight: white;"&gt;//Get the results&lt;/span&gt;&lt;span style="font-family: Consolas; background: white; color: black; font-size: 9.5pt; mso-highlight: white;"&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; background: white; color: black; font-size: 9.5pt; mso-highlight: white;"&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&gt;&lt;span style="font-family: Consolas; background: white; color: #2b91af; font-size: 9.5pt; mso-highlight: white;"&gt;ManagementObjectCollection&lt;/span&gt;&lt;span style="font-family: Consolas; background: white; color: black; font-size: 9.5pt; mso-highlight: white;"&gt; objReturnCollection = objSearcher.Get();&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; background: white; color: black; font-size: 9.5pt; mso-highlight: white;"&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; background: white; color: black; font-size: 9.5pt; mso-highlight: white;"&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&gt;&lt;span style="font-family: Consolas; background: white; color: blue; font-size: 9.5pt; mso-highlight: white;"&gt;foreach&lt;/span&gt;&lt;span style="font-family: Consolas; background: white; color: black; font-size: 9.5pt; mso-highlight: white;"&gt; (&lt;/span&gt;&lt;span style="font-family: Consolas; background: white; color: #2b91af; font-size: 9.5pt; mso-highlight: white;"&gt;ManagementObject&lt;/span&gt;&lt;span style="font-family: Consolas; background: white; color: black; font-size: 9.5pt; mso-highlight: white;"&gt; oReturn &lt;/span&gt;&lt;span style="font-family: Consolas; background: white; color: blue; font-size: 9.5pt; mso-highlight: white;"&gt;in&lt;/span&gt;&lt;span style="font-family: Consolas; background: white; color: black; font-size: 9.5pt; mso-highlight: white;"&gt; objReturnCollection)&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; background: white; color: black; font-size: 9.5pt; mso-highlight: white;"&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; background: white; color: black; font-size: 9.5pt; mso-highlight: white;"&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&gt;&lt;span style="font-family: Consolas; background: white; color: blue; font-size: 9.5pt; mso-highlight: white;"&gt;if&lt;/span&gt;&lt;span style="font-family: Consolas; background: white; color: black; font-size: 9.5pt; mso-highlight: white;"&gt; (oReturn[&lt;/span&gt;&lt;span style="font-family: Consolas; background: white; color: #a31515; font-size: 9.5pt; mso-highlight: white;"&gt;"Name"&lt;/span&gt;&lt;span style="font-family: Consolas; background: white; color: black; font-size: 9.5pt; mso-highlight: white;"&gt;].ToString().Equals(&lt;/span&gt;&lt;span style="font-family: Consolas; background: white; color: #a31515; font-size: 9.5pt; mso-highlight: white;"&gt;"ConsoleApplication1.exe"&lt;/span&gt;&lt;span style="font-family: Consolas; background: white; color: black; font-size: 9.5pt; mso-highlight: white;"&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; background: white; color: black; font-size: 9.5pt; mso-highlight: white;"&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;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; background: white; color: black; font-size: 9.5pt; mso-highlight: white;"&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&gt;&lt;span style="font-family: Consolas; background: white; color: blue; font-size: 9.5pt; mso-highlight: white;"&gt;object&lt;/span&gt;&lt;span style="font-family: Consolas; background: white; color: black; font-size: 9.5pt; mso-highlight: white;"&gt; outparams = oReturn.InvokeMethod(&lt;/span&gt;&lt;span style="font-family: Consolas; background: white; color: #a31515; font-size: 9.5pt; mso-highlight: white;"&gt;"AttachDebugger"&lt;/span&gt;&lt;span style="font-family: Consolas; background: white; color: black; font-size: 9.5pt; mso-highlight: white;"&gt;, &lt;/span&gt;&lt;span style="font-family: Consolas; background: white; color: blue; font-size: 9.5pt; mso-highlight: white;"&gt;null&lt;/span&gt;&lt;span style="font-family: Consolas; background: white; color: black; font-size: 9.5pt; mso-highlight: white;"&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; background: white; color: black; font-size: 9.5pt; mso-highlight: white;"&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;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; background: white; color: black; font-size: 9.5pt; mso-highlight: white;"&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&gt;&amp;nbsp;This code reads through the active processes in the remote machine using WMI, and there is a build method of invoking the process while attaching a debugger to it. To learn more about using WMI, &lt;a href="http://msdn.microsoft.com/en-us/library/ms257364(VS.80).aspx"&gt;&lt;span style="font-family: Calibri; font-size: small;"&gt;here&lt;/span&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=10146795" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/debug/">debug</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/WMI/">WMI</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/start+debugger+remotely/">start debugger remotely</category></item><item><title>Visual Studio is Hiring</title><link>http://blogs.msdn.com/b/csharpfaq/archive/2011/02/08/visual-studio-is-hiring.aspx</link><pubDate>Wed, 09 Feb 2011 00:55:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10126522</guid><dc:creator>CSharpFAQ</dc:creator><slash:comments>5</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/csharpfaq/rsscomments.aspx?WeblogPostID=10126522</wfw:commentRss><comments>http://blogs.msdn.com/b/csharpfaq/archive/2011/02/08/visual-studio-is-hiring.aspx#comments</comments><description>&lt;p&gt;Do you want to work on a product used by millions of developers around the world?&amp;nbsp; I do!&amp;nbsp; Come join me to deliver Visual Studio, the set of developer tools used across Microsoft and around the world.&amp;nbsp; We have open positions available across Test, Dev and PM at varying levels on many projects across Visual Studio Professional.&amp;nbsp; We&amp;rsquo;re looking for the most talented folks around to help us&amp;nbsp; deliver the core parts of Visual Studio, from the Shell &amp;amp; IDE, languages (VB/C#/F#), packaging &amp;amp; setup, to our future investments in C# &amp;amp; VB.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;Here are a list of our open positions.&amp;nbsp; We love referrals, let your friends know they can work on Visual Studio too!&amp;nbsp; To get more information, please submit your resume.&lt;/p&gt;
&lt;h3&gt;Visual Studio IDE/Platform Team&lt;/h3&gt;
&lt;table cellpadding="0" cellspacing="0" border="0"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Team&lt;/strong&gt; &lt;/td&gt;
&lt;td style="padding-left:20px;"&gt;&lt;strong&gt;Position &lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;IDE/Platform &lt;/td&gt;
&lt;td style="padding-left:20px;"&gt;&lt;a href="http://bit.ly/eBxRsZ"&gt;Software Development Engineer in Test 2&lt;/a&gt; &lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;IDE/Platform &lt;/td&gt;
&lt;td style="padding-left:20px;"&gt;&lt;a href="http://bit.ly/eFklng"&gt;Software Development Engineer in Test 2&lt;/a&gt; &lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;IDE/Platform &lt;/td&gt;
&lt;td style="padding-left:20px;"&gt;&lt;a href="http://bit.ly/hfxSJg"&gt;Software Development Engineer 2&lt;/a&gt; &lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;IDE/Platform &lt;/td&gt;
&lt;td style="padding-left:20px;"&gt;&lt;a href="http://bit.ly/gYjkEm"&gt;Program Manager 2&lt;/a&gt; &lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h3&gt;Visual Studio Pro Experience&lt;/h3&gt;
&lt;table cellpadding="0" cellspacing="0" border="0"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Team&lt;/strong&gt; &lt;/td&gt;
&lt;td style="padding-left:20px;"&gt;&lt;strong&gt;Position &lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Pro Experience &lt;/td&gt;
&lt;td style="padding-left:20px;"&gt;&lt;a href="http://bit.ly/gsYxsP"&gt;Software Development Engineer in Test 2&lt;/a&gt; &lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Pro Experience &lt;/td&gt;
&lt;td style="padding-left:20px;"&gt;&lt;a href="http://bit.ly/dIjFNA"&gt;Software Development Engineer 2&lt;/a&gt; &lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Pro Experience &lt;/td&gt;
&lt;td style="padding-left:20px;"&gt;&lt;a href="http://bit.ly/evidkQ"&gt;Program Manager 2&lt;/a&gt; &lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Pro Experience &lt;/td&gt;
&lt;td style="padding-left:20px;"&gt;&lt;a href="http://bit.ly/fzLPLD"&gt;​Senior Program Manager&lt;/a&gt; &lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Pro Experience &lt;/td&gt;
&lt;td style="padding-left:20px;"&gt;&lt;a href="http://bit.ly/homE6G"&gt;Senior Software Development Engineer&lt;/a&gt; &lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;h3&gt;&amp;nbsp;&lt;/h3&gt;
&lt;h3&gt;Codename Roslyn (compiler as a service)&lt;/h3&gt;
&lt;table cellpadding="0" cellspacing="0" border="0"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Team&lt;/strong&gt; &lt;/td&gt;
&lt;td style="padding-left:20px;"&gt;&lt;strong&gt;Position &lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Roslyn &lt;/td&gt;
&lt;td style="padding-left:20px;"&gt;&lt;a href="http://bit.ly/edjnOm"&gt;Software Development Engineer in Test Lead&lt;/a&gt; &lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Roslyn &lt;/td&gt;
&lt;td style="padding-left:20px;"&gt;&lt;a href="http://bit.ly/eu8jp4"&gt;Software Development Engineer 2&lt;/a&gt; &lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;h3&gt;&amp;nbsp;&lt;/h3&gt;
&lt;h3&gt;Visual Studio Project, Build &amp;amp; Windows focus&lt;/h3&gt;
&lt;table cellpadding="0" cellspacing="0" border="0"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Team&lt;/strong&gt; &lt;/td&gt;
&lt;td style="padding-left:20px;"&gt;&lt;strong&gt;Position&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Visual Studio Tools &lt;/td&gt;
&lt;td style="padding-left:20px;"&gt;&lt;a href="http://bit.ly/g8LaQ1"&gt;Software Development Engineer in Test 2&lt;/a&gt; &lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;In case you&amp;rsquo;re not familiar with these core engineering roles at Microsoft, you can find more information out on the &lt;a href="http://careers.microsoft.com/careers/en/us/professions.aspx"&gt;Microsoft Career Site&lt;/a&gt;.&amp;nbsp; Here are short descriptions of the roles:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Program Managers (PMs)&lt;/strong&gt; transform visions into elegant designs that ultimately turn into products and solutions. Or, put more simply, we anticipate what customers want and translate that into what they get. Working closely with development, test, user experience, and marketing professionals, we identify requirements, set priorities, manage feature sets across product lifecycles, and author technical specifications and customer scenarios. &lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Testers (Software Development Engineers in Test &amp;ndash;SDETs)&lt;/strong&gt; &amp;ldquo;build it to break it.&amp;rdquo; We push products to their limit, making sure they perform to customer expectations and conform to the highest quality standards. In partnership with developers and program managers, and using C, C++, C#, and VB.NET, we write test cases and plan, design, develop, and maintain automation tools that isolate and debug problems. &lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Developers (Software Development Engineers &amp;ndash; SDEs)&lt;/strong&gt; write the code&amp;mdash;C, C++, C#, and other programming languages&amp;mdash;that turns concepts into new technologies and services. We are experts in feature design and feasibility, and we collaborate with program managers and test engineers to define features and ensure quality. &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I hope to hear from you soon!&lt;/p&gt;
&lt;p&gt;Peter Hauge [MSFT]&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10126522" width="1" height="1"&gt;</description></item><item><title>Microsoft Help Viewer 1.1 - Updates Planned for Visual Studio 2010 SP1</title><link>http://blogs.msdn.com/b/csharpfaq/archive/2010/11/08/microsoft-help-viewer-1-1-updates-planned-for-visual-studio-2010-sp1.aspx</link><pubDate>Mon, 08 Nov 2010 18:16:54 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10087729</guid><dc:creator>Kathleen McGrath - MSFT</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/csharpfaq/rsscomments.aspx?WeblogPostID=10087729</wfw:commentRss><comments>http://blogs.msdn.com/b/csharpfaq/archive/2010/11/08/microsoft-help-viewer-1-1-updates-planned-for-visual-studio-2010-sp1.aspx#comments</comments><description>&lt;p&gt;Learn about the upcoming changes to the Help Viewer planned for Visual Studio 2010 SP1.&amp;#160; Paul O’Rear, a Program Manager on the Library Experience team, describes the changes planned and demonstrates the new functionality of the viewer in an early build of Help Viewer 1.1. &lt;/p&gt;  &lt;p&gt;See &lt;a href="http://channel9.msdn.com/Blogs/kmcgrath/Microsoft-Help-Viewer-Updates-Planned-for-Visual-Studio-SP1" target="_blank"&gt;Microsoft Help Viewer – Updates Planned for Visual Studio 2010 SP1&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://channel9.msdn.com/Blogs/kmcgrath/Microsoft-Help-Viewer-Updates-Planned-for-Visual-Studio-SP1" target="_blank"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/CommunityServer-Blogs-Components-WeblogFiles/00-00-00-33-48-metablogapi/7041.image_5F00_357FCE28.png" width="557" height="315" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;You can learn more in &lt;a href="http://thirdblogfromthesun.com/2010/10/the-story-of-help-in-visual-studio-2010/"&gt;The Story of Help in Visual Studio 2010&lt;/a&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;--Kathleen&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10087729" width="1" height="1"&gt;</description></item><item><title>What’s Next in C#? Get Ready for Async!</title><link>http://blogs.msdn.com/b/csharpfaq/archive/2010/10/28/async.aspx</link><pubDate>Thu, 28 Oct 2010 17:19:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10082237</guid><dc:creator>Alexandra Rusina</dc:creator><slash:comments>16</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/csharpfaq/rsscomments.aspx?WeblogPostID=10082237</wfw:commentRss><comments>http://blogs.msdn.com/b/csharpfaq/archive/2010/10/28/async.aspx#comments</comments><description>&lt;p&gt;Today we announced the &lt;a href="http://msdn.microsoft.com/en-us/vstudio/async.aspx"&gt;Visual Studio Async CTP&lt;/a&gt;, which shows one of the major features we plan to include in a future release of C# and Visual Basic. This feature makes development of asynchronous applications--which include everything from desktop applications with responsive UI to sophisticated web applications--much easier.&lt;/p&gt;  &lt;p&gt;The future release will introduce two new keywords to the C# language: &lt;b&gt;await&lt;/b&gt; and &lt;b&gt;async&lt;/b&gt;. The &lt;b&gt;await&lt;/b&gt; keyword is used to mark asynchronous calls, so that you don’t need to create callback functions anymore and can write code in the same way as if it were synchronous. The compiler will do all of the heavy lifting for you. The &lt;b&gt;async&lt;/b&gt; keyword must be presented in the signature of the method that makes asynchronous calls. Briefly, you can use the &lt;b&gt;await&lt;/b&gt; keyword only if the &lt;b&gt;async&lt;/b&gt; keyword is in the method signature. The same is true for lambda expressions.&lt;/p&gt;  &lt;p&gt;This post presents just a short description of the feature. I strongly recommend that you visit the &lt;a href="http://msdn.microsoft.com/en-us/vstudio/async.aspx"&gt;Visual Studio Async CTP&lt;/a&gt; page to learn more of the details and get tons of useful resources, including the language spec and cool samples.&lt;/p&gt;  &lt;p&gt;Although we have plenty of examples and resources available, it’s never enough. So I’m giving you one more demonstration of how much easier it might be to create asynchronous programs with a future version of the C# and Visual Basic languages.&lt;/p&gt;  &lt;p&gt;First, some preparatory steps to make the example work:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Download the &lt;a href="http://msdn.microsoft.com/en-us/vstudio/async.aspx"&gt;Visual Studio Async CTP&lt;/a&gt; and install it. &lt;/li&gt;    &lt;li&gt;Open &lt;a href="http://www.microsoft.com/visualstudio/en-us/download"&gt;Visual Studio 2010&lt;/a&gt; and create a new &lt;a href="http://www.silverlight.net/getstarted/"&gt;Silverlight 4&lt;/a&gt; project (To make things easier later, name it &lt;b&gt;FacebookAsync&lt;/b&gt;.) &lt;/li&gt;    &lt;li&gt;Add a reference to &lt;b&gt;AsyncCtpLibrary_Silverlight.dll&lt;/b&gt;. It should be in &lt;b&gt;My Documents/Microsoft Visual Studio Async CTP/Samples&lt;/b&gt;. &lt;/li&gt;    &lt;li&gt;Add a reference to the &lt;b&gt;System.Json&lt;/b&gt; library, because it’s used in the example. This is a standard Silverlight library. &lt;/li&gt;    &lt;li&gt;Go to the properties of the web project and, on the &lt;b&gt;Web&lt;/b&gt; page, check what port it uses for the &lt;b&gt;Visual Studio Development Server&lt;/b&gt;. My sample application uses the port &lt;b&gt;50750&lt;/b&gt;, so it might be easier for you to use the same one. &lt;/li&gt;    &lt;li&gt;&lt;a href="http://developers.facebook.com/setup/"&gt;Register your application on Facebook&lt;/a&gt;. You will get an application ID (which is the same as a client ID) and a secret key. &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;The FacebookAsync application I want to show you downloads a list of friends from Facebook and displays their names and Facebook IDs. It has a button and a &lt;b&gt;DataGrid&lt;/b&gt; control that autogenerates columns. Here is its XAML.&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:e2a91399-1dca-45fe-aff9-ad0ce2be496a" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background-color: #ffffff; overflow: auto; padding: 2px 5px; white-space: nowrap"&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;UserControl&lt;/span&gt;&lt;span style="color:#ff0000"&gt; x&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#ff0000"&gt;Class&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;FacebookAsync.MainPage&amp;quot;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#ff0000"&gt; xmlns&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&amp;quot;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#ff0000"&gt; xmlns&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#ff0000"&gt;x&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;http://schemas.microsoft.com/winfx/2006/xaml&amp;quot;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#ff0000"&gt; xmlns&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#ff0000"&gt;d&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;http://schemas.microsoft.com/expression/blend/2008&amp;quot;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#ff0000"&gt; xmlns&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#ff0000"&gt;mc&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;http://schemas.openxmlformats.org/markup-compatibility/2006&amp;quot;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#ff0000"&gt; mc&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#ff0000"&gt;Ignorable&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;d&amp;quot;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#ff0000"&gt; d&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#ff0000"&gt;DesignHeight&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;445&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000"&gt; d&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#ff0000"&gt;DesignWidth&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;514&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000"&gt; xmlns&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#ff0000"&gt;sdk&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk&amp;quot;&amp;gt;&lt;/span&gt;&lt;br /&gt; &lt;br /&gt;     &lt;span style="color:#a31515"&gt;&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;Grid&lt;/span&gt;&lt;span style="color:#ff0000"&gt; x&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#ff0000"&gt;Name&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;LayoutRoot&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000"&gt; Background&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;White&amp;quot;&amp;gt;&lt;/span&gt;&lt;br /&gt;         &lt;span style="color:#a31515"&gt;&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;sdk&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#a31515"&gt;DataGrid&lt;/span&gt;&lt;span style="color:#ff0000"&gt; AutoGenerateColumns&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;True&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000"&gt; Height&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;282&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000"&gt; HorizontalAlignment&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;Left&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000"&gt; Margin&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;20,69,0,0&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000"&gt; Name&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;searchResultsGrid&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000"&gt; VerticalAlignment&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;Top&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000"&gt; Width&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;390&amp;quot; /&amp;gt;&lt;/span&gt;&lt;br /&gt;         &lt;span style="color:#a31515"&gt;&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;Button&lt;/span&gt;&lt;span style="color:#ff0000"&gt; Content&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;Get list of friends from Facebook&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000"&gt; Height&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;23&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000"&gt; HorizontalAlignment&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;Left&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000"&gt; Margin&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;25,21,0,0&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000"&gt; Name&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;button1&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000"&gt; VerticalAlignment&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;Top&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000"&gt; Width&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;206&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000"&gt; Click&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;button1_Click&amp;quot; /&amp;gt;&lt;/span&gt;&lt;br /&gt;     &lt;span style="color:#a31515"&gt;&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;Grid&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt; &lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;UserControl&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;To get a list of friends, an application needs to perform several steps (according to &lt;a href="http://developers.facebook.com/docs/api"&gt;Facebook Graph API&lt;/a&gt;), including making two asynchronous calls: &lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;You need to redirect from the application page to the Facebook authorization page and provide a client ID, a secret key, and a return URL as parameters of the URL. The user should authorize the application. (In debugging mode the user is probably you, so you should have your own Facebook account.) &lt;/li&gt;    &lt;li&gt;If the user authorizes the application, the Facebook API redirects to a return URL, which your application provides. Facebook also adds an authorization code into its redirect URL as a parameter. No asynchronous calls are made yet, because this is done by simply navigating between pages. &lt;/li&gt;    &lt;li&gt;The application should exchange the authorization code to an access token by using the GET request. Remember that Silverlight doesn’t have a synchronous API, so you have to make an asynchronous call. &lt;/li&gt;    &lt;li&gt;After the application gets a token, it makes a new GET request to the Facebook API to get a list of friends for the current user. Once again, this call should be asynchronous. &lt;/li&gt;    &lt;li&gt;The application parses the response from Facebook and displays it on the page. &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;The full code of the version that doesn’t use the new &lt;b&gt;async&lt;/b&gt; feature might look like this.&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:2657583d-7ac1-4dcc-ab81-a8509ebaff0f" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background-color: #ffffff; max-height: 500px; overflow: auto; padding: 2px 5px; white-space: nowrap"&gt;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;partial&lt;/span&gt; &lt;span style="color:#0000ff"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af"&gt;MainPage&lt;/span&gt; : &lt;span style="color:#2b91af"&gt;UserControl&lt;/span&gt;&lt;br /&gt; {&lt;br /&gt;     &lt;span style="color:#008000"&gt;// Remember to change to your application URL.&lt;/span&gt;&lt;br /&gt;     &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; redirect_uri = &lt;span style="color:#a31515"&gt;@&amp;quot;http://localhost:50750/FacebookAsyncTestPage.aspx&amp;quot;&lt;/span&gt;;&lt;br /&gt; &lt;br /&gt;     &lt;span style="color:#008000"&gt;// Get these values by authorizing your application on Facebook.&lt;/span&gt;&lt;br /&gt;     &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; client_id = &lt;span style="color:#a31515"&gt;&amp;quot;Your Application ID&amp;quot;&lt;/span&gt;;&lt;br /&gt;     &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; client_secret = &lt;span style="color:#a31515"&gt;&amp;quot;Your Secret Key&amp;quot;&lt;/span&gt;;&lt;br /&gt; &lt;br /&gt;     &lt;span style="color:#008000"&gt;// This collection will contain the list of friends.&lt;/span&gt;&lt;br /&gt;     &lt;span style="color:#2b91af"&gt;ObservableCollection&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af"&gt;FacebookFriend&lt;/span&gt;&amp;gt; searchResults =&lt;br /&gt;         &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;ObservableCollection&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af"&gt;FacebookFriend&lt;/span&gt;&amp;gt;();&lt;br /&gt; &lt;br /&gt;     &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; MainPage()&lt;br /&gt;     {&lt;br /&gt;         InitializeComponent();&lt;br /&gt;         searchResultsGrid.ItemsSource = searchResults;&lt;br /&gt; &lt;br /&gt;         &lt;span style="color:#008000"&gt;// Check whether it was a redirect from the Facebook authorization page.&lt;/span&gt;&lt;br /&gt;         &lt;span style="color:#0000ff"&gt;if&lt;/span&gt; (&lt;span style="color:#2b91af"&gt;HtmlPage&lt;/span&gt;.Document.DocumentUri.Query.Contains(&lt;span style="color:#a31515"&gt;&amp;quot;?code=&amp;quot;&lt;/span&gt;))&lt;br /&gt;         {&lt;br /&gt;             GetFriends();&lt;br /&gt;         }&lt;br /&gt;     }&lt;br /&gt; &lt;br /&gt;     &lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; button1_Click(&lt;span style="color:#0000ff"&gt;object&lt;/span&gt; sender, &lt;span style="color:#2b91af"&gt;RoutedEventArgs&lt;/span&gt; e)&lt;br /&gt;     {&lt;br /&gt;         &lt;span style="color:#008000"&gt;// Navigating to a Facebook authorization page. &lt;/span&gt;&lt;br /&gt;         &lt;span style="color:#008000"&gt;// No asynchronous calls are necessary.&lt;/span&gt;&lt;br /&gt;         &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; facebookAuthUri =&lt;br /&gt;             &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;StringBuilder&lt;/span&gt;(&lt;span style="color:#a31515"&gt;@&amp;quot;https://graph.facebook.com/oauth/authorize?&amp;quot;&lt;/span&gt;);&lt;br /&gt;         facebookAuthUri.Append(&lt;span style="color:#a31515"&gt;&amp;quot;client_id=&amp;quot;&lt;/span&gt;).Append(client_id)&lt;br /&gt;             .Append(&lt;span style="color:#a31515"&gt;@&amp;quot;&amp;amp;redirect_uri=&amp;quot;&lt;/span&gt;).Append(redirect_uri);&lt;br /&gt;         &lt;span style="color:#2b91af"&gt;HtmlPage&lt;/span&gt;.Window.Navigate(&lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Uri&lt;/span&gt;(facebookAuthUri.ToString()));&lt;br /&gt;     }&lt;br /&gt; &lt;br /&gt;     &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; GetFriends()&lt;br /&gt;     {&lt;br /&gt;         &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; code = &lt;span style="color:#2b91af"&gt;HtmlPage&lt;/span&gt;.Document.DocumentUri.Query.TrimStart(&lt;span style="color:#a31515"&gt;&amp;#39;?&amp;#39;&lt;/span&gt;);&lt;br /&gt;         &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; facebookUri =&lt;br /&gt;             &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;StringBuilder&lt;/span&gt;(&lt;span style="color:#a31515"&gt;@&amp;quot;https://graph.facebook.com/oauth/access_token?&amp;quot;&lt;/span&gt;);&lt;br /&gt;         facebookUri.Append(&lt;span style="color:#a31515"&gt;&amp;quot;client_id=&amp;quot;&lt;/span&gt;).Append(client_id)&lt;br /&gt;             .Append(&lt;span style="color:#a31515"&gt;@&amp;quot;&amp;amp;redirect_uri=&amp;quot;&lt;/span&gt;).Append(redirect_uri)&lt;br /&gt;             .Append(&lt;span style="color:#a31515"&gt;@&amp;quot;&amp;amp;client_secret=&amp;quot;&lt;/span&gt;).Append(client_secret)&lt;br /&gt;             .Append(&lt;span style="color:#a31515"&gt;&amp;#39;&amp;amp;&amp;#39;&lt;/span&gt;).Append(code);&lt;br /&gt; &lt;br /&gt;         &lt;span style="color:#008000"&gt;// First asynchronous call, to get an access token.&lt;/span&gt;&lt;br /&gt;         &lt;span style="color:#2b91af"&gt;WebClient&lt;/span&gt; proxy = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;WebClient&lt;/span&gt;();&lt;br /&gt;         proxy.DownloadStringCompleted +=&lt;br /&gt;             &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;DownloadStringCompletedEventHandler&lt;/span&gt;(OnDownloadCompleted_Token);&lt;br /&gt;         proxy.DownloadStringAsync(&lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Uri&lt;/span&gt;(facebookUri.ToString()));&lt;br /&gt;     }&lt;br /&gt; &lt;br /&gt;     &lt;span style="color:#008000"&gt;// Processing results of the first asynchronous call.&lt;/span&gt;&lt;br /&gt;     &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; OnDownloadCompleted_Token(&lt;br /&gt;         &lt;span style="color:#0000ff"&gt;object&lt;/span&gt; sender, &lt;span style="color:#2b91af"&gt;DownloadStringCompletedEventArgs&lt;/span&gt; e)&lt;br /&gt;     {&lt;br /&gt;         &lt;span style="color:#008000"&gt;// Second asynchronous call, to get a list of friends.&lt;/span&gt;&lt;br /&gt;         &lt;span style="color:#2b91af"&gt;WebClient&lt;/span&gt; proxy = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;WebClient&lt;/span&gt;();&lt;br /&gt;         proxy.DownloadStringCompleted +=&lt;br /&gt;             &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;DownloadStringCompletedEventHandler&lt;/span&gt;(OnDownloadCompleted_Friends);&lt;br /&gt;         &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; facebookFriendsUri = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;StringBuilder&lt;/span&gt;(&lt;br /&gt;      &lt;span style="color:#a31515"&gt;@&amp;quot;https://graph.facebook.com/me/friends?fields=id,name&amp;amp;&amp;quot;&lt;/span&gt;);&lt;br /&gt;         facebookFriendsUri.Append((&lt;span style="color:#2b91af"&gt;String&lt;/span&gt;)e.Result);&lt;br /&gt;         proxy.DownloadStringAsync(&lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Uri&lt;/span&gt;(facebookFriendsUri.ToString()));&lt;br /&gt;     }&lt;br /&gt; &lt;br /&gt;     &lt;span style="color:#008000"&gt;// Processing results of the second asynchronous call.&lt;/span&gt;&lt;br /&gt;     &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; OnDownloadCompleted_Friends(&lt;br /&gt;         &lt;span style="color:#0000ff"&gt;object&lt;/span&gt; sender, &lt;span style="color:#2b91af"&gt;DownloadStringCompletedEventArgs&lt;/span&gt; e)&lt;br /&gt;     {&lt;br /&gt;         &lt;span style="color:#2b91af"&gt;JsonObject&lt;/span&gt; jsonObject = (&lt;span style="color:#2b91af"&gt;JsonObject&lt;/span&gt;)&lt;span style="color:#2b91af"&gt;JsonValue&lt;/span&gt;.Parse((&lt;span style="color:#2b91af"&gt;String&lt;/span&gt;)e.Result);&lt;br /&gt;         &lt;span style="color:#2b91af"&gt;JsonArray&lt;/span&gt; jsonArray = (&lt;span style="color:#2b91af"&gt;JsonArray&lt;/span&gt;)jsonObject[&lt;span style="color:#a31515"&gt;&amp;quot;data&amp;quot;&lt;/span&gt;];&lt;br /&gt;         &lt;span style="color:#0000ff"&gt;foreach&lt;/span&gt; (&lt;span style="color:#0000ff"&gt;var&lt;/span&gt; friend &lt;span style="color:#0000ff"&gt;in&lt;/span&gt; jsonArray)&lt;br /&gt;         {&lt;br /&gt;             searchResults.Add(&lt;br /&gt;                 &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;FacebookFriend&lt;/span&gt;() { &lt;br /&gt;                     Name = friend[&lt;span style="color:#a31515"&gt;&amp;quot;name&amp;quot;&lt;/span&gt;], ID = friend[&lt;span style="color:#a31515"&gt;&amp;quot;id&amp;quot;&lt;/span&gt;] });&lt;br /&gt;         }&lt;br /&gt;     }&lt;br /&gt; }&lt;br /&gt; &lt;br /&gt; &lt;span style="color:#008000"&gt;// Items displayed in the data grid.&lt;/span&gt;&lt;br /&gt; &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af"&gt;FacebookFriend&lt;/span&gt;&lt;br /&gt; {&lt;br /&gt;     &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; Name { &lt;span style="color:#0000ff"&gt;get&lt;/span&gt;; &lt;span style="color:#0000ff"&gt;set&lt;/span&gt;; }&lt;br /&gt;     &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; ID { &lt;span style="color:#0000ff"&gt;get&lt;/span&gt;; &lt;span style="color:#0000ff"&gt;set&lt;/span&gt;; }&lt;br /&gt; }&lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;This is what you typically see when you have to deal with asynchronous applications: lots of callbacks, and overall the code is rather hard to read, although I spent some time on beautifying it. Now, let’s see how much easier it might be with the new &lt;b&gt;async&lt;/b&gt; feature.&lt;/p&gt;  &lt;p&gt;XAML will not change. The same is true for the &lt;b&gt;FacebookFriend&lt;/b&gt; class, the button’s event handler, and all the properties in the &lt;b&gt;MainPage&lt;/b&gt; class.&lt;/p&gt;  &lt;p&gt;The changes start, unsurprisingly, in the &lt;b&gt;GetFriends&lt;/b&gt; method. I’m going to make asynchronous calls by using the &lt;b&gt;await&lt;/b&gt; keyword. But to enable this, I have to add the &lt;b&gt;async&lt;/b&gt; keyword to the method signature.&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:fbc48647-af45-46a2-a38e-8db46405ad35" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background-color: #ffffff; overflow: auto; padding: 2px 5px; white-space: nowrap"&gt;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;async&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; GetFriends()&lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;Now I can start changing the code within the method. Let’s take a look at the existing code:&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:082f586e-95e0-48b6-9f92-76f38a725d56" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background-color: #ffffff; overflow: auto; padding: 2px 5px; white-space: nowrap"&gt;&lt;span style="color:#008000"&gt;// First asynchronous call, to get an access token.&lt;/span&gt;&lt;br /&gt; &lt;span style="color:#2b91af"&gt;WebClient&lt;/span&gt; proxy = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;WebClient&lt;/span&gt;();&lt;br /&gt; proxy.DownloadStringCompleted +=&lt;br /&gt;     &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;DownloadStringCompletedEventHandler&lt;/span&gt;(OnDownloadCompleted_Token);&lt;br /&gt; proxy.DownloadStringAsync(&lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Uri&lt;/span&gt;(facebookUri.ToString()));&lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;Instead of making a callback, you can get the result string by using the &lt;b&gt;await&lt;/b&gt; keyword.&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:f5f183fe-61b4-45aa-b272-f53fc302febb" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background-color: #ffffff; overflow: auto; padding: 2px 5px; white-space: nowrap"&gt;&lt;span style="color:#008000"&gt;// First asynchronous call, to get an access token.&lt;/span&gt;&lt;br /&gt; &lt;span style="color:#2b91af"&gt;WebClient&lt;/span&gt; proxy = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;WebClient&lt;/span&gt;();&lt;br /&gt; &lt;span style="color:#2b91af"&gt;String&lt;/span&gt; token = &lt;span style="color:#0000ff"&gt;await&lt;/span&gt; proxy.DownloadStringTaskAsync(&lt;br /&gt;     &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Uri&lt;/span&gt;(facebookUri.ToString()));&lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;Note that I used a different method here: &lt;b&gt;DownloadStringTaskAsync.&lt;/b&gt; This method doesn’t exist in Silverlight today; it’s a part of the CTP release only. In addition to new keywords in the C# and Visual Basic languages, future versions of the .NET Framework and Silverlight need to implement the Task-based Asynchronous Pattern (TAP). The details about this pattern are also included into the CTP release; look for “The Task-based Asynchronous Pattern” document.&lt;/p&gt;  &lt;p&gt;According to this pattern, asynchronous methods should follow a certain naming convention: They should have the “Async” postfix (or “TaskAsync” if a method with the “Async” postfix already exists in the given API, as with the &lt;b&gt;WebClient&lt;/b&gt; class). Such methods return instances of the &lt;a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.task.aspx"&gt;Task&lt;/a&gt; or &lt;a href="http://msdn.microsoft.com/en-us/library/dd321424.aspx"&gt;Task&amp;lt;TResult&amp;gt;&lt;/a&gt; class from the Task Parallel Library. But once again, the compiler does some work for you when you use the &lt;b&gt;await&lt;/b&gt; keyword. In my example, the type of the &lt;b&gt;token&lt;/b&gt; variable is not &lt;b&gt;Task&amp;lt;String&amp;gt;&lt;/b&gt;, but just &lt;b&gt;String&lt;/b&gt;; no extra work such as conversion or type casting is necessary.&lt;/p&gt;  &lt;p&gt;And now you can simply continue writing your application logic in the same method! Everything I had in the &lt;b&gt;OnDownloadCompleted_Token&lt;/b&gt; method can be moved to the &lt;b&gt;GetFriends&lt;/b&gt; method. I don’t even need to create a new &lt;b&gt;WebClient&lt;/b&gt;, since I already have one. &lt;/p&gt;  &lt;p&gt;So, the code &lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:7fa0c814-85e1-4aa9-89f8-9ccb20830b40" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background-color: #ffffff; overflow: auto; padding: 2px 5px; white-space: nowrap"&gt;&lt;span style="color:#008000"&gt;// Second asynchronous call, to get a list of friends.&lt;/span&gt;&lt;br /&gt; &lt;span style="color:#2b91af"&gt;WebClient&lt;/span&gt; proxy = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;WebClient&lt;/span&gt;();&lt;br /&gt; proxy.DownloadStringCompleted +=&lt;br /&gt;     &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;DownloadStringCompletedEventHandler&lt;/span&gt;(OnDownloadCompleted_Friends);&lt;br /&gt; &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; facebookFriendsUri = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;StringBuilder&lt;/span&gt;(&lt;br /&gt;     &lt;span style="color:#a31515"&gt;@&amp;quot;https://graph.facebook.com/me/friends?fields=id,name&amp;amp;&amp;quot;&lt;/span&gt;);&lt;br /&gt; facebookFriendsUri.Append((&lt;span style="color:#2b91af"&gt;String&lt;/span&gt;)e.Result);&lt;br /&gt; proxy.DownloadStringAsync(&lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Uri&lt;/span&gt;(facebookFriendsUri.ToString()));&lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;from the &lt;b&gt;OnDownloadCompleted_Token()&lt;/b&gt; method can be replaced with the following code (once again, using the new &lt;b&gt;await&lt;/b&gt; keyword) and moved to the &lt;b&gt;GetFriends&lt;/b&gt; method.&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:e1689893-3085-4140-b899-69d697b0e6f2" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background-color: #ffffff; overflow: auto; padding: 2px 5px; white-space: nowrap"&gt;&lt;span style="color:#008000"&gt;// Second asynchronous call, to get a list of friends.&lt;/span&gt;&lt;br /&gt; &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; facebookFriendsUri = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;StringBuilder&lt;/span&gt;(&lt;br /&gt;     &lt;span style="color:#a31515"&gt;@&amp;quot;https://graph.facebook.com/me/friends?fields=id,name&amp;amp;&amp;quot;&lt;/span&gt;);&lt;br /&gt; facebookFriendsUri.Append(token);&lt;br /&gt; &lt;span style="color:#2b91af"&gt;String&lt;/span&gt; friends = &lt;span style="color:#0000ff"&gt;await&lt;/span&gt; proxy.DownloadStringTaskAsync(&lt;br /&gt;     &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Uri&lt;/span&gt;(facebookFriendsUri.ToString()));&lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;After that I can simply delete the &lt;b&gt;OnDownloadCompleted_Token &lt;/b&gt;method. In fact, I can make everything even shorter. I don’t need to create the &lt;b&gt;token&lt;/b&gt; variable, because I can use the &lt;b&gt;await&lt;/b&gt; keyword right in the method call, like this:&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:ec3443fa-5b19-4057-83ce-19760505ba5c" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background-color: #ffffff; overflow: auto; padding: 2px 5px; white-space: nowrap"&gt;&lt;span style="color:#0000ff"&gt;var&lt;/span&gt; facebookFriendsUri = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;StringBuilder&lt;/span&gt;(&lt;br /&gt;     &lt;span style="color:#a31515"&gt;@&amp;quot;https://graph.facebook.com/me/friends?fields=id,name&amp;amp;&amp;quot;&lt;/span&gt;);&lt;br /&gt; &lt;span style="color:#2b91af"&gt;WebClient&lt;/span&gt; proxy = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;WebClient&lt;/span&gt;();&lt;br /&gt; &lt;span style="color:#008000"&gt;// First asynchronous call, to get an access token.&lt;/span&gt;&lt;br /&gt; facebookFriendsUri.Append(&lt;span style="color:#0000ff"&gt;await&lt;/span&gt; proxy.DownloadStringTaskAsync(&lt;br /&gt;     &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Uri&lt;/span&gt;(facebookUri.ToString())));&lt;br /&gt; &lt;span style="color:#008000"&gt;// Second asynchronous call, to get a list of friends.&lt;/span&gt;&lt;br /&gt; &lt;span style="color:#2b91af"&gt;String&lt;/span&gt; friends = &lt;span style="color:#0000ff"&gt;await&lt;/span&gt; proxy.DownloadStringTaskAsync(&lt;br /&gt;     &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Uri&lt;/span&gt;(facebookFriendsUri.ToString()));&lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;The last step is to simply move the code from the &lt;b&gt;OnDownloadCompleted_Friends&lt;/b&gt; method to the &lt;b&gt;GetFriends&lt;/b&gt; method and delete the &lt;b&gt;OnDownloadCompleted_Friends&lt;/b&gt; method.&lt;/p&gt;  &lt;p&gt;Here’s what the final version of the &lt;b&gt;GetFriends&lt;/b&gt; method looks like:&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:035f938b-b087-4851-a486-f7ef440c3e98" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background-color: #ffffff; overflow: auto; padding: 2px 5px; white-space: nowrap"&gt;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;async&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; GetFriends()&lt;br /&gt; {&lt;br /&gt;     &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; code = &lt;span style="color:#2b91af"&gt;HtmlPage&lt;/span&gt;.Document.DocumentUri.Query.TrimStart(&lt;span style="color:#a31515"&gt;&amp;#39;?&amp;#39;&lt;/span&gt;);&lt;br /&gt;     &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; facebookUri = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;StringBuilder&lt;/span&gt;(&lt;br /&gt;         &lt;span style="color:#a31515"&gt;@&amp;quot;https://graph.facebook.com/oauth/access_token?&amp;quot;&lt;/span&gt;);&lt;br /&gt;     facebookUri.Append(&lt;span style="color:#a31515"&gt;&amp;quot;client_id=&amp;quot;&lt;/span&gt;).Append(client_id)&lt;br /&gt;         .Append(&lt;span style="color:#a31515"&gt;@&amp;quot;&amp;amp;redirect_uri=&amp;quot;&lt;/span&gt;).Append(redirect_uri)&lt;br /&gt;         .Append(&lt;span style="color:#a31515"&gt;@&amp;quot;&amp;amp;client_secret=&amp;quot;&lt;/span&gt;).Append(client_secret)&lt;br /&gt;         .Append(&lt;span style="color:#a31515"&gt;&amp;#39;&amp;amp;&amp;#39;&lt;/span&gt;).Append(code);&lt;br /&gt; &lt;br /&gt;     &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; facebookFriendsUri = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;StringBuilder&lt;/span&gt;(&lt;br /&gt;         &lt;span style="color:#a31515"&gt;@&amp;quot;https://graph.facebook.com/me/friends?fields=id,name&amp;amp;&amp;quot;&lt;/span&gt;);&lt;br /&gt; &lt;br /&gt;     &lt;span style="color:#2b91af"&gt;WebClient&lt;/span&gt; proxy = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;WebClient&lt;/span&gt;();&lt;br /&gt; &lt;br /&gt;     &lt;span style="color:#008000"&gt;// First asynchronous call, to get an access token.&lt;/span&gt;&lt;br /&gt;     facebookFriendsUri.Append(&lt;span style="color:#0000ff"&gt;await&lt;/span&gt; proxy.DownloadStringTaskAsync(&lt;br /&gt;         &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Uri&lt;/span&gt;(facebookUri.ToString())));&lt;br /&gt; &lt;br /&gt;     &lt;span style="color:#008000"&gt;// Second asynchronous call, to get a list of friends.&lt;/span&gt;&lt;br /&gt;     &lt;span style="color:#2b91af"&gt;String&lt;/span&gt; friends = &lt;span style="color:#0000ff"&gt;await&lt;/span&gt; proxy.DownloadStringTaskAsync(&lt;br /&gt;         &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Uri&lt;/span&gt;(facebookFriendsUri.ToString()));&lt;br /&gt; &lt;br /&gt;     &lt;span style="color:#2b91af"&gt;JsonObject&lt;/span&gt; jsonObject = (&lt;span style="color:#2b91af"&gt;JsonObject&lt;/span&gt;)&lt;span style="color:#2b91af"&gt;JsonValue&lt;/span&gt;.Parse(friends);&lt;br /&gt;     &lt;span style="color:#2b91af"&gt;JsonArray&lt;/span&gt; jsonArray = (&lt;span style="color:#2b91af"&gt;JsonArray&lt;/span&gt;)jsonObject[&lt;span style="color:#a31515"&gt;&amp;quot;data&amp;quot;&lt;/span&gt;];&lt;br /&gt; &lt;br /&gt;     &lt;span style="color:#0000ff"&gt;foreach&lt;/span&gt; (&lt;span style="color:#0000ff"&gt;var&lt;/span&gt; friend &lt;span style="color:#0000ff"&gt;in&lt;/span&gt; jsonArray)&lt;br /&gt;     {&lt;br /&gt;         searchResults.Add(&lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;FacebookFriend&lt;/span&gt;() { &lt;br /&gt;             Name = friend[&lt;span style="color:#a31515"&gt;&amp;quot;name&amp;quot;&lt;/span&gt;], ID = friend[&lt;span style="color:#a31515"&gt;&amp;quot;id&amp;quot;&lt;/span&gt;] });&lt;br /&gt;     }&lt;br /&gt; }&lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;Instead of three methods I have just one, and it’s much more straightforward and easier to read. It simply has less code to begin with. I hope that I gave you just enough information to encourage you to explore more on your own and read more about the &lt;a href="http://msdn.microsoft.com/en-us/vstudio/async.aspx"&gt;Visual Studio Async CTP&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=10082237" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/C_2300_/">C#</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/Visual+Studio+Async+CTP/">Visual Studio Async CTP</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/Task_2D00_based+Asynchronous+Pattern/">Task-based Asynchronous Pattern</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/async/">async</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/await/">await</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/asynchronous+programming/">asynchronous programming</category></item><item><title>Converting a VBA Macro to C# 4.0</title><link>http://blogs.msdn.com/b/csharpfaq/archive/2010/09/28/converting-a-vba-macro-to-c-4-0.aspx</link><pubDate>Tue, 28 Sep 2010 00:30:37 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10068463</guid><dc:creator>Alexandra Rusina</dc:creator><slash:comments>25</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/csharpfaq/rsscomments.aspx?WeblogPostID=10068463</wfw:commentRss><comments>http://blogs.msdn.com/b/csharpfaq/archive/2010/09/28/converting-a-vba-macro-to-c-4-0.aspx#comments</comments><description>&lt;p&gt;I've talked a lot about improved COM interop in C# 4.0 and how much easier it is now to work with Office applications. This time I want to share some tips and tricks on how you can convert Visual Basic for Applications (VBA) macros to C# 4.0 by using Office 2010 and Visual Studio 2010. &lt;/p&gt;  &lt;p&gt;You can either &lt;a href="http://msdn.microsoft.com/en-us/vcsharp/ff962528.aspx"&gt;watch a video&lt;/a&gt; or read this post: it’s the same scenario and the same code, only I tried a different medium this time. If you for some reason decide to take a look at both, let me know which one you liked more and why.&lt;/p&gt;  &lt;p&gt;One common scenario for people working on Office applications is to record a macro in Office and then use the results in their code. It's often much faster and easier than looking up all the methods and properties. &lt;/p&gt;  &lt;p&gt;To try this out, record a macro in Excel 2010: create a new workbook, fill a column with numbers from 1 to 10 by using the Auto Fill feature, change the color of the column, and then create a simple graph. &lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/CommunityServer-Blogs-Components-WeblogFiles/00-00-00-33-48-metablogapi/4670.image_5F00_68F53564.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/CommunityServer-Blogs-Components-WeblogFiles/00-00-00-33-48-metablogapi/7801.image_5F00_thumb_5F00_5CF34530.png" width="475" height="273" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Here's the VBA macro you'll get as a result:&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New"&gt;Sub Macro1()&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New"&gt;&amp;#160;&amp;#160;&amp;#160; ActiveCell.FormulaR1C1 = &amp;quot;1&amp;quot;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New"&gt;&amp;#160;&amp;#160;&amp;#160; Range(&amp;quot;A2&amp;quot;).Select&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New"&gt;&amp;#160;&amp;#160;&amp;#160; ActiveCell.FormulaR1C1 = &amp;quot;2&amp;quot;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New"&gt;&amp;#160;&amp;#160;&amp;#160; Range(&amp;quot;A1:A2&amp;quot;).Select&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New"&gt;&amp;#160;&amp;#160;&amp;#160; Selection.AutoFill Destination:=Range(&amp;quot;A1:A10&amp;quot;), Type:=xlFillDefault&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New"&gt;&amp;#160;&amp;#160;&amp;#160; Range(&amp;quot;A1:A10&amp;quot;).Select&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New"&gt;&amp;#160;&amp;#160;&amp;#160; With Selection.Interior&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .Pattern = xlSolid&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .PatternColorIndex = xlAutomatic&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .ThemeColor = xlThemeColorAccent1&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .TintAndShade = 0.399945066682943&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .PatternTintAndShade = 0&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New"&gt;&amp;#160;&amp;#160;&amp;#160; End With&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New"&gt;&amp;#160;&amp;#160;&amp;#160; Range(&amp;quot;A1:A10&amp;quot;).Select&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New"&gt;&amp;#160;&amp;#160;&amp;#160; ActiveSheet.Shapes.AddChart.Select&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New"&gt;&amp;#160;&amp;#160;&amp;#160; ActiveChart.ChartType = xlConeColStacked&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New"&gt;&amp;#160;&amp;#160;&amp;#160; ActiveChart.SetSourceData Source:=Range(&amp;quot;Sheet1!$A$1:$A$10&amp;quot;)&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New"&gt;End Sub&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;Now open Visual Studio 2010, create a new project, and add this reference to the project: &lt;b&gt;Microsoft.Office.Interop.Excel&lt;/b&gt;. Then copy the VBA script inside the following code:&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:fec629ef-3152-43c1-bcb0-c3bb1061ef9d" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background-color: #ffffff; overflow: auto; padding: 2px 5px; white-space: nowrap"&gt;&lt;span style="color:#0000ff"&gt;using&lt;/span&gt; Excel = Microsoft.Office.Interop.Excel;&lt;br /&gt; &lt;br /&gt; &lt;span style="color:#0000ff"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Program&lt;/span&gt;&lt;br /&gt; {&lt;br /&gt;     &lt;span style="color:#0000ff"&gt;static&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; Main(&lt;span style="color:#0000ff"&gt;string&lt;/span&gt;[] args)&lt;br /&gt;     {&lt;br /&gt;         &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; excelApp = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; Excel.&lt;span style="color:#2b91af"&gt;Application&lt;/span&gt;();&lt;br /&gt;         excelApp.Workbooks.Add();&lt;br /&gt; &lt;br /&gt;         &lt;span style="color:#008000"&gt;// Insert VBA code here.&lt;/span&gt;&lt;br /&gt; &lt;br /&gt;         excelApp.Visible = &lt;span style="color:#0000ff"&gt;true&lt;/span&gt;;&lt;br /&gt;     }&lt;br /&gt; }&lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;Of course, the VBA code is all highlighted as an error. Here are the steps that you can use to convert that code into C# code:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;In this macro, all the objects are in fact properties of the Excel application object. Basically, you need to add &lt;b&gt;excelApp&lt;/b&gt; to the beginning of each line.       &lt;br /&gt;Press ALT and select an area at the start of the lines, as shown here:&amp;#160;&amp;#160; &lt;br /&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/CommunityServer-Blogs-Components-WeblogFiles/00-00-00-33-48-metablogapi/0535.image_5F00_0D3A19E7.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/CommunityServer-Blogs-Components-WeblogFiles/00-00-00-33-48-metablogapi/4670.image_5F00_thumb_5F00_1D65E1E0.png" width="546" height="487" /&gt;&lt;/a&gt;&amp;#160; &lt;br /&gt;Now type &lt;b&gt;excelApp&lt;/b&gt;. and notice that it appears at the start of all the lines at once. &lt;/li&gt;    &lt;li&gt;You can use the same trick to add &lt;b&gt;Selection.Interior&lt;/b&gt; to all the lines in the &lt;b&gt;With&lt;/b&gt; block. Then delete the first and the last elements of the block (&lt;b&gt;With&lt;/b&gt; and &lt;b&gt;End With&lt;/b&gt;).       &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/CommunityServer-Blogs-Components-WeblogFiles/00-00-00-33-48-metablogapi/4263.image_5F00_03FDDEA6.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/CommunityServer-Blogs-Components-WeblogFiles/00-00-00-33-48-metablogapi/5756.image_5F00_thumb_5F00_0AB0E829.png" width="442" height="99" /&gt;&lt;/a&gt; &lt;/p&gt;   &lt;/li&gt;    &lt;li&gt;     &lt;p&gt;For all the &lt;b&gt;Range&lt;/b&gt; objects, you need to replace the parentheses with square brackets. This is because in C# you need to use &lt;a href="http://msdn.microsoft.com/en-us/library/ee310208.aspx"&gt;indexed properties&lt;/a&gt;, and their syntax requires, well, square brackets. In this piece of code all of the parentheses should become square brackets, so you can use the Find and Replace feature. (The keyboard shortcut for this is Ctrl + H.)         &lt;br /&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/CommunityServer-Blogs-Components-WeblogFiles/00-00-00-33-48-metablogapi/4744.image_5F00_7C726F38.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/CommunityServer-Blogs-Components-WeblogFiles/00-00-00-33-48-metablogapi/8880.image_5F00_thumb_5F00_77FBEE71.png" width="256" height="301" /&gt;&lt;/a&gt; &lt;/p&gt;   &lt;/li&gt;    &lt;li&gt;     &lt;p&gt;The next step is to replace the syntax for the &lt;a href="http://msdn.microsoft.com/en-us/library/dd264739.aspx"&gt;named parameters&lt;/a&gt;. VBA uses the &amp;quot;:=&amp;quot; operator, while in C# 4.0 it's simply &amp;quot;:&amp;quot;. Once again, simple find and replace can help.&lt;/p&gt;   &lt;/li&gt;    &lt;li&gt;     &lt;p&gt;Now you need to add semicolons to each line, and add parentheses after each method call. You could probably find a converter that would do this for you, but here you have such a small piece of code that you can do it manually.        &lt;br /&gt;        &lt;br /&gt;&lt;strong&gt;Update:          &lt;br /&gt;&lt;/strong&gt;You can use the multi-line editing for adding semicolons as well. You can select a column in a so-called “virtual space”, where no characters were typed in.         &lt;br /&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/CommunityServer-Blogs-Components-WeblogFiles/00-00-00-33-48-metablogapi/7206.image_5F00_thumb_5F00_75B4F11C.png" width="521" height="94" /&gt;         &lt;br /&gt;So, you can type several semicolons at once and then reformat the document. Thanks to Jan B and David Nelson for the tip.&lt;/p&gt;   &lt;/li&gt;    &lt;li&gt;     &lt;p&gt;Let’s take a look at the code once again.        &lt;br /&gt;&amp;#160;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/CommunityServer-Blogs-Components-WeblogFiles/00-00-00-33-48-metablogapi/1440.image_5F00_1BB5160C.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/CommunityServer-Blogs-Components-WeblogFiles/00-00-00-33-48-metablogapi/5340.image_5F00_thumb_5F00_5FA8D456.png" width="618" height="489" /&gt;&lt;/a&gt; &lt;/p&gt;   &lt;/li&gt;    &lt;li&gt;     &lt;p&gt;The last thing to fix is the constants. Press Ctrl+Alt+J to open the &lt;b&gt;Object Browser&lt;/b&gt;, and then search for each constant. You should find a class that contains this enumeration:         &lt;br /&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/CommunityServer-Blogs-Components-WeblogFiles/00-00-00-33-48-metablogapi/1033.image_5F00_581D64E9.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/CommunityServer-Blogs-Components-WeblogFiles/00-00-00-33-48-metablogapi/8473.image_5F00_thumb_5F00_53A6E422.png" width="482" height="131" /&gt;&lt;/a&gt;&amp;#160; &lt;br /&gt;All you need to do is to add the name of the class to the beginning of the constant.         &lt;br /&gt;&lt;/p&gt;      &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:92cc6843-49ad-46f5-b7ed-20cff0e03032" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background-color: #ffffff; overflow: auto; padding: 2px 5px; white-space: nowrap"&gt;excelApp.Selection.AutoFill(&lt;br /&gt;     Destination: excelApp.Range[&lt;span style="color:#a31515"&gt;&amp;quot;A1:A10&amp;quot;&lt;/span&gt;], &lt;br /&gt;     Type: Excel.&lt;span style="color:#2b91af"&gt;XlAutoFillType&lt;/span&gt;.xlFillDefault);&lt;/div&gt; &lt;/div&gt; &lt;/div&gt;   &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;That’s it. Now you can compile and run the program and get exactly the same picture that you saw at the beginning of this post. Here's the final version of this little program.&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:ec5a9e7a-5049-4cb5-9e4e-f0353c8e406b" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background-color: #ffffff; max-height: 300px; overflow: auto; padding: 2px 5px; white-space: nowrap"&gt;&lt;span style="color:#0000ff"&gt;using&lt;/span&gt; Excel = Microsoft.Office.Interop.Excel;&lt;br /&gt; &lt;br /&gt; &lt;span style="color:#0000ff"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Program&lt;/span&gt;&lt;br /&gt; {&lt;br /&gt;     &lt;span style="color:#0000ff"&gt;static&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; Main(&lt;span style="color:#0000ff"&gt;string&lt;/span&gt;[] args)&lt;br /&gt;     {&lt;br /&gt;         &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; excelApp = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; Excel.&lt;span style="color:#2b91af"&gt;Application&lt;/span&gt;();&lt;br /&gt;         excelApp.Workbooks.Add();&lt;br /&gt; &lt;br /&gt;         excelApp.ActiveCell.FormulaR1C1 = &lt;span style="color:#a31515"&gt;&amp;quot;1&amp;quot;&lt;/span&gt;;&lt;br /&gt;         excelApp.Range[&lt;span style="color:#a31515"&gt;&amp;quot;A2&amp;quot;&lt;/span&gt;].Select();&lt;br /&gt;         excelApp.ActiveCell.FormulaR1C1 = &lt;span style="color:#a31515"&gt;&amp;quot;2&amp;quot;&lt;/span&gt;;&lt;br /&gt;         excelApp.Range[&lt;span style="color:#a31515"&gt;&amp;quot;A1:A2&amp;quot;&lt;/span&gt;].Select();&lt;br /&gt;         excelApp.Selection.AutoFill(&lt;br /&gt;             Destination: excelApp.Range[&lt;span style="color:#a31515"&gt;&amp;quot;A1:A10&amp;quot;&lt;/span&gt;], &lt;br /&gt;             Type: Excel.&lt;span style="color:#2b91af"&gt;XlAutoFillType&lt;/span&gt;.xlFillDefault);&lt;br /&gt;         excelApp.Range[&lt;span style="color:#a31515"&gt;&amp;quot;A1:A10&amp;quot;&lt;/span&gt;].Select();&lt;br /&gt; &lt;br /&gt;         excelApp.Selection.Interior.Pattern = Excel.&lt;span style="color:#2b91af"&gt;Constants&lt;/span&gt;.xlSolid;&lt;br /&gt;         excelApp.Selection.Interior.PatternColorIndex = Excel.&lt;span style="color:#2b91af"&gt;Constants&lt;/span&gt;.xlAutomatic;&lt;br /&gt;         excelApp.Selection.Interior.ThemeColor = Excel.&lt;span style="color:#2b91af"&gt;XlThemeColor&lt;/span&gt;.xlThemeColorAccent1;&lt;br /&gt;         excelApp.Selection.Interior.TintAndShade = 0.399945066682943;&lt;br /&gt;         excelApp.Selection.Interior.PatternTintAndShade = 0;&lt;br /&gt; &lt;br /&gt;         excelApp.Range[&lt;span style="color:#a31515"&gt;&amp;quot;A1:A10&amp;quot;&lt;/span&gt;].Select();&lt;br /&gt;         excelApp.ActiveSheet.Shapes.AddChart.Select();&lt;br /&gt;         excelApp.ActiveChart.ChartType = Excel.&lt;span style="color:#2b91af"&gt;XlChartType&lt;/span&gt;.xlConeColStacked;&lt;br /&gt;         excelApp.ActiveChart.SetSourceData(Source: excelApp.Range[&lt;span style="color:#a31515"&gt;&amp;quot;Sheet1!$A$1:$A$10&amp;quot;&lt;/span&gt;]);&lt;br /&gt; &lt;br /&gt;         excelApp.Visible = &lt;span style="color:#0000ff"&gt;true&lt;/span&gt;;&lt;br /&gt;     }&lt;br /&gt; }&lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;If you've read all that and still feel like you missed some steps, watch the video: &lt;a href="http://msdn.microsoft.com/en-us/vcsharp/ff962528.aspx"&gt;How Do I: Convert Visual Basic for Applications Macro to C# 4.0&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;P.S.&lt;/p&gt;  &lt;p&gt;Thanks to Mads Torgersen and Alex Turner for reviewing this and providing helpful comments, to Mick Alberts for editing.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10068463" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/IDE/">IDE</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/Tips/">Tips</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/Visual+Studio+2010/">Visual Studio 2010</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/C_2300_+4-0/">C# 4.0</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/C_2300_/">C#</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/Visual+Studio/">Visual Studio</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/VBA/">VBA</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/Visual+Basic+for+Applications/">Visual Basic for Applications</category></item><item><title>Blocking Collection and the Producer-Consumer Problem</title><link>http://blogs.msdn.com/b/csharpfaq/archive/2010/08/12/blocking-collection-and-the-producer-consumer-problem.aspx</link><pubDate>Wed, 11 Aug 2010 23:18:48 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10049130</guid><dc:creator>Alexandra Rusina</dc:creator><slash:comments>11</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/csharpfaq/rsscomments.aspx?WeblogPostID=10049130</wfw:commentRss><comments>http://blogs.msdn.com/b/csharpfaq/archive/2010/08/12/blocking-collection-and-the-producer-consumer-problem.aspx#comments</comments><description>&lt;p&gt;This time I want to discuss features that belong to the new &lt;a href="http://msdn.microsoft.com/en-us/library/system.collections.concurrent.aspx"&gt;System.Collections.Concurrent&lt;/a&gt; namespace in the.NET Framework 4. When you design parallel applications, you often need thread-safe data storage as well as some mechanism of sending messages between tasks. Once again, this post will touch on just the basics and the most common problems a beginner might encounter, but I’ll provide links for further reading. &lt;/p&gt;  &lt;p&gt;This is the fourth post in the &lt;a href="http://blogs.msdn.com/b/csharpfaq/archive/tags/parallel_2d00_programming/"&gt;parallel programming series&lt;/a&gt;. Here’s a list of all the posts:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://blogs.msdn.com/b/csharpfaq/archive/2010/06/01/parallel-programming-in-net-framework-4-getting-started.aspx"&gt;Getting Started&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.msdn.com/b/csharpfaq/archive/2010/06/18/parallel-programming-task-schedulers-and-synchronization-context.aspx"&gt;Task Schedulers and Synchronization Context&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.msdn.com/b/csharpfaq/archive/2010/07/19/parallel-programming-task-cancellation.aspx"&gt;Task Cancellation&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;Blocking Collection and the Producer-Consumer Problem (this post) &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;To keep things short, I’ll start with the code that I have at the end of the &lt;a href="http://blogs.msdn.com/b/csharpfaq/archive/2010/06/18/parallel-programming-task-schedulers-and-synchronization-context.aspx"&gt;Task Schedulers and Synchronization Context&lt;/a&gt; post. This is a small parallel WPF application with a responsive UI that has one &lt;b&gt;Start&lt;/b&gt; button and displays the results of long-running operations in a text box.&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:a3e87d71-1fa5-4c99-82ec-12d7839ee90e" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background-color: #ffffff; max-height: 300px; overflow: auto; padding: 2px 5px; white-space: nowrap"&gt;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;partial&lt;/span&gt; &lt;span style="color:#0000ff"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af"&gt;MainWindow&lt;/span&gt; : &lt;span style="color:#2b91af"&gt;Window&lt;/span&gt;&lt;br /&gt; {&lt;br /&gt;     &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; MainWindow()&lt;br /&gt;     {&lt;br /&gt;         InitializeComponent();&lt;br /&gt;     }&lt;br /&gt; &lt;br /&gt;     &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;static&lt;/span&gt; &lt;span style="color:#0000ff"&gt;double&lt;/span&gt; SumRootN(&lt;span style="color:#0000ff"&gt;int&lt;/span&gt; root)&lt;br /&gt;     {&lt;br /&gt;         &lt;span style="color:#0000ff"&gt;double&lt;/span&gt; result = 0;&lt;br /&gt;         &lt;span style="color:#0000ff"&gt;for&lt;/span&gt; (&lt;span style="color:#0000ff"&gt;int&lt;/span&gt; i = 1; i &amp;lt; 10000000; i++)&lt;br /&gt;         {&lt;br /&gt;             result += &lt;span style="color:#2b91af"&gt;Math&lt;/span&gt;.Exp(&lt;span style="color:#2b91af"&gt;Math&lt;/span&gt;.Log(i) / root);&lt;br /&gt;         }&lt;br /&gt;         &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; result;&lt;br /&gt;     }&lt;br /&gt; &lt;br /&gt;     &lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; start_Click(&lt;span style="color:#0000ff"&gt;object&lt;/span&gt; sender, &lt;span style="color:#2b91af"&gt;RoutedEventArgs&lt;/span&gt; e)&lt;br /&gt;     {&lt;br /&gt;         textBlock1.Text = &lt;span style="color:#a31515"&gt;&amp;quot;&amp;quot;&lt;/span&gt;;&lt;br /&gt;         label1.Content = &lt;span style="color:#a31515"&gt;&amp;quot;Milliseconds: &amp;quot;&lt;/span&gt;;&lt;br /&gt; &lt;br /&gt;         &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; watch = &lt;span style="color:#2b91af"&gt;Stopwatch&lt;/span&gt;.StartNew();&lt;br /&gt;         &lt;span style="color:#2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af"&gt;Task&lt;/span&gt;&amp;gt; tasks = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af"&gt;Task&lt;/span&gt;&amp;gt;();&lt;br /&gt;         &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; ui = &lt;span style="color:#2b91af"&gt;TaskScheduler&lt;/span&gt;.FromCurrentSynchronizationContext();&lt;br /&gt;         &lt;span style="color:#0000ff"&gt;for&lt;/span&gt; (&lt;span style="color:#0000ff"&gt;int&lt;/span&gt; i = 2; i &amp;lt; 20; i++)&lt;br /&gt;         {&lt;br /&gt;             &lt;span style="color:#0000ff"&gt;int&lt;/span&gt; j = i;&lt;br /&gt;             &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; compute = &lt;span style="color:#2b91af"&gt;Task&lt;/span&gt;.Factory.StartNew(() =&amp;gt;&lt;br /&gt;             {&lt;br /&gt;                 &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; SumRootN(j);&lt;br /&gt;             });&lt;br /&gt;             tasks.Add(compute);&lt;br /&gt; &lt;br /&gt;             &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; display = compute.ContinueWith(resultTask =&amp;gt;&lt;br /&gt;                             textBlock1.Text += &lt;span style="color:#a31515"&gt;&amp;quot;root &amp;quot;&lt;/span&gt; + j.ToString() + &lt;span style="color:#a31515"&gt;&amp;quot; &amp;quot;&lt;/span&gt; +&lt;br /&gt;                                                compute.Result.ToString() +&lt;br /&gt;                                                &lt;span style="color:#2b91af"&gt;Environment&lt;/span&gt;.NewLine,&lt;br /&gt;                                 ui);&lt;br /&gt;         }&lt;br /&gt; &lt;br /&gt;         &lt;span style="color:#2b91af"&gt;Task&lt;/span&gt;.Factory.ContinueWhenAll(tasks.ToArray(),&lt;br /&gt;             result =&amp;gt;&lt;br /&gt;             {&lt;br /&gt;                 &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; time = watch.ElapsedMilliseconds;&lt;br /&gt;                 label1.Content += time.ToString();&lt;br /&gt;             }, &lt;span style="color:#2b91af"&gt;CancellationToken&lt;/span&gt;.None, &lt;span style="color:#2b91af"&gt;TaskContinuationOptions&lt;/span&gt;.None, ui);&lt;br /&gt;     }&lt;br /&gt; }&lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;But imagine that I’m designing a larger application and I need to store the results of the long-running parallel operations somewhere. (I don’t do this in the current version at all.) &lt;/p&gt;  &lt;p&gt;Until .NET Framework 4, this task was challenging for C# developers: the collections in the &lt;a href="http://msdn.microsoft.com/en-us/library/system.collections.aspx"&gt;System.Collections&lt;/a&gt; and &lt;a href="http://msdn.microsoft.com/en-us/library/system.collections.generic.aspx"&gt;System.Collections.Generic&lt;/a&gt; namespaces do not guarantee thread safety, and developers needed to design the locking and synchronization mechanisms themselves. But now generic thread-safe collections are a part of the .NET Framework. So, let me introduce the new namespace: &lt;a href="http://msdn.microsoft.com/en-us/library/system.collections.concurrent.aspx"&gt;System.Collections.Concurrent&lt;/a&gt;. &lt;/p&gt;  &lt;p&gt;I’m going to use the &lt;a href="http://msdn.microsoft.com/en-us/library/dd267312.aspx"&gt;BlockingCollection&amp;lt;T&amp;gt;&lt;/a&gt; class. This class can help you implement the well-known &lt;a href="http://en.wikipedia.org/wiki/Producer-consumer_problem"&gt;producer-consumer pattern&lt;/a&gt;, where items are produced and consumed by different operations at different rates. I will update my application to imitate the producer-consumer scenario, so that the &lt;b&gt;compute&lt;/b&gt; task will become a producer, and the &lt;b&gt;display&lt;/b&gt; task will become a consumer.&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:91f1fea5-7373-4572-953e-e2e5b4ea63f5" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background-color: #ffffff; overflow: auto; padding: 2px 5px; white-space: nowrap"&gt;&lt;span style="color:#0000ff"&gt;var&lt;/span&gt; results = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;BlockingCollection&lt;/span&gt;&amp;lt;&lt;span style="color:#0000ff"&gt;double&lt;/span&gt;&amp;gt;();&lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;I’ll also update the &lt;b&gt;compute&lt;/b&gt; task, so that instead of returning the result, it will add it to the &lt;b&gt;results&lt;/b&gt; collection.&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:16c857c3-34e9-4366-98fe-0a7b6a8f8c3e" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background-color: #ffffff; overflow: auto; padding: 2px 5px; white-space: nowrap"&gt;&lt;span style="color:#0000ff"&gt;var&lt;/span&gt; compute = &lt;span style="color:#2b91af"&gt;Task&lt;/span&gt;.Factory.StartNew(() =&amp;gt;&lt;br /&gt; {&lt;br /&gt;     results.Add(SumRootN(j));&lt;br /&gt; });&lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;When all the results are ready, I’ll set the collection to the “completed” state, so the consumer will know that this collection won’t be updated anymore. For this purpose, I’ll call the &lt;a href="http://msdn.microsoft.com/en-us/library/dd287086.aspx"&gt;CompleteAdding&lt;/a&gt; method, which will set the &lt;a href="http://msdn.microsoft.com/en-us/library/dd267315.aspx"&gt;IsCompleted&lt;/a&gt; property of the &lt;b&gt;results&lt;/b&gt; collection to &lt;b&gt;true&lt;/b&gt;. The good place to perform this operation is the task that calculates the total time: it waits for all other tasks to finish, which is exactly what I need.&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:566485bb-b771-4349-a7ef-9d963a9ddf42" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background-color: #ffffff; overflow: auto; padding: 2px 5px; white-space: nowrap"&gt;&lt;span style="color:#2b91af"&gt;Task&lt;/span&gt;.Factory.ContinueWhenAll(tasks.ToArray(),&lt;br /&gt;     result =&amp;gt;&lt;br /&gt;     {&lt;br /&gt;         results.CompleteAdding();&lt;br /&gt;         &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; time = watch.ElapsedMilliseconds;&lt;br /&gt;         label1.Content += time.ToString();&lt;br /&gt;     }, &lt;span style="color:#2b91af"&gt;CancellationToken&lt;/span&gt;.None, &lt;span style="color:#2b91af"&gt;TaskContinuationOptions&lt;/span&gt;.None, ui);&lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;As you can see, the producer part is easy: all tasks can safely write to the &lt;b&gt;results&lt;/b&gt; collection, and all locking and synchronization issues are managed by the .NET Framework and TPL.&lt;/p&gt;  &lt;p&gt;Now let’s move to the consumer side. I’ll do a small refactoring: I’ll convert the &lt;b&gt;display&lt;/b&gt; task into a &lt;b&gt;consume&lt;/b&gt; task that will run the &lt;b&gt;display&lt;/b&gt; method:&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:b688d09d-3624-47cd-aa94-8a8dd9d0d474" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background-color: #ffffff; overflow: auto; padding: 2px 5px; white-space: nowrap"&gt;&lt;span style="color:#0000ff"&gt;var&lt;/span&gt; consume = &lt;span style="color:#2b91af"&gt;Task&lt;/span&gt;.Factory.StartNew(() =&amp;gt; display(results));&lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;I want to start the &lt;b&gt;consume&lt;/b&gt; task before I start any of the &lt;b&gt;compute&lt;/b&gt; tasks so that the consumer can wait for the producer and I can see the real-time results. That’s why I put the above line right before the main &lt;b&gt;for&lt;/b&gt; loop in the button’s event handler.&lt;/p&gt;  &lt;p&gt;This is what the naïve first version of the &lt;b&gt;display&lt;/b&gt; method might look like. (Don’t forget to convert the &lt;b&gt;ui&lt;/b&gt; task scheduler into a field. It’s a local variable in the original code.)&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:4dd9735f-4fe3-4d18-830f-f1bcbcc7ee66" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background-color: #ffffff; overflow: auto; padding: 2px 5px; white-space: nowrap"&gt;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; display(&lt;span style="color:#2b91af"&gt;BlockingCollection&lt;/span&gt;&amp;lt;&lt;span style="color:#0000ff"&gt;double&lt;/span&gt;&amp;gt; results)&lt;br /&gt; {&lt;br /&gt;     &lt;span style="color:#0000ff"&gt;double&lt;/span&gt; item;&lt;br /&gt;     &lt;span style="color:#0000ff"&gt;while&lt;/span&gt; (!results.IsCompleted)&lt;br /&gt;     {&lt;br /&gt;         &lt;span style="color:#0000ff"&gt;while&lt;/span&gt; (!results.TryTake(&lt;span style="color:#0000ff"&gt;out&lt;/span&gt; item));&lt;br /&gt;         &lt;span style="color:#0000ff"&gt;double&lt;/span&gt; currentItem = item;&lt;br /&gt;         &lt;span style="color:#2b91af"&gt;Task&lt;/span&gt;.Factory.StartNew(&lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Action&lt;/span&gt;(() =&amp;gt;&lt;br /&gt;                   textBlock1.Text += currentItem.ToString() + &lt;span style="color:#2b91af"&gt;Environment&lt;/span&gt;.NewLine),&lt;br /&gt;              &lt;span style="color:#2b91af"&gt;CancellationToken&lt;/span&gt;.None, &lt;span style="color:#2b91af"&gt;TaskCreationOptions&lt;/span&gt;.None, ui);&lt;br /&gt;     }&lt;br /&gt; }&lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;This method checks for new elements in the collection, until it’s notified that the collection is completed, which means that it has finished adding new items. If it gets a new element from the collection, it immediately removes the item and prints the value into the UI threads. Did you notice that I copied &lt;b&gt;item&lt;/b&gt; to &lt;b&gt;currentItem&lt;/b&gt;? It’s all about closure again: you’ll get a list of zero’s otherwise.&lt;/p&gt;  &lt;p&gt;This version works and you won’t get any exceptions. But if you run it on a dual-core computer like I did, you’ll discover that it takes twice as long as the version that doesn’t use the collection. In fact, it runs as if the application weren’t parallelized at all! This is just one of the problems that you might run into, so don’t forget to always measure the performance of your parallel applications: it’s easy to cancel out the benefits of parallelization.&lt;/p&gt;  &lt;p&gt;Of course, the problem is in this line:&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:f347f40b-07b3-47df-a7bc-cb00a40afed7" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background-color: #ffffff; overflow: auto; padding: 2px 5px; white-space: nowrap"&gt;&lt;span style="color:#0000ff"&gt;while&lt;/span&gt; (!results.TryTake(&lt;span style="color:#0000ff"&gt;out&lt;/span&gt; item));&lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;An empty loop is rarely a good idea. This was an attempt to implement some kind of messaging between the threads – the consumer is constantly checking the collection and starts working only if it can retrieve a value from it. And it does this over and over again, so one of my processors is fully occupied with this work and can’t compute the values anymore.&lt;/p&gt;  &lt;p&gt;One simple trick is to make this loop to consume less processing power. It can be as easy as this (however, this is not a recommended way, but rather an illustration of the principle):&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:ad7d8398-bfb0-4dfa-ae0b-5e9eacf70106" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background-color: #ffffff; overflow: auto; padding: 2px 5px; white-space: nowrap"&gt;&lt;span style="color:#0000ff"&gt;while&lt;/span&gt; (!results.TryTake(&lt;span style="color:#0000ff"&gt;out&lt;/span&gt; item)) &lt;span style="color:#2b91af"&gt;Thread&lt;/span&gt;.Sleep(200);&lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;Now after each attempt the task simply waits for 200 milliseconds before trying again. And during those 200 milliseconds the processor can compute the results this task is actually waiting for. You can compile and run the code to make sure that the performance indeed improved.&lt;/p&gt;  &lt;p&gt;However, it might be tricky to find the perfect wait time. Ideally, I need some kind of message from the collection notifying me that the value was added. &lt;/p&gt;  &lt;p&gt;In a blocking collection, you can do this by using a &lt;b&gt;foreach&lt;/b&gt; loop. The &lt;a href="http://msdn.microsoft.com/en-us/library/dd267312.aspx"&gt;BlockingCollection&lt;/a&gt; class has the &lt;a href="http://msdn.microsoft.com/en-us/library/dd287186.aspx"&gt;GetConsumingEnumerable&lt;/a&gt; method that can be used to enumerate through the blocking collection and consume its elements until the collection is completed. It might look like this:&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:682030c5-7665-4c76-9970-e2bb6661038b" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background-color: #ffffff; overflow: auto; padding: 2px 5px; white-space: nowrap"&gt;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; display(&lt;span style="color:#2b91af"&gt;BlockingCollection&lt;/span&gt;&amp;lt;&lt;span style="color:#0000ff"&gt;double&lt;/span&gt;&amp;gt; results)&lt;br /&gt; {&lt;br /&gt;     &lt;span style="color:#0000ff"&gt;foreach&lt;/span&gt; (&lt;span style="color:#0000ff"&gt;var&lt;/span&gt; item &lt;span style="color:#0000ff"&gt;in&lt;/span&gt; results.GetConsumingEnumerable())&lt;br /&gt;     {&lt;br /&gt;         &lt;span style="color:#0000ff"&gt;double&lt;/span&gt; currentItem = item;&lt;br /&gt;         &lt;span style="color:#2b91af"&gt;Task&lt;/span&gt;.Factory.StartNew(&lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Action&lt;/span&gt;(() =&amp;gt;&lt;br /&gt;              textBlock1.Text += currentItem.ToString() + &lt;span style="color:#2b91af"&gt;Environment&lt;/span&gt;.NewLine),&lt;br /&gt;         &lt;span style="color:#2b91af"&gt;CancellationToken&lt;/span&gt;.None, &lt;span style="color:#2b91af"&gt;TaskCreationOptions&lt;/span&gt;.None, ui);&lt;br /&gt;     }&lt;br /&gt; }&lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;Now the &lt;b&gt;display&lt;/b&gt; method checks whether there is an item in the &lt;b&gt;results&lt;/b&gt; collection and, if there is, consumes the item. When the collection is completed and empty, execution exits the loop. All the locking, synchronization, and messaging between the tasks are managed by the TPL.&lt;/p&gt;  &lt;p&gt;The resulting application will probably still be a little slower than the version that didn’t use the collection at all, but of course writing to and reading from thread-safe data storage added some overhead.&lt;/p&gt;  &lt;p&gt;If you got lost in all the changes, here’s the full code:&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:44cf9ee4-c534-4bc3-8794-a857d5b3419e" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background-color: #ffffff; max-height: 300px; overflow: auto; padding: 2px 5px; white-space: nowrap"&gt;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;partial&lt;/span&gt; &lt;span style="color:#0000ff"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af"&gt;MainWindow&lt;/span&gt; : &lt;span style="color:#2b91af"&gt;Window&lt;/span&gt;&lt;br /&gt; {&lt;br /&gt;     &lt;span style="color:#2b91af"&gt;TaskScheduler&lt;/span&gt; ui = &lt;span style="color:#2b91af"&gt;TaskScheduler&lt;/span&gt;.FromCurrentSynchronizationContext();&lt;br /&gt; &lt;br /&gt;     &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; MainWindow()&lt;br /&gt;     {&lt;br /&gt;         InitializeComponent();&lt;br /&gt;     }&lt;br /&gt; &lt;br /&gt;     &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;static&lt;/span&gt; &lt;span style="color:#0000ff"&gt;double&lt;/span&gt; SumRootN(&lt;span style="color:#0000ff"&gt;int&lt;/span&gt; root)&lt;br /&gt;     {&lt;br /&gt;         &lt;span style="color:#0000ff"&gt;double&lt;/span&gt; result = 0;&lt;br /&gt;         &lt;span style="color:#0000ff"&gt;for&lt;/span&gt; (&lt;span style="color:#0000ff"&gt;int&lt;/span&gt; i = 1; i &amp;lt; 10000000; i++)&lt;br /&gt;         {&lt;br /&gt;             result += &lt;span style="color:#2b91af"&gt;Math&lt;/span&gt;.Exp(&lt;span style="color:#2b91af"&gt;Math&lt;/span&gt;.Log(i) / root);&lt;br /&gt;         }&lt;br /&gt;         &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; result;&lt;br /&gt;     }&lt;br /&gt; &lt;br /&gt;     &lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; start_Click(&lt;span style="color:#0000ff"&gt;object&lt;/span&gt; sender, &lt;span style="color:#2b91af"&gt;RoutedEventArgs&lt;/span&gt; e)&lt;br /&gt;     {&lt;br /&gt;         textBlock1.Text = &lt;span style="color:#a31515"&gt;&amp;quot;&amp;quot;&lt;/span&gt;;&lt;br /&gt;         label1.Content = &lt;span style="color:#a31515"&gt;&amp;quot;Milliseconds: &amp;quot;&lt;/span&gt;;&lt;br /&gt; &lt;br /&gt;         &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; results = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;BlockingCollection&lt;/span&gt;&amp;lt;&lt;span style="color:#0000ff"&gt;double&lt;/span&gt;&amp;gt;();&lt;br /&gt;         &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; watch = &lt;span style="color:#2b91af"&gt;Stopwatch&lt;/span&gt;.StartNew();&lt;br /&gt;         &lt;span style="color:#2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af"&gt;Task&lt;/span&gt;&amp;gt; tasks = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af"&gt;Task&lt;/span&gt;&amp;gt;();&lt;br /&gt; &lt;br /&gt;         &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; consume = &lt;span style="color:#2b91af"&gt;Task&lt;/span&gt;.Factory.StartNew(() =&amp;gt; display(results));&lt;br /&gt; &lt;br /&gt;         &lt;span style="color:#0000ff"&gt;for&lt;/span&gt; (&lt;span style="color:#0000ff"&gt;int&lt;/span&gt; i = 2; i &amp;lt; 20; i++)&lt;br /&gt;         {&lt;br /&gt;             &lt;span style="color:#0000ff"&gt;int&lt;/span&gt; j = i;&lt;br /&gt;             &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; compute = &lt;span style="color:#2b91af"&gt;Task&lt;/span&gt;.Factory.StartNew(() =&amp;gt;&lt;br /&gt;             {&lt;br /&gt;                 results.Add(SumRootN(j));&lt;br /&gt;             });&lt;br /&gt;             tasks.Add(compute);&lt;br /&gt;         }&lt;br /&gt; &lt;br /&gt;         &lt;span style="color:#2b91af"&gt;Task&lt;/span&gt;.Factory.ContinueWhenAll(tasks.ToArray(),&lt;br /&gt;             result =&amp;gt;&lt;br /&gt;             {&lt;br /&gt;                 results.CompleteAdding();&lt;br /&gt;                 &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; time = watch.ElapsedMilliseconds;&lt;br /&gt;                 label1.Content += time.ToString();&lt;br /&gt;             }, &lt;span style="color:#2b91af"&gt;CancellationToken&lt;/span&gt;.None, &lt;span style="color:#2b91af"&gt;TaskContinuationOptions&lt;/span&gt;.None, ui);&lt;br /&gt;     }&lt;br /&gt; &lt;br /&gt;     &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; display(&lt;span style="color:#2b91af"&gt;BlockingCollection&lt;/span&gt;&amp;lt;&lt;span style="color:#0000ff"&gt;double&lt;/span&gt;&amp;gt; results)&lt;br /&gt;     {&lt;br /&gt;         &lt;span style="color:#0000ff"&gt;foreach&lt;/span&gt; (&lt;span style="color:#0000ff"&gt;var&lt;/span&gt; item &lt;span style="color:#0000ff"&gt;in&lt;/span&gt; results.GetConsumingEnumerable())&lt;br /&gt;         {&lt;br /&gt;             &lt;span style="color:#0000ff"&gt;double&lt;/span&gt; currentItem = item;&lt;br /&gt;             &lt;span style="color:#2b91af"&gt;Task&lt;/span&gt;.Factory.StartNew(&lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Action&lt;/span&gt;(() =&amp;gt;&lt;br /&gt;                  textBlock1.Text += currentItem.ToString() + &lt;span style="color:#2b91af"&gt;Environment&lt;/span&gt;.NewLine),&lt;br /&gt;             &lt;span style="color:#2b91af"&gt;CancellationToken&lt;/span&gt;.None, &lt;span style="color:#2b91af"&gt;TaskCreationOptions&lt;/span&gt;.None, ui);&lt;br /&gt;         }&lt;br /&gt;     }&lt;br /&gt; }&lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;For now, this is the last post in my parallel programming series. I hope that I’ve provided enough information and examples and bumped into and recovered from enough problems to enable even beginners to continue on their own. (At least I asked fewer questions while writing this post than I did for the first one!)&lt;/p&gt;  &lt;p&gt;If you want to learn more about blocking collections, check out &lt;a href="http://msdn.microsoft.com/en-us/library/dd997371.aspx"&gt;BlockingCollection Overview&lt;/a&gt; in the MSDN Library and &lt;a href="http://blogs.msdn.com/b/pfxteam/archive/2010/04/06/9990420.aspx"&gt;BlockingCollection Extensions&lt;/a&gt; on the Parallel Programming with .NET blog.&lt;/p&gt;  &lt;p&gt;I could not cover all the features provided by the TPL. If you want to see what else is available, here are some links:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/dd460718.aspx"&gt;Data Structures for Parallel Programming&lt;/a&gt;. This MSDN topic lists .NET 4 classes that are useful for parallel programming, such as thread-safe collections, synchronization primitives, and lazy initialization classes. &lt;/li&gt;    &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/dd997425.aspx"&gt;Introduction to PLINQ&lt;/a&gt;. Parallel Language Integrated Query, or PLINQ, enables quick and easy parallelization of LINQ queries. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Two more links that I used a lot in this series are &lt;a href="http://msdn.microsoft.com/en-us/library/dd460693.aspx"&gt;Parallel Programming in the .NET Framework&lt;/a&gt; on MSDN and the &lt;a href="http://blogs.msdn.com/b/pfxteam/"&gt;Parallel Programming with .NET&lt;/a&gt; team blog.&lt;/p&gt;  &lt;p&gt;P.S. &lt;/p&gt;  &lt;p&gt;Thanks to &lt;a href="http://blogs.msdn.com/b/mulambda/"&gt;Dmitry Lomov&lt;/a&gt;, Michael Blome, and Danny Shih for reviewing this and providing helpful comments, to Mick Alberts for editing.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10049130" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/-NET+Framework/">.NET Framework</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/C_2300_+4-0/">C# 4.0</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/C_2300_/">C#</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/WPF/">WPF</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/-NET+Framework+4/">.NET Framework 4</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/Task+Parallel+Library/">Task Parallel Library</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/TPL/">TPL</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/parallel_2D00_programming/">parallel-programming</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/parallel_2D00_processing/">parallel-processing</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/concurrent/">concurrent</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/consumer_2D00_producer+pattern/">consumer-producer pattern</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/blocking+collection/">blocking collection</category></item><item><title>C# 4.0 PowerPoint Presentations</title><link>http://blogs.msdn.com/b/csharpfaq/archive/2010/07/27/c-4-0-powerpoint-presentations.aspx</link><pubDate>Tue, 27 Jul 2010 22:57:49 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10043206</guid><dc:creator>CSharpFAQ</dc:creator><slash:comments>46</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/csharpfaq/rsscomments.aspx?WeblogPostID=10043206</wfw:commentRss><comments>http://blogs.msdn.com/b/csharpfaq/archive/2010/07/27/c-4-0-powerpoint-presentations.aspx#comments</comments><description>&lt;p style="line-height: 13.5pt; background: white;"&gt;&lt;span lang="EN" style="font-family: 'Segoe UI','sans-serif'; color: #333333; font-size: 9pt; mso-ansi-language: EN;"&gt;&lt;a href="http://blogs.msdn.com/b/alexghi/"&gt;Alexandru Ghiondea&lt;/a&gt; from the C# team created an excellent deck of slides that he used at &lt;a href="http://www.msbgregistration.com/"&gt;Microsoft Days 2010&lt;/a&gt; in Bulgaria. The C# team reused the deck several times for User Groups' presentations and talks. If you are planning a talk at your User Group, feel free to download. And even if you are not, it&amp;rsquo;s still might be useful to take a look &amp;ndash; it&amp;rsquo;s a nice overview of the new C# 4.0 and Visual Studio 2010 features. &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="line-height: 13.5pt; background: white;"&gt;&lt;span lang="EN" style="font-family: 'Segoe UI','sans-serif'; color: #333333; font-size: 9pt; mso-ansi-language: EN;"&gt;There are three presentations included: Introduction to C# 4.0, Increased Productivity with Visual C# IDE, and Dynamic Deep Dive.&lt;o:p&gt;&lt;/o:p&gt;&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=10043206" width="1" height="1"&gt;</description><enclosure url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-10-04-32-06/C_2300_-Slide-Decks.zip" length="2694868" type="application/x-zip-compressed" /><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/Visual+Studio+2010/">Visual Studio 2010</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/C_2300_+4-0/">C# 4.0</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/User+Group/">User Group</category></item><item><title>Parallel Programming: Task Cancellation</title><link>http://blogs.msdn.com/b/csharpfaq/archive/2010/07/19/parallel-programming-task-cancellation.aspx</link><pubDate>Mon, 19 Jul 2010 17:40:25 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10039923</guid><dc:creator>Alexandra Rusina</dc:creator><slash:comments>22</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/csharpfaq/rsscomments.aspx?WeblogPostID=10039923</wfw:commentRss><comments>http://blogs.msdn.com/b/csharpfaq/archive/2010/07/19/parallel-programming-task-cancellation.aspx#comments</comments><description>&lt;p&gt;In this post, which is the third one in my &lt;a href="http://blogs.msdn.com/b/csharpfaq/archive/tags/parallel_2d00_programming/"&gt;parallel programming introduction&lt;/a&gt; series, I want to show how you can cancel parallel operations when working with the &lt;a href="http://msdn.microsoft.com/en-us/library/dd460717.aspx"&gt;Task Parallel Library (TPL)&lt;/a&gt;. I’m going to modify the program that I started in the previous posts. By the way, here’s the full list of posts in this series:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://blogs.msdn.com/b/csharpfaq/archive/2010/06/01/parallel-programming-in-net-framework-4-getting-started.aspx"&gt;Getting Started&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.msdn.com/b/csharpfaq/archive/2010/06/18/parallel-programming-task-schedulers-and-synchronization-context.aspx"&gt;Task Schedulers and Synchronization Context&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;Task Cancellation (this post) &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.msdn.com/b/csharpfaq/archive/2010/08/12/blocking-collection-and-the-producer-consumer-problem.aspx"&gt;Blocking Collection and the Producer-Consumer Problem&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;At the end of the last post, I had a small parallel application with responsive UI that could be easily used in both WPF and Windows Forms UI programming models. I’m going to stick with the WPF version and add a &lt;b&gt;Cancel&lt;/b&gt; button to the application.&lt;/p&gt;  &lt;p&gt;This is the code I’m going to work with:&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:dd1dd9df-4581-42eb-a110-1215eac16ab4" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background-color: #ffffff; max-height: 400px; overflow: auto; padding: 2px 5px; white-space: nowrap"&gt;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;partial&lt;/span&gt; &lt;span style="color:#0000ff"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af"&gt;MainWindow&lt;/span&gt; : &lt;span style="color:#2b91af"&gt;Window&lt;/span&gt;&lt;br /&gt; {&lt;br /&gt;     &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; MainWindow()&lt;br /&gt;     {&lt;br /&gt;         InitializeComponent();&lt;br /&gt;     }&lt;br /&gt;     &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;static&lt;/span&gt; &lt;span style="color:#0000ff"&gt;double&lt;/span&gt; SumRootN(&lt;span style="color:#0000ff"&gt;int&lt;/span&gt; root)&lt;br /&gt;     {&lt;br /&gt;         &lt;span style="color:#0000ff"&gt;double&lt;/span&gt; result = 0;&lt;br /&gt;         &lt;span style="color:#0000ff"&gt;for&lt;/span&gt; (&lt;span style="color:#0000ff"&gt;int&lt;/span&gt; i = 1; i &amp;lt; 10000000; i++)&lt;br /&gt;         {&lt;br /&gt;             result += &lt;span style="color:#2b91af"&gt;Math&lt;/span&gt;.Exp(&lt;span style="color:#2b91af"&gt;Math&lt;/span&gt;.Log(i) / root);&lt;br /&gt;         }&lt;br /&gt;         &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; result;&lt;br /&gt;     }&lt;br /&gt;     &lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; start_Click(&lt;span style="color:#0000ff"&gt;object&lt;/span&gt; sender, &lt;span style="color:#2b91af"&gt;RoutedEventArgs&lt;/span&gt; e)&lt;br /&gt;     {&lt;br /&gt;         textBlock1.Text = &lt;span style="color:#a31515"&gt;&amp;quot;&amp;quot;&lt;/span&gt;;&lt;br /&gt;         label1.Content = &lt;span style="color:#a31515"&gt;&amp;quot;Milliseconds: &amp;quot;&lt;/span&gt;;&lt;br /&gt; &lt;br /&gt;         &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; watch = &lt;span style="color:#2b91af"&gt;Stopwatch&lt;/span&gt;.StartNew();&lt;br /&gt;         &lt;span style="color:#2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af"&gt;Task&lt;/span&gt;&amp;gt; tasks = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af"&gt;Task&lt;/span&gt;&amp;gt;();&lt;br /&gt;         &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; ui = &lt;span style="color:#2b91af"&gt;TaskScheduler&lt;/span&gt;.FromCurrentSynchronizationContext();&lt;br /&gt;         &lt;span style="color:#0000ff"&gt;for&lt;/span&gt; (&lt;span style="color:#0000ff"&gt;int&lt;/span&gt; i = 2; i &amp;lt; 20; i++)&lt;br /&gt;         {&lt;br /&gt;             &lt;span style="color:#0000ff"&gt;int&lt;/span&gt; j = i;&lt;br /&gt;             &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; compute = &lt;span style="color:#2b91af"&gt;Task&lt;/span&gt;.Factory.StartNew(() =&amp;gt;&lt;br /&gt;             {&lt;br /&gt;                 &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; SumRootN(j);&lt;br /&gt;             });&lt;br /&gt;             tasks.Add(compute);&lt;br /&gt; &lt;br /&gt;             &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; display = compute.ContinueWith(resultTask =&amp;gt;&lt;br /&gt;                               textBlock1.Text += &lt;span style="color:#a31515"&gt;&amp;quot;root &amp;quot;&lt;/span&gt; + j.ToString() + &lt;span style="color:#a31515"&gt;&amp;quot; &amp;quot;&lt;/span&gt; +&lt;br /&gt;                                                   compute.Result.ToString() +&lt;br /&gt;                                                   &lt;span style="color:#2b91af"&gt;Environment&lt;/span&gt;.NewLine,&lt;br /&gt;                               ui);&lt;br /&gt;         }&lt;br /&gt; &lt;br /&gt;         &lt;span style="color:#2b91af"&gt;Task&lt;/span&gt;.Factory.ContinueWhenAll(tasks.ToArray(),&lt;br /&gt;             result =&amp;gt;&lt;br /&gt;             {&lt;br /&gt;                 &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; time = watch.ElapsedMilliseconds;&lt;br /&gt;                 label1.Content += time.ToString();&lt;br /&gt;             }, &lt;span style="color:#2b91af"&gt;CancellationToken&lt;/span&gt;.None, &lt;span style="color:#2b91af"&gt;TaskContinuationOptions&lt;/span&gt;.None, ui);&lt;br /&gt;     }&lt;br /&gt; &lt;br /&gt;     &lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; cancel_Click(&lt;span style="color:#0000ff"&gt;object&lt;/span&gt; sender, &lt;span style="color:#2b91af"&gt;RoutedEventArgs&lt;/span&gt; e)&lt;br /&gt;     {&lt;br /&gt; &lt;br /&gt;     }&lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;Now let’s refer to the &lt;a href="http://msdn.microsoft.com/en-us/library/dd997396.aspx"&gt;Task Cancellation&lt;/a&gt; topic on MSDN. It tells me that to cancel a task I need to pass a special cancellation token to the task factory. By the way, the cancellation model is one of the new features in .NET Framework 4, so if you haven’t heard about it yet, take a look at &lt;a href="http://msdn.microsoft.com/en-us/library/dd997364.aspx"&gt;Cancellation&lt;/a&gt; on MSDN and the &lt;a href="http://blogs.msdn.com/b/pfxteam/archive/2009/05/22/9635790.aspx"&gt;.NET 4 Cancellation Framework&lt;/a&gt; post from Mike Liddell.&lt;/p&gt;  &lt;p&gt;I have several tasks created in a loop. I’m going to use just one cancellation token so I can cancel them all at once.&lt;/p&gt;  &lt;p&gt;I’ll add the following field to the &lt;b&gt;&lt;font face="Courier New"&gt;MainWindow&lt;/font&gt;&lt;/b&gt; class:&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:d5c90e74-305f-4c40-bbe2-93bf3524d2f4" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background-color: #ffffff; overflow: auto; padding: 2px 5px; white-space: nowrap"&gt;&lt;span style="color:#2b91af"&gt;CancellationTokenSource&lt;/span&gt; tokenSource = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;CancellationTokenSource&lt;/span&gt;();&lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;The tasks I want to cancel are the ones that compute the results (and I don’t want to cancel the tasks that display the results). So here’s the next change. The code&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:a4332892-1a32-4402-9624-60ce6568d256" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background-color: #ffffff; overflow: auto; padding: 2px 5px; white-space: nowrap"&gt;&lt;span style="color:#0000ff"&gt;var&lt;/span&gt; compute = &lt;span style="color:#2b91af"&gt;Task&lt;/span&gt;.Factory.StartNew(() =&amp;gt;&lt;br /&gt; {&lt;br /&gt;     &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; SumRootN(j);&lt;br /&gt; });&lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;becomes&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:776a5615-a512-4fa1-ae56-e96ca09697c8" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background-color: #ffffff; overflow: auto; padding: 2px 5px; white-space: nowrap"&gt;&lt;span style="color:#0000ff"&gt;var&lt;/span&gt; compute = &lt;span style="color:#2b91af"&gt;Task&lt;/span&gt;.Factory.StartNew(() =&amp;gt;&lt;br /&gt; {&lt;br /&gt;     &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; SumRootN(j);&lt;br /&gt; }, tokenSource.Token);&lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;Finally, I’ll add code to the event handler for the &lt;b&gt;Cancel&lt;/b&gt; button. I’ll simply call the &lt;a href="http://msdn.microsoft.com/en-us/library/dd321955.aspx"&gt;Cancel&lt;/a&gt; method for the cancellation token and all tasks created with this token are notified about the cancellation. I also want to print “Cancel” into the text block.&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:e96a2da6-75e2-4b4a-b35f-20bd6c2e93a0" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background-color: #ffffff; overflow: auto; padding: 2px 5px; white-space: nowrap"&gt;&lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; cancel_Click(&lt;span style="color:#0000ff"&gt;object&lt;/span&gt; sender, &lt;span style="color:#2b91af"&gt;RoutedEventArgs&lt;/span&gt; e)&lt;br /&gt; {&lt;br /&gt;     tokenSource.Cancel();&lt;br /&gt;     textBlock1.Text += &lt;span style="color:#a31515"&gt;&amp;quot;Cancel&amp;quot;&lt;/span&gt; + &lt;span style="color:#2b91af"&gt;Environment&lt;/span&gt;.NewLine;&lt;br /&gt; }&lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;Let’s press F5 to compile and run the code. Now I can click &lt;b&gt;Start&lt;/b&gt;, then click &lt;b&gt;Cancel&lt;/b&gt;, and get an exception. (Aren’t you getting used to it?) &lt;a href="http://msdn.microsoft.com/en-us/library/system.aggregateexception.aspx"&gt;AggregateException&lt;/a&gt; is the exception that tasks throw if something goes wrong. True to its name, it aggregates all the task failures into a single exception. &lt;/p&gt;  &lt;p&gt;In my case, the tasks that calculate the results were canceled successfully, but the ones that display the results to the UI failed. This is reasonable: there were no results to display.&lt;/p&gt;  &lt;p&gt;Let’s take a short pause here. Basically, to cancel a task, it’s enough to just pass a cancellation token to the task and then call the &lt;a href="http://msdn.microsoft.com/en-us/library/dd321955.aspx"&gt;Cancel&lt;/a&gt; method on the token, as I did above. But in the real world, even in a small application like this one, you have to deal with the consequences of task cancellation.&lt;/p&gt;  &lt;p&gt;In my example, there are several options for handling this exception. First is the standard &lt;b&gt;try-catch &lt;/b&gt;block. The exception is thrown by the delegate within the &lt;font face="Courier New"&gt;ContinueWith&lt;/font&gt; method, so I need to add the &lt;b&gt;try-catch&lt;/b&gt; block right into the lambda expression. I may get more than one exception aggregated, so I need to iterate through the collection of inner exceptions to see the exceptions’ messages.&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:5d9ab3af-a777-46ce-8e88-45384aa52a74" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background-color: #ffffff; overflow: auto; padding: 2px 5px; white-space: nowrap"&gt;&lt;span style="color:#0000ff"&gt;var&lt;/span&gt; display = compute.ContinueWith(resultTask =&amp;gt;&lt;br /&gt; {&lt;br /&gt;     &lt;span style="color:#0000ff"&gt;try&lt;/span&gt;&lt;br /&gt;     {&lt;br /&gt;         textBlock1.Text += &lt;span style="color:#a31515"&gt;&amp;quot;root &amp;quot;&lt;/span&gt; + j.ToString() + &lt;span style="color:#a31515"&gt;&amp;quot; &amp;quot;&lt;/span&gt; +&lt;br /&gt;                             compute.Result.ToString() +&lt;br /&gt;                             &lt;span style="color:#2b91af"&gt;Environment&lt;/span&gt;.NewLine;&lt;br /&gt;     }&lt;br /&gt;     &lt;span style="color:#0000ff"&gt;catch&lt;/span&gt; (&lt;span style="color:#2b91af"&gt;AggregateException&lt;/span&gt; ae)&lt;br /&gt;     {&lt;br /&gt;         &lt;span style="color:#0000ff"&gt;foreach&lt;/span&gt; (&lt;span style="color:#0000ff"&gt;var&lt;/span&gt; inner &lt;span style="color:#0000ff"&gt;in&lt;/span&gt; ae.InnerExceptions)&lt;br /&gt;             textBlock1.Text += &lt;span style="color:#a31515"&gt;&amp;quot;root &amp;quot;&lt;/span&gt; + j.ToString() + &lt;span style="color:#a31515"&gt;&amp;quot; &amp;quot;&lt;/span&gt;&lt;br /&gt;                 + inner.Message + &lt;span style="color:#2b91af"&gt;Environment&lt;/span&gt;.NewLine;&lt;br /&gt;     }&lt;br /&gt; }, ui);&lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;This works fine. (You can compile and check, if you want to.) But once again, TPL provides a more elegant way of dealing with this type of issue: I can analyze what happened with the &lt;b&gt;compute&lt;/b&gt; task and use its status to decide whether I want to start the &lt;b&gt;display&lt;/b&gt; task.&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:246ef745-3fe5-40b1-a44c-99777c673f2b" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background-color: #ffffff; overflow: auto; padding: 2px 5px; white-space: nowrap"&gt;&lt;span style="color:#0000ff"&gt;var&lt;/span&gt; displayResults = compute.ContinueWith(resultTask =&amp;gt;&lt;br /&gt;                      textBlock1.Text += &lt;span style="color:#a31515"&gt;&amp;quot;root &amp;quot;&lt;/span&gt; + j.ToString() + &lt;span style="color:#a31515"&gt;&amp;quot; &amp;quot;&lt;/span&gt; +&lt;br /&gt;                                             compute.Result.ToString() +&lt;br /&gt;                                             &lt;span style="color:#2b91af"&gt;Environment&lt;/span&gt;.NewLine,&lt;br /&gt;                          &lt;span style="color:#2b91af"&gt;CancellationToken&lt;/span&gt;.None,&lt;br /&gt;                          &lt;span style="color:#2b91af"&gt;TaskContinuationOptions&lt;/span&gt;.OnlyOnRanToCompletion,&lt;br /&gt;                          ui);&lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;Now I’m passing two more parameters to the &lt;font face="Courier New"&gt;ContinueWith&lt;/font&gt; method. The first is the cancellation token. I don’t want the &lt;b&gt;display&lt;/b&gt; task to be dependent on the cancellation token, so I’m passing &lt;font face="Courier New"&gt;CancellationToken.None&lt;/font&gt;. But I want this task to run only if the &lt;b&gt;compute&lt;/b&gt; task returns some result. For this purpose I need to choose one of the &lt;a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskcontinuationoptions.aspx"&gt;TaskContinuationOptions&lt;/a&gt;. In my case the best solution is simply not to run the &lt;b&gt;display&lt;/b&gt; task if the &lt;b&gt;compute&lt;/b&gt; task was canceled. I can use the &lt;font face="Courier New"&gt;NotOnCanceled&lt;/font&gt; option that does just that. &lt;/p&gt;  &lt;p&gt;But tasks may fail for some other reason, not just because of cancellation. I don’t want to display results of any failed task, so I’m choosing the &lt;font face="Courier New"&gt;OnlyOnRanToCompletion&lt;/font&gt; option. I’ll explain the “ran to completion” concept more a little bit later in this post.&lt;/p&gt;  &lt;p&gt;However, the previous version with the &lt;b&gt;try-catch&lt;/b&gt; block could also inform me that the tasks were indeed canceled. (Each task either printed the result or reported a cancellation by printing the exception message.) How to do the same in this version? Well, I can create a new task that will run only if the task is canceled. I’m going to convert the &lt;b&gt;display&lt;/b&gt; task into two different tasks:&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:594cf64c-c5c5-4385-a04a-0d8008a6efb7" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background-color: #ffffff; max-height: 300px; overflow: auto; padding: 2px 5px; white-space: nowrap"&gt;&lt;span style="color:#0000ff"&gt;var&lt;/span&gt; displayResults = compute.ContinueWith(resultTask =&amp;gt;&lt;br /&gt;                      textBlock1.Text += &lt;span style="color:#a31515"&gt;&amp;quot;root &amp;quot;&lt;/span&gt; + j.ToString() + &lt;span style="color:#a31515"&gt;&amp;quot; &amp;quot;&lt;/span&gt; +&lt;br /&gt;                                             compute.Result.ToString() +&lt;br /&gt;                                             &lt;span style="color:#2b91af"&gt;Environment&lt;/span&gt;.NewLine,&lt;br /&gt;                          &lt;span style="color:#2b91af"&gt;CancellationToken&lt;/span&gt;.None,&lt;br /&gt;                          &lt;span style="color:#2b91af"&gt;TaskContinuationOptions&lt;/span&gt;.OnlyOnRanToCompletion,&lt;br /&gt;                          ui);&lt;br /&gt; &lt;br /&gt; &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; displayCancelledTasks = compute.ContinueWith(resultTask =&amp;gt;&lt;br /&gt;                                textBlock1.Text += &lt;span style="color:#a31515"&gt;&amp;quot;root &amp;quot;&lt;/span&gt; + j.ToString() +&lt;br /&gt;                                                   &lt;span style="color:#a31515"&gt;&amp;quot; canceled&amp;quot;&lt;/span&gt; +&lt;br /&gt;                                                   &lt;span style="color:#2b91af"&gt;Environment&lt;/span&gt;.NewLine,&lt;br /&gt;                                &lt;span style="color:#2b91af"&gt;CancellationToken&lt;/span&gt;.None,&lt;br /&gt;                                &lt;span style="color:#2b91af"&gt;TaskContinuationOptions&lt;/span&gt;.OnlyOnCanceled, ui);&lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;Now I get the same results as I did with the &lt;b&gt;try-catch&lt;/b&gt; block, and I don’t have to deal with exceptions at all. Let me emphasize that this is a more natural way of working with TPL and tasks, and I’d recommend that you to use this approach whenever possible.&lt;/p&gt;  &lt;p&gt;By the way, if you click the &lt;b&gt;Cancel&lt;/b&gt; button and then click the &lt;b&gt;Start&lt;/b&gt; button in my application, all you will see is a list of canceled tasks and no results at all. The cancellation token got into the canceled state, and there is no way to turn it back to “not canceled.” You have to create a new token each time. But in my case it’s easy. I simply add the following line at the beginning of the event handler for the &lt;b&gt;Start&lt;/b&gt; button:&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:6dc64fcf-75eb-4da7-adbe-44c7f9e1540b" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background-color: #ffffff; overflow: auto; padding: 2px 5px; white-space: nowrap"&gt;tokenSource = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;CancellationTokenSource&lt;/span&gt;();&lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;Now let’s take a look at the output of my little program. (I clicked &lt;b&gt;Cancel&lt;/b&gt; right after I saw the results of the 5&lt;sup&gt;th&lt;/sup&gt; root.)&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/CommunityServer-Blogs-Components-WeblogFiles/00-00-00-33-48-metablogapi/4628.tasks_5F00_25A2A1F5.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="tasks" border="0" alt="tasks" src="http://blogs.msdn.com/cfs-file.ashx/__key/CommunityServer-Blogs-Components-WeblogFiles/00-00-00-33-48-metablogapi/3073.tasks_5F00_thumb_5F00_5DE1190D.png" width="246" height="457" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;You can see that after I canceled the operation some roots were calculated nonetheless. This is a good illustration of the task cancellation concept in .NET. After I called the &lt;b&gt;Cancel&lt;/b&gt; method for the task cancellation token, tasks that were already running switched into the “run to completion” mode, so I got the results even after I canceled the tasks. The tasks that were still waiting in the queue were indeed canceled.&lt;/p&gt;  &lt;p&gt;What if I don’t want to waste resources and want to stop all computations immediately after I click &lt;b&gt;Cancel&lt;/b&gt;? In this case, I have to periodically check for the status of the cancellation token somewhere within the method that performs the long-running operation. Since I declared the cancellation token as a field, I can simply use it within the method.&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:3ad63adb-2f62-4308-a912-9898cf980bb5" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background-color: #ffffff; overflow: auto; padding: 2px 5px; white-space: nowrap"&gt;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;double&lt;/span&gt; SumRootN(&lt;span style="color:#0000ff"&gt;int&lt;/span&gt; root)&lt;br /&gt; {&lt;br /&gt;     &lt;span style="color:#0000ff"&gt;double&lt;/span&gt; result = 0;&lt;br /&gt;     &lt;span style="color:#0000ff"&gt;for&lt;/span&gt; (&lt;span style="color:#0000ff"&gt;int&lt;/span&gt; i = 1; i &amp;lt; 10000000; i++)&lt;br /&gt;     {&lt;br /&gt;         tokenSource.Token.ThrowIfCancellationRequested();&lt;br /&gt;         result += &lt;span style="color:#2b91af"&gt;Math&lt;/span&gt;.Exp(&lt;span style="color:#2b91af"&gt;Math&lt;/span&gt;.Log(i) / root);&lt;br /&gt;     }&lt;br /&gt;     &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; result;&lt;br /&gt; }&lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;I made two changes: I removed the &lt;b&gt;static&lt;/b&gt; keyword from the method declaration to enable field access, and I added a line that checks for the status of the cancellation token. The &lt;a href="http://msdn.microsoft.com/en-us/library/system.threading.cancellationtoken.throwifcancellationrequested.aspx?queryresult=true"&gt;ThrowIfCancellationRequested&lt;/a&gt; method indicates so-called “cooperative cancellation,” which means that the task throws an exception to show that it accepted the cancellation request and will stop working.&lt;/p&gt;  &lt;p&gt;In this case, the thrown exception is handled by the TPL, which transitions the task to the canceled state. You cannot and should not handle this exception in your code. However, Visual Studio checks for all unhandled exceptions and shows them when in debug mode. So, if you now press F5, you’re going to see this exception. Basically, you need to ignore it: You can simply press F5 several times to continue or run the program by using Ctrl+F5 to avoid debug mode. &lt;/p&gt;  &lt;p&gt;Another possibility is to switch off the checking of these “unhandled by the user code” exceptions in Visual Studio: Go to &lt;b&gt;Tools&lt;/b&gt; -&amp;gt; &lt;b&gt;Options&lt;/b&gt; -&amp;gt; &lt;b&gt;Debugging&lt;/b&gt; -&amp;gt; &lt;b&gt;General&lt;/b&gt;, and clear the &lt;b&gt;Just My Code&lt;/b&gt; check box. This makes Visual Studio “swallow” this exception. However, this may cause side effects in your debugging routine, so check the &lt;a href="http://msdn.microsoft.com/en-us/library/h5e30exc.aspx"&gt;MSDN documentation&lt;/a&gt; to make sure it’s the right option for you.&lt;/p&gt;  &lt;p&gt;Well, that’s all what I wanted to show you this time. Here is the final code of my still surprisingly small program.&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:d24922a4-27ef-4ee3-a794-85d8eb0d7b7d" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background-color: #ffffff; max-height: 400px; overflow: auto; padding: 2px 5px; white-space: nowrap"&gt;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;partial&lt;/span&gt; &lt;span style="color:#0000ff"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af"&gt;MainWindow&lt;/span&gt; : &lt;span style="color:#2b91af"&gt;Window&lt;/span&gt;&lt;br /&gt; {&lt;br /&gt;     &lt;span style="color:#2b91af"&gt;CancellationTokenSource&lt;/span&gt; tokenSource = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;CancellationTokenSource&lt;/span&gt;();&lt;br /&gt;     &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; MainWindow()&lt;br /&gt;     {&lt;br /&gt;         InitializeComponent();&lt;br /&gt;     }&lt;br /&gt;     &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;double&lt;/span&gt; SumRootN(&lt;span style="color:#0000ff"&gt;int&lt;/span&gt; root)&lt;br /&gt;     {&lt;br /&gt;         &lt;span style="color:#0000ff"&gt;double&lt;/span&gt; result = 0;&lt;br /&gt;         &lt;span style="color:#0000ff"&gt;for&lt;/span&gt; (&lt;span style="color:#0000ff"&gt;int&lt;/span&gt; i = 1; i &amp;lt; 10000000; i++)&lt;br /&gt;         {&lt;br /&gt;             tokenSource.Token.ThrowIfCancellationRequested();&lt;br /&gt;             result += &lt;span style="color:#2b91af"&gt;Math&lt;/span&gt;.Exp(&lt;span style="color:#2b91af"&gt;Math&lt;/span&gt;.Log(i) / root);&lt;br /&gt;         }&lt;br /&gt;         &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; result;&lt;br /&gt;     }&lt;br /&gt; &lt;br /&gt;     &lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; start_Click(&lt;span style="color:#0000ff"&gt;object&lt;/span&gt; sender, &lt;span style="color:#2b91af"&gt;RoutedEventArgs&lt;/span&gt; e)&lt;br /&gt;     {&lt;br /&gt;         tokenSource = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;CancellationTokenSource&lt;/span&gt;();&lt;br /&gt;         &lt;br /&gt;         textBlock1.Text = &lt;span style="color:#a31515"&gt;&amp;quot;&amp;quot;&lt;/span&gt;;&lt;br /&gt;         label1.Content = &lt;span style="color:#a31515"&gt;&amp;quot;Milliseconds: &amp;quot;&lt;/span&gt;;&lt;br /&gt; &lt;br /&gt;         &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; watch = &lt;span style="color:#2b91af"&gt;Stopwatch&lt;/span&gt;.StartNew();&lt;br /&gt;         &lt;span style="color:#2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af"&gt;Task&lt;/span&gt;&amp;gt; tasks = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af"&gt;Task&lt;/span&gt;&amp;gt;();&lt;br /&gt;         &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; ui = &lt;span style="color:#2b91af"&gt;TaskScheduler&lt;/span&gt;.FromCurrentSynchronizationContext();&lt;br /&gt;         &lt;span style="color:#0000ff"&gt;for&lt;/span&gt; (&lt;span style="color:#0000ff"&gt;int&lt;/span&gt; i = 2; i &amp;lt; 20; i++)&lt;br /&gt;         {&lt;br /&gt;             &lt;span style="color:#0000ff"&gt;int&lt;/span&gt; j = i;&lt;br /&gt;             &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; compute = &lt;span style="color:#2b91af"&gt;Task&lt;/span&gt;.Factory.StartNew(() =&amp;gt;&lt;br /&gt;             {&lt;br /&gt;                 &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; SumRootN(j);&lt;br /&gt;             }, tokenSource.Token);&lt;br /&gt; &lt;br /&gt;             tasks.Add(compute);&lt;br /&gt; &lt;br /&gt;             &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; displayResults = compute.ContinueWith(resultTask =&amp;gt;&lt;br /&gt;                                  textBlock1.Text += &lt;span style="color:#a31515"&gt;&amp;quot;root &amp;quot;&lt;/span&gt; + j.ToString() + &lt;span style="color:#a31515"&gt;&amp;quot; &amp;quot;&lt;/span&gt; +&lt;br /&gt;                                                         compute.Result.ToString() +&lt;br /&gt;                                                         &lt;span style="color:#2b91af"&gt;Environment&lt;/span&gt;.NewLine,&lt;br /&gt;                                      &lt;span style="color:#2b91af"&gt;CancellationToken&lt;/span&gt;.None,&lt;br /&gt;                                      &lt;span style="color:#2b91af"&gt;TaskContinuationOptions&lt;/span&gt;.OnlyOnRanToCompletion,&lt;br /&gt;                                      ui);&lt;br /&gt; &lt;br /&gt;             &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; displayCancelledTasks = compute.ContinueWith(resultTask =&amp;gt;&lt;br /&gt;                                            textBlock1.Text += &lt;span style="color:#a31515"&gt;&amp;quot;root &amp;quot;&lt;/span&gt; + j.ToString() +&lt;br /&gt;                                                               &lt;span style="color:#a31515"&gt;&amp;quot; canceled&amp;quot;&lt;/span&gt; +&lt;br /&gt;                                                               &lt;span style="color:#2b91af"&gt;Environment&lt;/span&gt;.NewLine,&lt;br /&gt;                                            &lt;span style="color:#2b91af"&gt;CancellationToken&lt;/span&gt;.None,&lt;br /&gt;                                            &lt;span style="color:#2b91af"&gt;TaskContinuationOptions&lt;/span&gt;.OnlyOnCanceled, ui);&lt;br /&gt;         }&lt;br /&gt; &lt;br /&gt;         &lt;span style="color:#2b91af"&gt;Task&lt;/span&gt;.Factory.ContinueWhenAll(tasks.ToArray(),&lt;br /&gt;             result =&amp;gt;&lt;br /&gt;             {&lt;br /&gt;                 &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; time = watch.ElapsedMilliseconds;&lt;br /&gt;                 label1.Content += time.ToString();&lt;br /&gt;             }, &lt;span style="color:#2b91af"&gt;CancellationToken&lt;/span&gt;.None, &lt;span style="color:#2b91af"&gt;TaskContinuationOptions&lt;/span&gt;.None, ui);&lt;br /&gt;     }&lt;br /&gt; &lt;br /&gt;     &lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; cancel_Click(&lt;span style="color:#0000ff"&gt;object&lt;/span&gt; sender, &lt;span style="color:#2b91af"&gt;RoutedEventArgs&lt;/span&gt; e)&lt;br /&gt;     {&lt;br /&gt;         tokenSource.Cancel();&lt;br /&gt;         textBlock1.Text += &lt;span style="color:#a31515"&gt;&amp;quot;Cancel&amp;quot;&lt;/span&gt; + &lt;span style="color:#2b91af"&gt;Environment&lt;/span&gt;.NewLine;&lt;br /&gt;     }&lt;br /&gt; &lt;br /&gt; }&lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;As usual, here are some links for further reading if you want to know more about task cancellation:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Documentation about the new &lt;a href="http://msdn.microsoft.com/en-us/library/dd997364.aspx"&gt;.NET 4 cancellation model&lt;/a&gt; in general and &lt;a href="http://msdn.microsoft.com/en-us/library/dd997396.aspx"&gt;task cancellation&lt;/a&gt; in particular on MSDN. &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.msdn.com/b/pfxteam/archive/2009/05/22/9635790.aspx"&gt;The .NET 4 Cancellation Framework&lt;/a&gt; and &lt;a href="http://blogs.msdn.com/b/pfxteam/archive/2009/06/22/9791840.aspx"&gt;Cancellation in Parallel Extensions&lt;/a&gt; from the Parallel Programming with .NET blog. &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.msdn.com/b/pfxteam/archive/2010/01/11/9946736.aspx"&gt;The Debugger does not correctly handle Task exceptions?&lt;/a&gt; from the Parallel Programming with .NET blog. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;P.S. &lt;/p&gt;  &lt;p&gt;Thanks to &lt;a href="http://blogs.msdn.com/b/mulambda/"&gt;Dmitry Lomov&lt;/a&gt;, Michael Blome, and Danny Shih for reviewing this and providing helpful comments, to Mick Alberts for editing.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10039923" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/-NET+Framework/">.NET Framework</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/C_2300_+4-0/">C# 4.0</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/C_2300_/">C#</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/WPF/">WPF</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/-NET+Framework+4/">.NET Framework 4</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/Task+Parallel+Library/">Task Parallel Library</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/TPL/">TPL</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/parallel_2D00_programming/">parallel-programming</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/parallel_2D00_processing/">parallel-processing</category><category domain="http://blogs.msdn.com/b/csharpfaq/archive/tags/task+cancellation/">task cancellation</category></item></channel></rss>