<?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>.NET Blog</title><link>http://blogs.msdn.com/b/dotnet/</link><description>The .NET blog discusses new features in the .NET Framework and important issues for .NET developers.</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>Portable HttpClient is now available as RC</title><link>http://blogs.msdn.com/b/dotnet/archive/2013/05/22/portable-httpclient-is-now-available-as-rc.aspx</link><pubDate>Thu, 23 May 2013 00:24:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10419964</guid><dc:creator>Immo Landwerth [MSFT]</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/dotnet/rsscomments.aspx?WeblogPostID=10419964</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/dotnet/commentapi.aspx?WeblogPostID=10419964</wfw:comment><comments>http://blogs.msdn.com/b/dotnet/archive/2013/05/22/portable-httpclient-is-now-available-as-rc.aspx#comments</comments><description>&lt;p&gt;Three months ago &lt;a href="http://blogs.msdn.com/b/bclteam/archive/2013/02/18/portable-httpclient-for-net-framework-and-windows-phone.aspx"&gt;we shipped the first preview of the portable HttpClient&lt;/a&gt;. Many of you wondered when we would ship the RTM version. Today, we&amp;rsquo;re happy to announce the first step towards an RTM: We shipped a release candidate (RC) of HttpClient (&lt;a href="http://nuget.org/packages/Microsoft.Net.Http/"&gt;Microsoft.Net.Http package on NuGet&lt;/a&gt;) that includes all the bug fixes since the preview.&lt;/p&gt;
&lt;h1&gt;New features&lt;/h1&gt;
&lt;p&gt;To address some of the platform differences in a portable fashion, we&amp;rsquo;ve added new&amp;nbsp;capability APIs.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;HttpClientHandler.SupportsUseProxy()&lt;/strong&gt;: The existing &lt;a href="http://msdn.microsoft.com/en-us/library/system.net.http.httpclienthandler.supportsproxy.aspx"&gt;HttpClientHandler.SupportsProxy&lt;/a&gt; property indicates whether both the &lt;a href="http://msdn.microsoft.com/en-us/library/system.net.http.httpclienthandler.useproxy.aspx"&gt;UseProxy&lt;/a&gt; property and the &lt;a href="http://msdn.microsoft.com/en-us/library/system.net.http.httpclienthandler.proxy.aspx"&gt;Proxy&lt;/a&gt; property are supported. This created an issue because some platforms (for example, Windows Phone) don&amp;rsquo;t allow you to configure a proxy explicitly. However, Windows Phone lets you control whether the machine wide proxy should be used. To model this behavior, we added the &lt;strong&gt;HttpClientHandler.SupportsUseProxy()&lt;/strong&gt; extension method. For some platforms that don&amp;rsquo;t support both, such as Windows Phone, the &lt;strong&gt;SupportsProxy&lt;/strong&gt; property will continue to return false, but the &lt;strong&gt;SupportsUseProxy()&lt;/strong&gt; method will return true.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;HttpClientHandler.SupportsAllowAutoRedirect()&lt;/strong&gt;: The &lt;a href="http://msdn.microsoft.com/en-us/library/system.net.http.httpclienthandler.supportsredirectconfiguration.aspx"&gt;HttpClientHandler.SupportsRedirectConfiguration&lt;/a&gt; property had a similar issue: It controls whether both the &lt;a href="http://msdn.microsoft.com/en-us/library/system.net.http.httpclienthandler.allowautoredirect.aspx"&gt;AllowAutoRedirect&lt;/a&gt; and the &lt;a href="http://msdn.microsoft.com/en-us/library/system.net.http.httpclienthandler.maxautomaticredirections.aspx"&gt;MaxAutomaticRedirections&lt;/a&gt; properties are supported. Windows Phone doesn&amp;rsquo;t support specifying the maximum number of automatic redirections, but it does support redirection. For that reason, we added the &lt;strong&gt;HttpClientHandler.SupportsAllowAutoRedirect()&lt;/strong&gt; extension method.&lt;/p&gt;
&lt;p&gt;Here's how you use these capability APIs:&lt;/p&gt;
&lt;pre&gt;HttpClientHandler handler = new HttpClientHandler();

// Configure proxy (if supported)
if (handler.SupportsUseProxy())
    handler.UseProxy = true;

// Allow automatic redirection (if supported)
if (handler.SupportsAllowAutoRedirect())
    handler.AllowAutoRedirect = true;&lt;/pre&gt;
&lt;p&gt;In case you are wondering why we added extension methods instead of regular properties: Some of the platforms that &lt;strong&gt;Microsoft.Net.Http&lt;/strong&gt; supports already provide the &lt;strong&gt;HttpClientHandler&lt;/strong&gt; class, which is used on those platforms. Since we can&amp;rsquo;t modify the built-in version of the properties directly, we added extension methods that ship in a separate assembly with the NuGet package.&lt;/p&gt;
&lt;h1&gt;Bug fixes&lt;/h1&gt;
&lt;p&gt;All bug fixes are listed in the &lt;a href="http://blogs.msdn.com/b/bclteam/p/httpclient.aspx"&gt;release notes&lt;/a&gt;. In this post, I&amp;rsquo;d like to highlight just one:&lt;/p&gt;
&lt;p style="padding-left: 30px;"&gt;&lt;em&gt;Issue: When consuming a portable class library that depends on HttpClient from a Windows Store application, the app can crash with a MissingMethodException.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;We&amp;rsquo;ve fixed this bug by using the technique we explained in our &lt;a href="http://blogs.msdn.com/b/bclteam/archive/2013/04/17/microsoft-bcl-async-is-now-stable.aspx"&gt;blog post about Microsoft.Bcl.Async&lt;/a&gt;: We ensure during the build that the application has a reference to the NuGet package.&lt;/p&gt;
&lt;h1&gt;Future directions&lt;/h1&gt;
&lt;p&gt;This RC doesn&amp;rsquo;t include support for the &lt;a href="http://msdn.microsoft.com/en-us/library/system.net.http.httpclienthandler.automaticdecompression.aspx"&gt;HttpClientHandler.AutomaticDecompression&lt;/a&gt; property. Many of you have asked for this support, and we certainly haven&amp;rsquo;t ignored it or forgotten about it. In fact, &lt;a href="http://social.msdn.microsoft.com/Forums/en-US/wptools/thread/1fa78fca-5aae-436b-a9c0-0bafa5b78b14/#5667f6af-21c5-4224-b2c3-400729bfbf14"&gt;our original plan was&lt;/a&gt; to ship another preview with support for automatic decompression, and then ship an RTM version a few weeks later. So why did we change our minds about it?&lt;/p&gt;
&lt;p&gt;We had two options:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Block the release of HttpClient until automatic decompression is ready.&lt;/li&gt;
&lt;li&gt;Ship an interim release of HttpClient that doesn&amp;rsquo;t have automatic decompression but is marked as a stable NuGet release and comes with a license that allows usage in production.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;We decided to go with the second option, because we believe it delivers functionality sooner to our customers. After all, that's why we ship on NuGet, and we hope you agree.&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s recap why our team is investing so much in releasing components via NuGet. The reason is twofold:&lt;/p&gt;
&lt;p&gt;First, it allows bridging differences in functionality between platforms we already shipped. A good example is HttpClient, and also includes the &lt;a href="http://blogs.msdn.com/b/bclteam/archive/2013/04/17/microsoft-bcl-async-is-now-stable.aspx"&gt;support for the async and await keywords&lt;/a&gt;. Shipping features out of band also enables us to ship new functionality for multiple platforms via a single portable class library, without having to wait for any of the platforms to add that functionality.&lt;/p&gt;
&lt;p&gt;Second, our goal is to strengthen the feedback loop with you, our customers. In the past, we&amp;rsquo;ve shipped &amp;ldquo;big&amp;rdquo; betas, like a beta of the entire .NET Framework. This approach certainly has some advantages, but we&amp;rsquo;ve seen issues with it as well. The biggest downside is that &amp;ldquo;big&amp;rdquo; betas are pretty expensive to ship and are typically very close to RTM, which, in turn, means that we can&amp;rsquo;t make impactful changes anymore. In fact, we have to turn down a large number of the bug reports we get in &amp;ldquo;big&amp;rdquo; betas, either because they affect a relatively small number of customers or because fixing the bugs would place the RTM release at risk. We&amp;rsquo;re certainly not the first company running into this problem; there is an entire agile software development movement in our industry that focuses on this. I don&amp;rsquo;t want to go into a philosophical discussion about agile methodologies, but it&amp;rsquo;s pretty hard to disagree that shipping early and often helps with feedback loop issues.&lt;/p&gt;
&lt;p&gt;A good example where we use your feedback quite heavily is in the previews of i&lt;a href="http://blogs.msdn.com/b/bclteam/archive/tags/immutable/"&gt;mmutable collections&lt;/a&gt;; in contrast to &amp;ldquo;big&amp;rdquo; betas, we were able to change the design of the APIs when we believe that&amp;rsquo;s the correct approach.&lt;/p&gt;
&lt;p&gt;Unfortunately, shipping a constant stream of previews isn&amp;rsquo;t necessarily a helpful solution in all scenarios. On certain platforms, such as the .NET Framework 4.5 and Windows Store, HttpClient is already available as a fully supported, RTM-quality component. For that reason, we can offer a preview of HttpClient only for Windows Phone today.&lt;/p&gt;
&lt;p&gt;Here are our tentative plans for future NuGet releases of HttpClient:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Today: RC of HttpClient 2.1.&lt;/li&gt;
&lt;li&gt;Wednesday 5/29: RTM of HttpClient 2.1. Yep, it&amp;rsquo;s only a week away now, but it entirely depends on your feedback. If we find substantial issues, we might change the release date.&lt;/li&gt;
&lt;li&gt;Mid-June: Beta of HttpClient 2.2 with automatic decompression&lt;/li&gt;
&lt;li&gt;End of June: RC of HttpClient 2.2 with automatic decompression&lt;/li&gt;
&lt;li&gt;Around July (depending on feedback): RTM of HttpClient 2.2 with automatic decompression&lt;/li&gt;
&lt;/ul&gt;
&lt;h1&gt;Summary&lt;/h1&gt;
&lt;p&gt;We released an RC version of the &lt;a href="http://nuget.org/packages/Microsoft.Net.Http/"&gt;Microsoft.Net.Http&lt;/a&gt; NuGet package. We expect this to be the last preview of HttpClient before we ship an RTM. Please provide feedback if you find any issues.&lt;/p&gt;
&lt;p&gt;Of course, we&amp;rsquo;d also love to hear from you if everything just works. After all, we're happy if you're happy!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10419964" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/dotnet/archive/tags/bcl/">bcl</category><category domain="http://blogs.msdn.com/b/dotnet/archive/tags/portable+class+libraries/">portable class libraries</category><category domain="http://blogs.msdn.com/b/dotnet/archive/tags/nuget/">nuget</category></item><item><title>.NET Crash Dump and Live Process Inspection</title><link>http://blogs.msdn.com/b/dotnet/archive/2013/05/01/net-crash-dump-and-live-process-inspection.aspx</link><pubDate>Thu, 02 May 2013 04:44:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10415493</guid><dc:creator>Immo Landwerth [MSFT]</dc:creator><slash:comments>19</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/dotnet/rsscomments.aspx?WeblogPostID=10415493</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/dotnet/commentapi.aspx?WeblogPostID=10415493</wfw:comment><comments>http://blogs.msdn.com/b/dotnet/archive/2013/05/01/net-crash-dump-and-live-process-inspection.aspx#comments</comments><description>&lt;p&gt;&lt;em&gt;Analyzing crash dumps can be complicated. Although Visual Studio supports viewing managed crash dumps, you often have to resort to more specialized tools like the SOS debugging extensions or WinDbg. In today&amp;rsquo;s post, &lt;strong&gt;Lee Culver, software developer on the .NET Runtime team&lt;/strong&gt;, will introduce you to a new managed library that allows you to automate inspection tasks and access even more debugging information. --Immo&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Today are we excited to announce the beta release of the &lt;a href="https://nuget.org/packages/Microsoft.Diagnostics.Runtime"&gt;Microsoft.Diagnostics.Runtime component&lt;/a&gt; (called ClrMD for short) through the NuGet Package Manager.&lt;/p&gt;
&lt;p&gt;ClrMD is a set of advanced APIs for programmatically inspecting a crash dump of a .NET program much in the same way as the &lt;a href="http://msdn.microsoft.com/en-us/library/bb190764.aspx"&gt;SOS Debugging Extensions&lt;/a&gt; (SOS). It allows you to write automated crash analysis for your applications and automate many common debugger tasks.&lt;/p&gt;
&lt;p&gt;We understand that this API won&amp;rsquo;t be for everyone -- hopefully debugging .NET crash dumps is a rare thing for you. However, our .NET Runtime team has had so much success automating complex diagnostics tasks with this API that we wanted to release it publicly.&lt;/p&gt;
&lt;p&gt;One last, quick note, before we get started: The ClrMD managed library is a wrapper around CLR internal-only debugging APIs. Although those internal-only APIs are very useful for diagnostics, we do not support them as a public, documented release because they are incredibly difficult to use and tightly coupled with other implementation details of the CLR. ClrMD addresses this problem by providing an easy-to-use managed wrapper around these low-level debugging APIs.&lt;/p&gt;
&lt;h1&gt;Getting Started&lt;/h1&gt;
&lt;p&gt;Let's dive right into an example of what can be done with ClrMD. The API was designed to be as discoverable as possible, so IntelliSense will be your primary guide. As an initial example, we will show you how to collect a set of heap statistics (objects, sizes, and counts) similar to what SOS reports when you run the command &lt;strong&gt;!dumpheap &amp;ndash;stat&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;The &amp;ldquo;root&amp;rdquo; object of ClrMD to start with is the &lt;strong&gt;DataTarget&lt;/strong&gt; class. A DataTarget represents either a crash dump or a live .NET process. In this example, we will attach to a live process that has the name &amp;ldquo;HelloWorld.exe&amp;rdquo; with a timeout of 5 seconds to attempt to attach:&lt;/p&gt;
&lt;pre&gt;        int pid = Process.GetProcessesByName("HelloWorld")[0].Id;
        using (DataTarget dataTarget = DataTarget.AttachToProcess(pid, 5000))
        {
            string dacLocation = dataTarget.ClrVersions[0].TryGetDacLocation();
            ClrRuntime runtime = dataTarget.CreateRuntime(dacLocation);

            // ...
        }    &lt;/pre&gt;
&lt;p&gt;You may wonder what the &lt;strong&gt;TryGetDacLocation&lt;/strong&gt; method does. The CLR is a managed runtime, which means that it provides additional abstractions, such as garbage collection and JIT compilation, over what the operating system provides. The bookkeeping for those abstractions is done via internal data structures that live within the process. Those data structures are specific to the CPU architecture and the CLR version. In order to decouple debuggers from the internal data structures, the CLR provides a data access component (DAC), implemented in mscordacwks.dll. The DAC has a standardized interface and is used by the debugger to obtain information about the state of those abstractions, for example, the managed heap. It is essential to use the DAC that matches the CLR version and the architecture of the process or crash dump you want to inspect. For a given CLR version, the &lt;strong&gt;TryGetDacLocation&lt;/strong&gt; method tries to find a matching DAC on the same machine. If you need to inspect a process for which you do not have a matching CLR installed, you have another option: you can copy the DAC from a machine that has that version of the CLR installed. In that case, you provide the path to the alternate mscordacwks.dll to the &lt;strong&gt;CreateRuntime&lt;/strong&gt; method manually. You can read more about the DAC &lt;a href="http://msdn.microsoft.com/en-us/library/windows/hardware/ff540665.aspx"&gt;on MSDN&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Note that the DAC is a native DLL and must be loaded into the program that uses ClrMD. If the dump or the live process is 32-bit, you must use the 32-bit version of the DAC, which, in turn, means that your inspection program needs to be 32-bit as well. The same is true for 64-bit processes. Make sure that your program&amp;rsquo;s platform matches what you are debugging.&lt;/p&gt;
&lt;h1&gt;Analyzing the Heap&lt;/h1&gt;
&lt;p&gt;Once you have attached to the process, you can use the &lt;strong&gt;runtime&lt;/strong&gt; object to inspect the contents of the GC heap:&lt;/p&gt;
&lt;pre&gt;        ClrHeap heap = runtime.GetHeap();
        foreach (ulong obj in heap.EnumerateObjects())
        {
            ClrType type = heap.GetObjectType(obj);
            ulong size = type.GetSize(obj);
            Console.WriteLine("{0,12:X} {1,8:n0} {2}", obj, size, type.Name);
        }
    &lt;/pre&gt;
&lt;p&gt;This produces output similar to the following:&lt;/p&gt;
&lt;pre&gt;         23B1D30       36 System.Security.PermissionSet
         23B1D54       20 Microsoft.Win32.SafeHandles.SafePEFileHandle
         23B1D68       32 System.Security.Policy.PEFileEvidenceFactory
         23B1D88       40 System.Security.Policy.Evidence
    &lt;/pre&gt;
&lt;p&gt;However, the original goal was to output a set of heap statistics. Using the data above, you can use a LINQ query to group the heap by type and sort by total object size:&lt;/p&gt;
&lt;pre&gt;        var stats = from o in heap.EnumerateObjects()
                    let t = heap.GetObjectType(o)
                    group o by t into g
                    let size = g.Sum(o =&amp;gt; (uint)g.Key.GetSize(o))
                    orderby size
                    select new
                    {
                        Name = g.Key.Name,
                        Size = size,
                        Count = g.Count()
                    };

        foreach (var item in stats)
            Console.WriteLine("{0,12:n0} {1,12:n0} {2}", item.Size, item.Count, item.Name);
    &lt;/pre&gt;
&lt;p&gt;This will output data like the following -- a collection of statistics about what objects are taking up the most space on the GC heap for your process:&lt;/p&gt;
&lt;pre&gt;           564           11 System.Int32[]
           616            2 System.Globalization.CultureData
           680           18 System.String[]
           728           26 System.RuntimeType
           790            7 System.Char[]
         5,788          165 System.String
        17,252            6 System.Object[]
    &lt;/pre&gt;
&lt;h1&gt;ClrMD Features and Functionality&lt;/h1&gt;
&lt;p&gt;Of course, there&amp;rsquo;s a lot more to this API than simply printing out heap statistics. You can also walk every managed thread in a process or crash dump and print out a managed callstack. For example, this code prints the managed stack trace for each thread, similar to what the SOS &lt;strong&gt;!clrstack&lt;/strong&gt; command would report (and similar to the output in the Visual Studio stack trace window):&lt;/p&gt;
&lt;pre&gt;        foreach (ClrThread thread in runtime.Threads)
        {
            Console.WriteLine("ThreadID: {0:X}", thread.OSThreadId);
            Console.WriteLine("Callstack:");

            foreach (ClrStackFrame frame in thread.StackTrace)
                Console.WriteLine("{0,12:X} {1,12:X} {2}", frame.InstructionPointer, frame.StackPointer, frame.DisplayString);

            Console.WriteLine();
        }
    &lt;/pre&gt;
&lt;p&gt;This produces output similar to the following:&lt;/p&gt;
&lt;pre&gt;        ThreadID: 2D90
        Callstack:
                   0       90F168 HelperMethodFrame
            660E3365       90F1DC System.Threading.Thread.Sleep(Int32)
              C70089       90F1E0 HelloWorld.Program.Main(System.String[])
                   0       90F36C GCFrame
    &lt;/pre&gt;
&lt;p&gt;Each &lt;strong&gt;ClrThread&lt;/strong&gt; object also contains a &lt;strong&gt;CurrentException&lt;/strong&gt; property, which may be null, but if not, contains the last thrown exception on this thread. This exception object contains the full stack trace, message, and type of the exception thrown.&lt;/p&gt;
&lt;p&gt;ClrMD also provides the following features:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Gets general information about the GC heap:
&lt;ul&gt;
&lt;li&gt;Whether the GC is workstation or server&lt;/li&gt;
&lt;li&gt;The number of logical GC heaps in the process&lt;/li&gt;
&lt;li&gt;Data about the bounds of GC segments&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Walks the CLR&amp;rsquo;s handle table (similar to &lt;strong&gt;!gchandles&lt;/strong&gt; in SOS).&lt;/li&gt;
&lt;li&gt;Walks the application domains in the process and identifies which modules are loaded into them.&lt;/li&gt;
&lt;li&gt;Enumerates threads, callstacks of those threads, the last thrown exception on threads, etc.&lt;/li&gt;
&lt;li&gt;Enumerates the object roots of the process (as the GC sees them for our mark-and-sweep algorithm).&lt;/li&gt;
&lt;li&gt;Walks the fields of objects.&lt;/li&gt;
&lt;li&gt;Gets data about the various heaps that the .NET runtime uses to see where memory is going in the process (see &lt;strong&gt;ClrRuntime.EnumerateMemoryRegions&lt;/strong&gt; in the ClrMD package).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;All of this functionality can generally be found on the &lt;strong&gt;ClrRuntime&lt;/strong&gt; or the &lt;strong&gt;ClrHeap&lt;/strong&gt; objects, as seen above. IntelliSense can help you explore the various properties and functions when you install the ClrMD package. In addition, you can also use the attached sample code.&lt;/p&gt;
&lt;p&gt;Please use the comments under this post to let us know if you have any feedback!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10415493" width="1" height="1"&gt;</description><enclosure url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-10-41-54-93/ClrMDSample_2E00_cs" length="3602" type="application/octet-stream" /><category domain="http://blogs.msdn.com/b/dotnet/archive/tags/clr/">clr</category><category domain="http://blogs.msdn.com/b/dotnet/archive/tags/releases/">releases</category><category domain="http://blogs.msdn.com/b/dotnet/archive/tags/diagnostics/">diagnostics</category></item><item><title>Social Engineering</title><link>http://blogs.msdn.com/b/dotnet/archive/2013/04/24/social-engineering.aspx</link><pubDate>Wed, 24 Apr 2013 18:36:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10413741</guid><dc:creator>Immo Landwerth [MSFT]</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/dotnet/rsscomments.aspx?WeblogPostID=10413741</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/dotnet/commentapi.aspx?WeblogPostID=10413741</wfw:comment><comments>http://blogs.msdn.com/b/dotnet/archive/2013/04/24/social-engineering.aspx#comments</comments><description>&lt;p&gt;When people in our industry use the term &amp;ldquo;social engineering&amp;rdquo; they are usually referring to &lt;a href="http://en.wikipedia.org/wiki/Social_engineering_(security)"&gt;security attacks&lt;/a&gt;. Politicians and lobbyists &lt;a href="http://en.wikipedia.org/wiki/Social_engineering_(political_science)"&gt;would mean something entirely different&lt;/a&gt; too. But that&amp;rsquo;s not what I will talk about here. Instead, I&amp;rsquo;ll talk about the kind of social life we&amp;rsquo;ve around in our team.&lt;/p&gt;
&lt;p&gt;Why am I using the term engineering then? Because I strongly believe that building a great team is a conscious effort. &lt;a href="http://xkcd.com/978/"&gt;Wikipedia defines&lt;/a&gt; the term &lt;a href="http://en.wikipedia.org/wiki/Engineering"&gt;engineering&lt;/a&gt; as follows:&lt;/p&gt;
&lt;p style="margin-left: 40px;"&gt;&lt;em&gt;Engineering is the application of scientific, economic, social, and practical knowledge, in order to design, build, and maintain structures, machines, devices, systems, materials and processes.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;From my point of view, building great software is a lot easier if you have a great team. You obviously can&amp;rsquo;t build a great team by writing a spec and triaging bugs. But you nonetheless need to invest into it as it doesn&amp;rsquo;t come for free or even happen automatically.&lt;/p&gt;
&lt;p&gt;In this post, I&amp;rsquo;d like to show some of the big and small things that my team does and what makes working with this team so great &amp;ndash; even when being under stress.&lt;/p&gt;
&lt;h1&gt;Official: Morale Events&lt;/h1&gt;
&lt;p&gt;About twice a year each team at Microsoft has a morale event. It&amp;rsquo;s up to the team to decide what to do but it typically involves some kind of team sport and food. Usually, these events are paid for by Microsoft. But before you think Formula 1 and Champaign with caviar: like in almost every company budget for those type of things is limited. However, I don&amp;rsquo;t think a successful morale event needs a lot of money. All it needs is a point in time and a place to gather.&lt;/p&gt;
&lt;p&gt;About a month ago, our team went out to play &lt;a href="http://en.wikipedia.org/wiki/Kickball"&gt;kickball&lt;/a&gt; and have a BBQ in a public park. In case you wonder whether we had fun: you can judge by browsing the pictures below.&lt;/p&gt;
&lt;p style="text-align: center;"&gt;&lt;a href="http://sdrv.ms/12w388v"&gt;&lt;img src="http://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-34/1581.SocialEngineering1.jpg" alt="" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p style="text-align: center;"&gt;(Click picture or &lt;a href="http://sdrv.ms/12w388v"&gt;this link&lt;/a&gt;&amp;nbsp;to see more pictures)&lt;/p&gt;
&lt;h1&gt;Semi Official: Get Together&lt;/h1&gt;
&lt;p&gt;It would miss the point if I&amp;rsquo;d state that it&amp;rsquo;s only the employer&amp;rsquo;s responsibility to invest into team building. In fact, I&amp;rsquo;m not even sure building a team like that is possible. From personal experience I&amp;rsquo;d say that in all the teams I&amp;rsquo;ve worked in the ones with the best culture had one thing in common: most of the team building was driven by individual team members.&lt;/p&gt;
&lt;p&gt;In our team, we usually hang out during lunch time instead of using this time of the day to be productive in front of the computer. However, I don&amp;rsquo;t mean to say you absolutely have to have a team lunch. But in my experience talking to your coworkers in an unofficial environment, such as the company&amp;rsquo;s cafeteria or a sushi place, is super helpful. For one, you have a venue to talk about personal stuff, such as renovating your living room or the tough nights you had because your daughter is getting teeth. And secondly, chances are you talk about work stuff as well. I found that people are more open to make compromises when they talk about the issue in a relaxing environment. Also, I&amp;rsquo;ve found on more than one occasion we developed a brilliant idea during lunch.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.joelonsoftware.com/items/2011/04/28.html"&gt;Joel takes this one step further&lt;/a&gt; and has lunch delivered to his employees so that they can all sit together as one big family.&lt;/p&gt;
&lt;p&gt;&lt;img style="margin-right: auto; margin-left: auto; display: block;" src="http://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-34/7853.SocialEngineering2.jpg" alt="" border="0" /&gt;&lt;/p&gt;
&lt;p&gt;In addition to that, we meet every Friday after work in a social area in our building. Each week, someone else is responsible for getting the beer, chips and cake. Yes, &lt;a href="http://en.wikipedia.org/wiki/Portal_(video_game)"&gt;there will be cake&lt;/a&gt;.&lt;/p&gt;
&lt;h1&gt;Unofficial: Office Pranks&lt;/h1&gt;
&lt;p&gt;At Microsoft, we have a long tradition of office pranks. They range from smaller jokes like removing the computer equipment to more elaborate pranks like replacing the door with a fully painted dry-wall. Needless to say they are unofficial and not driven by Microsoft, but completely organized by individual team members.&lt;/p&gt;
&lt;p&gt;When I went on a conference trip to Germany a few weeks ago, my team pulled off a super awesome office prank. Since I&amp;rsquo;m a big Star Trek fan they got inspired by &lt;a href="http://www.youtube.com/watch?v=rQ6LC-olw9Q"&gt;this episode&lt;/a&gt; and flooded my office with &lt;a href="http://en.wikipedia.org/wiki/Tribble_(Star_Trek)"&gt;tribbles&lt;/a&gt;. Great pictures and epic making-of mails can be found below.&lt;/p&gt;
&lt;p style="text-align: center;"&gt;&lt;a href="http://sdrv.ms/XNzgow"&gt;&lt;img src="http://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-34/7444.SocialEngineering3.jpg" alt="" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p style="text-align: center;"&gt;(Click picture or &lt;a href="http://sdrv.ms/XNzgow"&gt;this link&lt;/a&gt;&amp;nbsp;to see more pictures)&lt;/p&gt;
&lt;p&gt;Special thanks to our release PM Marc Goodner who organized and PM&amp;rsquo;ed this epic prank!&lt;/p&gt;
&lt;h1&gt;Summary&lt;/h1&gt;
&lt;p&gt;In case you didn&amp;rsquo;t notice, I wrote this post primarily to say Thanks to my team. We talk a lot about the software we ship but cool stuff deserves to be shared &amp;ndash; even it&amp;rsquo;s not shipping and especially when it involves people.&lt;/p&gt;
&lt;p&gt;In case you thought &amp;ldquo;I wish I could be part of such a team&amp;rdquo; I&amp;rsquo;ve good news for you. &lt;a href="http://aka.ms/f1jurj"&gt;We are hiring!&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;As always, we love to hear what you have to say. What do you do to keep up the team spirit? Share your story in the comments!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10413741" width="1" height="1"&gt;</description></item><item><title>.NET Memory Allocation Profiling with Visual Studio 2012</title><link>http://blogs.msdn.com/b/dotnet/archive/2013/04/04/net-memory-allocation-profiling-with-visual-studio-2012.aspx</link><pubDate>Thu, 04 Apr 2013 15:54:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10407439</guid><dc:creator>The .NET Team</dc:creator><slash:comments>7</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/dotnet/rsscomments.aspx?WeblogPostID=10407439</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/dotnet/commentapi.aspx?WeblogPostID=10407439</wfw:comment><comments>http://blogs.msdn.com/b/dotnet/archive/2013/04/04/net-memory-allocation-profiling-with-visual-studio-2012.aspx#comments</comments><description>&lt;blockquote&gt;   &lt;p&gt;&lt;em&gt;This post was written by Stephen Toub, a frequent contributor to the &lt;a href="http://blogs.msdn.com/b/pfxteam"&gt;Parallel Programming in .NET blog&lt;/a&gt;. He shows us how Visual Studio 2012 and an attention to detail can help you discover unnecessary allocations in your app that can prevent it from achieving higher performance. &lt;/em&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Visual Studio 2012 has a wealth of valuable functionality, so much so that I periodically hear developers that already use Visual Studio asking for a feature the IDE already has and that they’ve just never discovered. Other times, I hear developers asking about a specific feature, thinking it’s meant for one purpose, not realizing it’s really intended for another.&lt;/p&gt;  &lt;p&gt;Both of these cases apply to Visual Studio’s &lt;a href="http://msdn.microsoft.com/library/dd264966.aspx"&gt;.NET memory allocation profiler&lt;/a&gt;. Many developers that could benefit from it don’t know it exists, and other developers have an incorrect expectation for its purpose. This is unfortunate, as the feature can provide a lot of value for particular scenarios; many developers will benefit from understanding first that it exists, and second the intended scenarios for its use.&lt;/p&gt;  &lt;h3&gt;Why memory profiling?&lt;/h3&gt;  &lt;p&gt;When it comes to .NET and memory analysis, there are two primary reasons one would want to use a diagnostics tool:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;&lt;b&gt;To discover memory leaks. &lt;/b&gt;Leaks on a garbage-collecting runtime like the CLR manifest differently than do leaks in a non-garbage-collected environment, such as in code written in C/C++. A leak in the latter typically occurs due to the developer not manually freeing some memory that was previously allocated. In a garbage collected environment, however, manually freeing memory isn’t required, as that’s the duty of the &lt;a href="http://msdn.microsoft.com/library/0xy59wtx.aspx"&gt;garbage collector&lt;/a&gt; (GC). However, the GC can only release memory that is provably no longer being used, meaning as long as there are no rooted references to the memory. Leaks in .NET code manifest then when some memory that should have been collected is incorrectly still rooted, e.g. a reference to the object occurs in an event handler registered with a static event. A good memory analysis tool might help you to find such leaks, such as by allowing you to take snapshots of the process at two different points and then comparing those snapshots to see which objects stuck around for the second point, and more importantly, why.&lt;/li&gt;    &lt;li&gt;&lt;b&gt;To discover unnecessary allocations.&lt;/b&gt; In .NET, allocation is often quite cheap. This cost is deceptive, however, as there are more costs later when the GC needs to clean up. The more memory that gets allocated, the more frequently the GC will need to run, and typically the more objects that survive collections, the more work the GC needs to do when it runs to determine which objects are no longer reachable. Thus, the more allocations a program does, the higher the GC costs will be. These GC costs are often negligible to the program’s performance profile, but for certain kinds of apps, especially those on servers that require high-throughput operation, these costs can add up quickly and make a noticeable impact to the performance of the app. As such, a good memory analysis tool might help you to understand all of the allocation being done by the program, in order to help spot allocations you can potentially avoid.&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;The .NET memory profiler included in Visual Studio 2012 (Professional and higher versions) was designed primarily to address the latter case of helping to discover unnecessary allocations, and it’s quite useful towards that goal, as the rest of this post will explore. The tool is not tuned for the former case of finding and fixing memory leaks, though this is an area the Visual Studio diagnostics team is looking to address in depth for the future (you can see &lt;a href="http://msdn.microsoft.com/library/windows/apps/jj819176.aspx"&gt;such an experience for JavaScript&lt;/a&gt; that was added to Visual Studio as part of &lt;a href="http://blogs.msdn.com/b/somasegar/archive/2012/11/26/visual-studio-2012-update-1-now-available.aspx"&gt;VS2012.1&lt;/a&gt;). While the tool today does have an &lt;a href="http://msdn.microsoft.com/library/dd264934.aspx"&gt;advanced option to track when objects are collected&lt;/a&gt;, it doesn’t help you to understand why objects weren’t collected or why they were held onto longer than was expected.&lt;/p&gt;  &lt;p&gt;There are also other useful tools in this space. The downloadable &lt;a href="http://www.microsoft.com/download/details.aspx?id=28567"&gt;PerfView&lt;/a&gt; &lt;a href="http://blogs.msdn.com/b/dotnet/archive/2012/10/09/improving-your-app-s-performance-with-perfview.aspx"&gt;tool&lt;/a&gt; doesn’t provide as user-friendly an interface as does the .NET memory profiler in Visual Studio 2012, but it is a very powerful tool that supports both tasks of finding memory leaks and discovering unnecessary allocations. It also supports profiling Windows Store apps, which the .NET memory allocation profiler in Visual Studio 2012 does not currently support as of the writing of this post.&lt;/p&gt;  &lt;h3&gt;Example to be optimized&lt;/h3&gt;  &lt;p&gt;To better understand the memory profiler’s role and how it can help, let’s walk through an example. We’ll start with the core method that we’ll be looking to optimize (in a real-world case, you’d likely be analyzing your whole application and narrowing in on the particular offending areas, but for the purpose of this example, we’ll keep this constrained):&lt;/p&gt;  &lt;div id="codeSnippetWrapper"&gt;   &lt;blockquote&gt;     &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; async Task&amp;lt;T&amp;gt; WithCancellation1&amp;lt;T&amp;gt;(&lt;span class="kwrd"&gt;this&lt;/span&gt; Task&amp;lt;T&amp;gt; task, CancellationToken cancellationToken)
{
    var tcs = &lt;span class="kwrd"&gt;new&lt;/span&gt; TaskCompletionSource&amp;lt;&lt;span class="kwrd"&gt;bool&lt;/span&gt;&amp;gt;();
    &lt;span class="kwrd"&gt;using&lt;/span&gt; (cancellationToken.Register(() =&amp;gt; tcs.TrySetResult(&lt;span class="kwrd"&gt;true&lt;/span&gt;)))
    &lt;span class="kwrd"&gt;if&lt;/span&gt; (task != await Task.WhenAny(task, tcs.Task))
        &lt;span class="kwrd"&gt;throw&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; OperationCanceledException(cancellationToken);
    &lt;span class="kwrd"&gt;return&lt;/span&gt; await task;
}&lt;/pre&gt;
  &lt;/blockquote&gt;
  &lt;/div&gt;

&lt;p&gt;The purpose of this small method is to enable code to await a task in a cancelable manner, meaning that regardless of whether the task has completed, the developer wants to be able to stop waiting for it. Instead of writing code like:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="csharpcode"&gt;T result = await someTask;&lt;/pre&gt;
&lt;/blockquote&gt;


&lt;p&gt;the developer would write:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="csharpcode"&gt;T result = await someTask.WithCancellation1(token);&lt;/pre&gt;
&lt;/blockquote&gt;


&lt;p&gt;and if cancellation is requested on the relevant CancellationToken before the task completes, an OperationCanceledException will be thrown. This is achieved in WithCancellation1 by wrapping the original task in an async method. The method creates a second task that will complete when cancellation is requested (by Registering a call to TrySetResult with the CancellationToken), and then uses Task.WhenAny to wait for either the original task or the cancellation task to complete. As soon as either does, the async method completes, either by throwing a cancellation exception if the cancellation task completed first, or by propagating the outcome of the original task by awaiting it. (For more details, see the blog post “&lt;a href="http://blogs.msdn.com/b/pfxteam/archive/2012/10/05/how-do-i-cancel-non-cancelable-async-operations.aspx"&gt;How do I cancel non-cancelable async operations?&lt;/a&gt;”)&lt;/p&gt;

&lt;p&gt;To understand the allocations involved in this method, we’ll use a small harness method:&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="csharpcode"&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System;
&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Threading;
&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Threading.Tasks;
 
&lt;span class="kwrd"&gt;class&lt;/span&gt; Harness
{
    &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Main() 
     { 
         Console.ReadLine(); &lt;span class="rem"&gt;// wait until profiler attaches&lt;/span&gt;
         TestAsync().Wait(); 
     }
    &lt;span class="kwrd"&gt;static&lt;/span&gt; async Task TestAsync()
    {
        var token = CancellationToken.None;
        &lt;span class="kwrd"&gt;for&lt;/span&gt;(&lt;span class="kwrd"&gt;int&lt;/span&gt; i=0; i&amp;lt;100000; i++)
            await Task.FromResult(42).WithCancellation1(token);
    }
}
 

&lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Extensions
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; async Task&amp;lt;T&amp;gt; WithCancellation1&amp;lt;T&amp;gt;(
    &lt;span class="kwrd"&gt;this&lt;/span&gt; Task&amp;lt;T&amp;gt; task, CancellationToken cancellationToken)
    {
        var tcs = &lt;span class="kwrd"&gt;new&lt;/span&gt; TaskCompletionSource&amp;lt;&lt;span class="kwrd"&gt;bool&lt;/span&gt;&amp;gt;();
        &lt;span class="kwrd"&gt;using&lt;/span&gt; (cancellationToken.Register(() =&amp;gt; tcs.TrySetResult(&lt;span class="kwrd"&gt;true&lt;/span&gt;)))
            &lt;span class="kwrd"&gt;if&lt;/span&gt; (task != await Task.WhenAny(task, tcs.Task))
                &lt;span class="kwrd"&gt;throw&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; OperationCanceledException(cancellationToken);
        &lt;span class="kwrd"&gt;return&lt;/span&gt; await task;
    }
}&lt;/pre&gt; 
  &lt;/pre&gt;
&lt;/blockquote&gt;


&lt;p&gt;The TestAsync method will iterate 100,000 times. Each time, it creates a new task, invokes the WithCancellation1 on it, and awaits the result of that WithCancellation1 call. This await will complete synchronously, as the task created by Task.FromResult is returned in an already completed state, and the WithCancellation1 method itself doesn’t introduce any additional asynchrony such that the task it returns will complete synchronously as well.&lt;/p&gt;

&lt;h3&gt;Running the .NET memory allocation profiler&lt;/h3&gt;

&lt;p&gt;To start the memory allocation profiler, in Visual Studio go to the Analyze menu and select “Launch Performance Wizard…”. This will open a dialog like the following:&lt;/p&gt;

&lt;p&gt;&lt;img title="image" style="border: 0px currentcolor; margin-right: auto; margin-left: auto; float: none; display: block; background-image: none;" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-34-metablogapi/1586.image_5F00_6BAB9C20.png" width="487" height="419" /&gt;&lt;/p&gt;

&lt;p&gt;Choose “.NET memory allocation (sampling)”, click Next twice, followed by Finish (if this is the first time you’ve used the profiler since you logged into Windows, you’ll need to accept the elevation prompt so the profiler can start). At that point, the application will be launched and the profiler will start monitoring it for allocations (the harness code above also requires that you press ‘Enter’, in order to ensure the profiler has attached by the time the program starts the real test). When the app completes, or when you manually choose to stop profiling, the profiler will load symbols and will start analyzing the trace. That’s a good time to go and get yourself a cup of coffee, or lunch, as depending on how many allocations occurred, the tool can take a while to do this analysis.&lt;/p&gt;

&lt;p&gt;When the analysis completes, we’re presented with a summary of the allocations that occurred, including highlighting the functions that allocated the most memory, the types that resulted in the most memory allocated, and the types with the most instances allocated:&lt;/p&gt;

&lt;p&gt;&lt;img title="image" style="border: 0px currentcolor; margin-right: auto; margin-left: auto; float: none; display: block; background-image: none;" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-34-metablogapi/7652.image_5F00_1D3709B6.png" width="624" height="334" /&gt;&lt;/p&gt;

&lt;p&gt;From there, we can drill in further, by looking at the allocations summary (choose “Allocation” from the “Current View” dropdown):&lt;/p&gt;

&lt;p&gt;&lt;img title="image" style="border: 0px currentcolor; margin-right: auto; margin-left: auto; float: none; display: block; background-image: none;" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-34-metablogapi/5518.image_5F00_120D7F6C.png" width="624" height="205" /&gt;&lt;/p&gt;

&lt;p&gt;Here, we get to see a row for each type that was allocated, with the columns showing information about how many allocations were tracked, how much space was associated with those allocations, and what percentage of allocations mapped back to that type. We can also expand an entry to see the stack of method calls that resulted in these allocations:&lt;/p&gt;

&lt;p&gt;&lt;img title="image" style="border: 0px currentcolor; margin-right: auto; margin-left: auto; float: none; display: block; background-image: none;" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-34-metablogapi/2465.image_5F00_71F272AE.png" width="624" height="279" /&gt;&lt;/p&gt;

&lt;p&gt;By selecting the “Functions” view, we can get a different pivot on this data, highlighting which functions allocated the most objects and bytes:&lt;/p&gt;

&lt;p&gt;&lt;img title="image" style="border: 0px currentcolor; margin-right: auto; margin-left: auto; float: none; display: block; background-image: none;" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-34-metablogapi/3036.image_5F00_3CE5E37E.png" width="624" height="212" /&gt;&lt;/p&gt;

&lt;h3&gt;Interpreting and acting on the profiling results&lt;/h3&gt;

&lt;p&gt;With this capability, we can analyze our example’s results. First, we can see that there’s a substantial number of allocations here, which might be surprising. After all, in our example we were using WithCancellation1 with a task that was already completed, which means there should have been very little work to do (with the task already done, there is nothing to cancel), and yet from the above trace we can see that each iteration of our example is resulting in:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Three allocations of Task`1 (we ran the harness 100K times and can see there were ~300K allocations)&lt;/li&gt;
&lt;/ul&gt;

&lt;ul&gt;
  &lt;li&gt;Two allocations of Task[] &lt;/li&gt;
&lt;/ul&gt;

&lt;ul&gt;
  &lt;li&gt;One allocation each of TaskCompletionSource`1, Action, a compiler-generated type called &amp;lt;&amp;gt;c_DisplayClass2`1, and some type called CompleteOnInvokePromise&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That’s nine allocations for a case where we might expect only one (the task allocation we explicitly asked for in the harness by calling Task.FromResult), with our WithCancellation1 method incurring eight allocations.&lt;/p&gt;

&lt;p&gt;For helper operations on tasks, it’s actually fairly common to deal with already completed tasks, as often times operations implemented to be asynchronous will actually complete synchronously (e.g. one read operation on a network stream may buffer into memory enough additional data to fulfill a subsequent read operation). As such, optimizing for the already completed case can be really beneficial for performance. Let’s try. Here’s a second attempt at WithCancellation, one that optimizes for several “already completed” cases:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; Task&amp;lt;T&amp;gt; WithCancellation2&amp;lt;T&amp;gt;(&lt;span class="kwrd"&gt;this&lt;/span&gt; Task&amp;lt;T&amp;gt; task, &lt;br /&gt;                             CancellationToken cancellationToken)
    {
        &lt;span class="kwrd"&gt;if&lt;/span&gt; (task.IsCompleted || !cancellationToken.CanBeCanceled)
            &lt;span class="kwrd"&gt;return&lt;/span&gt; task;
        &lt;span class="kwrd"&gt;else&lt;/span&gt; &lt;span class="kwrd"&gt;if&lt;/span&gt; (cancellationToken.IsCancellationRequested)
            &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; Task&amp;lt;T&amp;gt;(() =&amp;gt; &lt;span class="kwrd"&gt;default&lt;/span&gt;(T), cancellationToken);
        &lt;span class="kwrd"&gt;else&lt;/span&gt;
            &lt;span class="kwrd"&gt;return&lt;/span&gt; task.WithCancellation1(cancellationToken);
    }&lt;/pre&gt;


&lt;p&gt;This implementation checks:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;First, whether the task is already completed or whether the supplied CancellationToken can’t be canceled; in both of those cases, there’s no additional work needed, as cancellation can’t be applied, and as such we can just return the original task immediately rather than spending any time or memory creating a new one.&lt;/li&gt;
&lt;/ul&gt;

&lt;ul&gt;
  &lt;li&gt;Then whether cancellation has already been requested; if it has, we can allocate a single already-canceled task to be returned, rather than spending the eight allocations we previously paid to invoke our original implementation.&lt;/li&gt;
&lt;/ul&gt;

&lt;ul&gt;
  &lt;li&gt;Finally, if none of these fast paths apply, we fall through to calling the original implementation.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Re-profiling our micro-benchmark while using WithCancellation2 instead of WithCancellation1 provides a much improved outlook (you’ll likely notice that the analysis completes much more quickly than it did before, already a sign that we’ve significantly decreased memory allocation). Now we have just have the primary allocation we expected, the one from Task.FromResult called from our TestAsync method in the harness:&lt;/p&gt;

&lt;p&gt;&lt;img title="image" style="border: 0px currentcolor; margin-right: auto; margin-left: auto; float: none; display: block; background-image: none;" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-34-metablogapi/7268.image_5F00_1CCAD6C1.png" width="624" height="115" /&gt;&lt;/p&gt;

&lt;p&gt;So, we’ve now successfully optimized the case where the task is already completed, where cancellation can’t be requested, or where cancellation has already been requested. What about the case where we do actually need to invoke the more complicated logic? Are there any improvements that can be made there?&lt;/p&gt;

&lt;p&gt;Let’s change our benchmark to use a task that’s not already completed by the time we invoke WithCancellation2, and also to use a token that can have cancellation requested. This will ensure we make it to the “slow” path:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;    &lt;span class="kwrd"&gt;static&lt;/span&gt; async Task TestAsync()
    {
        var token = &lt;span class="kwrd"&gt;new&lt;/span&gt; CancellationTokenSource().Token;
        &lt;span class="kwrd"&gt;for&lt;/span&gt; (&lt;span class="kwrd"&gt;int&lt;/span&gt; i = 0; i &amp;lt; 100000; i++)
        {
            var tcs = &lt;span class="kwrd"&gt;new&lt;/span&gt; TaskCompletionSource&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt;();
            var t = tcs.Task.WithCancellation2(token);
            tcs.SetResult(42);
            await t;
        }
    }&lt;/pre&gt;


&lt;p&gt;Profiling again provides more insight:&lt;/p&gt;

&lt;p&gt;&lt;img title="image" style="border: 0px currentcolor; margin-right: auto; margin-left: auto; float: none; display: block; background-image: none;" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-34-metablogapi/0486.image_5F00_7CAFCA03.png" width="624" height="132" /&gt;&lt;/p&gt;

&lt;p&gt;On this slow path, there are now 14 total allocations per iteration, including the 2 from our TestAsync harness (the TaskCompletionSource&amp;lt;int&amp;gt; we explicitly create, and the Task&amp;lt;int&amp;gt; it creates). At this point, we can use all of the information provided by the profiling results to understand where the remaining 12 allocations are coming from and to then address them as is relevant and possible. For example, let’s look at two allocations specifically: the &amp;lt;&amp;gt;c__DisplayClass2`1 instance and one of the two Action instances. These two allocations will likely be logical to anyone familiar with &lt;a href="http://blogs.msdn.com/b/pfxteam/archive/2012/02/29/10263921.aspx"&gt;how the C# compiler handles closures&lt;/a&gt;. Why do we have a closure? Because of this line:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt;(cancellationToken.Register(() =&amp;gt; tcs.TrySetResult(&lt;span class="kwrd"&gt;true&lt;/span&gt;)))&lt;/pre&gt;
&lt;/blockquote&gt;


&lt;p&gt;The call to Register is closing over the ‘tcs’ variable. But this isn’t strictly necessary: the Register method has another overload which instead of taking an Action takes an Action&amp;lt;object&amp;gt; and the object state to be passed to it. If we instead rewrite this line to use that state-based overload, along with a manually cached delegate, we can avoid the closure and those two allocations:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;readonly&lt;/span&gt; Action&amp;lt;&lt;span class="kwrd"&gt;object&lt;/span&gt;&amp;gt; s_cancellationRegistration =
    s =&amp;gt; ((TaskCompletionSource&amp;lt;&lt;span class="kwrd"&gt;bool&lt;/span&gt;&amp;gt;)s).TrySetResult(&lt;span class="kwrd"&gt;true&lt;/span&gt;);
…
&lt;span class="kwrd"&gt;using&lt;/span&gt;(cancellationToken.Register(s_cancellationRegistration, tcs))
  &lt;/pre&gt;
  &lt;/blockquote&gt;

&lt;p&gt;Rerunning the profiler confirms those two allocations are no longer occurring:&lt;/p&gt;

&lt;h3&gt;Start profiling today!&lt;/h3&gt;

&lt;p&gt;This cycle of profiling, finding and eliminating hotspots, and then going around again is a common approach towards improving the performance of your code, whether using a CPU profiler or a memory profiler. So, if you find yourself in a scenario where you determine that minimizing allocations is important for the performance of your code, give the .NET memory allocation profiler in Visual Studio 2012 a try. Feel free to &lt;a href="http://aka.ms/wpv10c"&gt;download the sample project used in this blog post&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;For more on profiling, see the blog of the &lt;a href="http://blogs.msdn.com/b/visualstudioalm/archive/tags/diagnostics/"&gt;Visual Studio Diagnostics team&lt;/a&gt;, and ask them questions in the &lt;a href="http://social.msdn.microsoft.com/Forums/en-US/vsdebug/threads"&gt;Visual Studio Diagnostics forum&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Stephen Toub&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;Follow us on Twitter (&lt;a href="https://twitter.com/dotnet"&gt;@dotnet&lt;/a&gt;) and Facebook (&lt;a href="http://facebook.com/dotnet"&gt;dotnet&lt;/a&gt;). You can follow other .NET teams, too: &lt;a href="http://twitter.com/aspnet"&gt;@aspnet&lt;/a&gt;/&lt;a href="http://facebook.com/asp.net"&gt;asp.net&lt;/a&gt;, &lt;a href="https://twitter.com/efmagicunicorns"&gt;@efmagicunicorns&lt;/a&gt;/&lt;a href="https://www.facebook.com/efmagicunicorns"&gt;efmagicunicorns&lt;/a&gt;, &lt;a href="http://twitter.com/visualstudio"&gt;@visualstudio&lt;/a&gt;/&lt;a href="https://www.facebook.com/visualstudio"&gt;visualstudio&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=10407439" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/dotnet/archive/tags/-net+framework/">.net framework</category><category domain="http://blogs.msdn.com/b/dotnet/archive/tags/async/">async</category><category domain="http://blogs.msdn.com/b/dotnet/archive/tags/diagnostics/">diagnostics</category><category domain="http://blogs.msdn.com/b/dotnet/archive/tags/visual+studio/">visual studio</category></item><item><title>.NET Framework Documentation Improvements</title><link>http://blogs.msdn.com/b/dotnet/archive/2013/02/19/net-documentation-improvements.aspx</link><pubDate>Tue, 19 Feb 2013 20:07:49 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10395322</guid><dc:creator>Brandon Bray</dc:creator><slash:comments>29</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/dotnet/rsscomments.aspx?WeblogPostID=10395322</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/dotnet/commentapi.aspx?WeblogPostID=10395322</wfw:comment><comments>http://blogs.msdn.com/b/dotnet/archive/2013/02/19/net-documentation-improvements.aspx#comments</comments><description>&lt;p&gt;The CLR documentation team has been busy responding to feedback and making updates and changes to the &lt;a href="http://msdn.microsoft.com/library/vstudio/w0x726c2.aspx"&gt;.NET Framework documentation&lt;/a&gt; in the MSDN Library. We would like to tell you about the most recent set of document updates, which were published earlier in February.&lt;/p&gt;  &lt;h3&gt;&lt;b&gt;Performance content&lt;/b&gt;&lt;/h3&gt;  &lt;p&gt;We have received extensive customer feedback regarding the importance of performance in .NET Framework apps, and we wanted to make it easier for you to find relevant content. &lt;/p&gt;  &lt;p&gt;As a result, we’ve reworked the existing performance and reliability topic to include more performance guidance as well as links to performance analysis tools and technology-specific performance content. We’ll continue to improve this documentation area in the coming months.&lt;/p&gt;  &lt;p&gt;Do take a look at the revised &lt;a href="http://msdn.microsoft.com/en-us/library/hh156536.aspx"&gt;Performance in .NET Framework Apps&lt;/a&gt; topic. Please give us your feedback to help us choose which area to focus on next. &lt;/p&gt;  &lt;h3&gt;&lt;b&gt;Overload list topics&lt;/b&gt;&lt;/h3&gt;  &lt;p&gt;We are experimenting with a new approach to document methods that have overloads. These pages have been an area of consistent feedback. We have heard that the current approach requires too many clicks to get to the overload page that you want. We are piloting this new approach with the following method overload pages:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/system.datetime.parse.aspx"&gt;DateTime.Parse&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/as4h66hd.aspx"&gt;Math.Round&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/system.string.string.aspx"&gt;String constructors&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/xh1dzhdx.aspx"&gt;String.Format&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Let’s take a look at why we decided to try something different with these pages. In the MSDN Library, our current documentation design for overloaded methods and constructors uses an overload list to provide summary information and links to individual overload pages. Typically, these pages include just the overload list, requiring you to choose one of the overloads to find any technical information for that method beyond just the overload signatures. For example, the following image shows a portion of the &lt;a href="http://msdn.microsoft.com/en-us/library/2se42k1z(v=vs.110).aspx"&gt;&lt;b&gt;String.Compare&lt;/b&gt;&lt;/a&gt; page. &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;img title="image" style="border: 0px currentcolor; margin-right: auto; margin-left: auto; float: none; display: block; background-image: none;" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-34-metablogapi/7624.image_5F00_3FE79849.png" width="622" height="443" /&gt;&lt;/p&gt;  &lt;br /&gt;The overload list provides an abbreviated syntax for each overload (without its return value or parameter names) and a brief, typically one-sentence description. The page was designed to provide just enough information for you to select an overload, and then follow a link to detailed documentation about that overload. However, we have heard many of you ask for more detail on the overload page, such as individual parameters, return value information, and examples.   &lt;p&gt;Until now, our strategy has been to direct you to the detailed information on the individual overload pages. We had done two things to achieve that goal:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Added the following note before the overload list: &amp;quot;This member is overloaded. For complete information about this member, including syntax, usage, and examples, click a name in the overload list.&amp;quot; &lt;/li&gt;    &lt;li&gt;Reduced the visibility of these pages by ensuring that they do not appear in searches and member lists. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;However, even after those changes, we were still hearing requests for more information. To address this issue, we would like to solicit your feedback about the new approach we're trying out. As already stated, we've revised four overload list topics to provide complete documentation for all the individual method overloads. In each case, the overload list topic should serve as a one-stop source for information about that method and all its overloads. &lt;/p&gt;  &lt;p&gt;Note, though, that our build system does not yet support this revised format. Because of this, the boilerplate text &amp;quot;This method is overloaded…&amp;quot; continues to appear on these overload list pages. At a later time, we will remove that text.&lt;/p&gt;  &lt;p&gt;The expanded documentation is in the Remarks section and includes complete method syntax, parameter and return value descriptions, a list of exceptions, a table to help choose an overload, extended discussion of using the method, and at least one example for each overload.&amp;#160; Here are a few screenshots from the updated &lt;a href="http://msdn.microsoft.com/library/xh1dzhdx.aspx"&gt;String.Format&lt;/a&gt; overload page to give you a better idea of the information that you can expect on overload pages with this new approach. Feel free to check out the whole page for yourself.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;img title="image" style="border: 0px currentcolor; margin-right: auto; margin-left: auto; float: none; display: block; background-image: none;" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-34-metablogapi/8475.image_5F00_5DC50CF1.png" width="519" height="480" /&gt;&lt;/p&gt;  &lt;p&gt;&lt;img title="image" style="border: 0px currentcolor; margin-right: auto; margin-left: auto; float: none; display: block; background-image: none;" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-34-metablogapi/0045.image_5F00_64781674.png" width="582" height="480" /&gt;&lt;/p&gt;  &lt;p&gt;&lt;img title="image" style="border: 0px currentcolor; margin-right: auto; margin-left: auto; float: none; display: block; background-image: none;" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-34-metablogapi/0068.image_5F00_445D09B7.png" width="498" height="480" /&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Tell us what you think of this approach. Does it make it easier to get to the information you need? Do you have any suggestions for further improvements? We look forward to hearing from you.&lt;/p&gt;  &lt;p&gt;Follow us on Twitter (&lt;a href="https://twitter.com/dotnet"&gt;@dotnet&lt;/a&gt;) and Facebook (&lt;a href="http://facebook.com/dotnet"&gt;dotnet&lt;/a&gt;). You can follow other .NET teams, too: &lt;a href="http://twitter.com/aspnet"&gt;@aspnet&lt;/a&gt;/&lt;a href="http://facebook.com/asp.net"&gt;asp.net&lt;/a&gt;, &lt;a href="https://twitter.com/efmagicunicorns"&gt;@efmagicunicorns&lt;/a&gt;/&lt;a href="https://www.facebook.com/efmagicunicorns"&gt;efmagicunicorns&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=10395322" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/dotnet/archive/tags/clr/">clr</category><category domain="http://blogs.msdn.com/b/dotnet/archive/tags/-net+framework/">.net framework</category><category domain="http://blogs.msdn.com/b/dotnet/archive/tags/performance/">performance</category><category domain="http://blogs.msdn.com/b/dotnet/archive/tags/bcl/">bcl</category><category domain="http://blogs.msdn.com/b/dotnet/archive/tags/announcement/">announcement</category><category domain="http://blogs.msdn.com/b/dotnet/archive/tags/docs/">docs</category></item><item><title>C# is the “Language of the Year”</title><link>http://blogs.msdn.com/b/dotnet/archive/2013/01/03/c-is-the-language-of-the-year.aspx</link><pubDate>Fri, 04 Jan 2013 01:25:09 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10382254</guid><dc:creator>Brandon Bray</dc:creator><slash:comments>12</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/dotnet/rsscomments.aspx?WeblogPostID=10382254</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/dotnet/commentapi.aspx?WeblogPostID=10382254</wfw:comment><comments>http://blogs.msdn.com/b/dotnet/archive/2013/01/03/c-is-the-language-of-the-year.aspx#comments</comments><description>&lt;p&gt;It’s a great way to start a new year: the &lt;a href="https://sites.google.com/site/pydatalog/pypl/PyPL-PopularitY-of-Programming-Language"&gt;PYPL index has named C# the language of the year&lt;/a&gt;. This index focuses on the leading indicator of learning a language. It’s nice to see that there’s been increased interest in C# over the past year. In the spirit of learning something new, here’s what we suggest you check out if you’re looking to learn a new C# trick.&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;&lt;b&gt;Learn to program with Async. &lt;/b&gt;Async is finally unleashing the power of multicore hardware. Where I/O is involved, we expect almost all new platform APIs to be exposed as async versions. This &lt;a href="http://blogs.msdn.com/b/dotnet/archive/2012/04/03/async-in-4-5-worth-the-await.aspx"&gt;new language capability&lt;/a&gt; ensures that you can quickly build &lt;a href="http://blogs.msdn.com/b/dotnet/archive/2012/11/13/the-net-framework-4-5-is-optimized-for-the-cloud.aspx"&gt;highly scalable&lt;/a&gt; and &lt;a href="http://blogs.msdn.com/b/dotnet/archive/2012/06/06/async-in-4-5-enabling-progress-and-cancellation-in-async-apis.aspx"&gt;highly responsive&lt;/a&gt; apps for the cloud and devices.       &lt;br /&gt;&lt;b&gt;       &lt;br /&gt;&lt;/b&gt;&lt;/li&gt;    &lt;li&gt;&lt;b&gt;Learn to build a Windows Store App.&lt;/b&gt; All of our favorite Windows Store apps were written with C# – &lt;a href="http://www.amazon.com/Windows-Apps-XAML-Unleashed-Nathan/dp/0672336014"&gt;yours can be too&lt;/a&gt;. Many of the new Windows APIs are &lt;a href="http://msdn.microsoft.com/en-us/magazine/jj651569.aspx"&gt;based on WinRT&lt;/a&gt;, which means that all of these new APIs are amazing with C#. To top it off, you can use &lt;a href="http://blogs.msdn.com/b/dotnet/archive/2012/11/20/building-windows-store-apps-with-net.aspx"&gt;XAML&lt;/a&gt; or &lt;a href="http://sharpdx.org/"&gt;DirectX&lt;/a&gt;.       &lt;br /&gt;      &lt;br /&gt;&lt;/li&gt;    &lt;li&gt;&lt;b&gt;Learn to build (or use) cross-platform libraries.&lt;/b&gt; .NET is now integral to every platform Microsoft builds. And with so many platforms to target, we’ve made it &lt;a href="http://blogs.msdn.com/b/dotnet/archive/2012/07/06/targeting-multiple-platforms-with-portable-code-overview.aspx"&gt;easy to write portable libraries&lt;/a&gt; so you can distribute your binary once for all of them. Many of these libraries are available on &lt;a href="http://www.nuget.org/packages"&gt;NuGet&lt;/a&gt; such as &lt;a href="http://www.nuget.org/packages/Newtonsoft.Json"&gt;Json.NET&lt;/a&gt;. Not to mention that your C# code also works on billions of non-Microsoft devices thanks to &lt;a href="http://xamarin.com/"&gt;tools from Xamarin&lt;/a&gt;.       &lt;br /&gt;      &lt;br /&gt;&lt;/li&gt;    &lt;li&gt;&lt;b&gt;Learn about the performance advantages of .NET.&lt;/b&gt; One of the best aspects of programming with C# is that your apps get faster by just upgrading to the latest version of Windows or .NET Framework. With Windows Server 2012, Windows 8 and .NET Framework 4.5 your app can see significant improvements in &lt;a href="http://blogs.msdn.com/b/dotnet/archive/2012/07/20/the-net-framework-4-5-includes-new-garbage-collector-enhancements-for-client-and-server-apps.aspx"&gt;garbage collection&lt;/a&gt; and &lt;a href="http://blogs.msdn.com/b/dotnet/archive/2012/10/18/an-easy-solution-for-improving-app-launch-performance.aspx"&gt;startup time&lt;/a&gt;. &lt;a href="http://blogs.technet.com/b/windowsserver/archive/2012/06/07/bing-com-runs-on-windows-server-2012.aspx"&gt;Bing saw some amazing performance wins&lt;/a&gt; from .NET too.       &lt;br /&gt;      &lt;br /&gt;&lt;/li&gt;    &lt;li&gt;&lt;b&gt;Learn about cool libraries that build on top of C#.&lt;/b&gt; What makes C# so cool is that our innovations in the language have inspired new programming patterns and libraries. LINQ is awesome and it’s led to the &lt;a href="http://msdn.microsoft.com/en-us/data/gg577609.aspx"&gt;Reactive Extensions (Rx)&lt;/a&gt;. Just spend some time understanding how to use Rx and you’ll take your programming game to a new level.&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;What new C# skill would you recommend everyone learn? Share your thoughts in the comments.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10382254" width="1" height="1"&gt;</description></item><item><title>Downloading the .NET Framework and Visual Studio Express Editions and SDKs</title><link>http://blogs.msdn.com/b/dotnet/archive/2012/12/17/downloading-the-net-framework-and-visual-studio-express-editions-and-sdks.aspx</link><pubDate>Mon, 17 Dec 2012 22:35:32 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10378831</guid><dc:creator>Brandon Bray</dc:creator><slash:comments>5</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/dotnet/rsscomments.aspx?WeblogPostID=10378831</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/dotnet/commentapi.aspx?WeblogPostID=10378831</wfw:comment><comments>http://blogs.msdn.com/b/dotnet/archive/2012/12/17/downloading-the-net-framework-and-visual-studio-express-editions-and-sdks.aspx#comments</comments><description>&lt;p&gt;We see a lot of daily traffic through the .NET blog and the &lt;a href="http://msdn.com/netframework"&gt;.NET dev center on MSDN&lt;/a&gt;. It is clear from the web traffic data that a key task for many of you is downloading more .NET software, to expand your options and experience developing .NET apps. The number one download for .NET is the .NET Framework –the latest version being the &lt;a href="http://www.microsoft.com/en-us/download/details.aspx?id=30653"&gt;.NET Framework 4.5&lt;/a&gt;. However, we also see a lot of activity downloading &lt;a href="http://microsoft.com/visualstudio"&gt;Visual Studio&lt;/a&gt; and the many SDKs that you can install to expand its capabilities.&lt;/p&gt;  &lt;p&gt;The purpose of this post is to direct you to the most streamlined way of downloading the latest versions of .NET, Visual Studio Express Editions and SDKs. By “SDKs”, I mean SDK releases like Windows Azure and Windows Phone. Again, it is clear from the traffic data that a lot of you are after those downloads, to start building apps for the various platforms that Microsoft offers to you. However, it appears that many of would probably appreciate a more streamlined way to achieve your download tasks. &lt;/p&gt;  &lt;h3&gt;Most popular .NET Framework downloads&lt;/h3&gt;  &lt;p&gt;To start off, I’m including download links to the most popular .NET Framework downloads. &lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://go.microsoft.com/fwlink/?LinkId=255943"&gt;.NET Framework 4.5&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://www.microsoft.com/download/details.aspx?id=17851"&gt;.NET Framework 4&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://www.microsoft.com/download/details.aspx?id=22"&gt;.NET Framework 3.5 SP1&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;h3&gt;Just in time download&lt;/h3&gt;  &lt;p&gt;As part of Visual Studio 2010, we added a new experience to help you find the products that you want to download, right when you want them. We improved on this within Visual Studio 2012, however, the general experience is the same. You’ve probably seen this option before, and may have taken advantage of it. &lt;/p&gt;  &lt;p&gt;The experience within Visual Studio takes you to a web page, specific to that version of Visual Studio. These pages list the additional downloads that are available to you, to expand your development options. If you notice that a product download is missing from that list, please tell us! Here are links to those two pages:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;.&lt;a href="http://msdn.microsoft.com/en-US/hh487283.aspx"&gt;NET Framework Multi-Targeting Packs and SDKs for Visual Studio 2012&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-US/hh487282.aspx"&gt;.NET Framework Multi-Targeting Packs and SDKs for Visual Studio 2010&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Here is a partial screenshot of the page for Visual Studio 2012, to give you a quick idea of what you can expect from these pages. &lt;/p&gt;  &lt;p&gt;&lt;img title="clip_image001" style="border: 0px currentcolor; margin-right: auto; margin-left: auto; float: none; display: block; background-image: none;" border="0" alt="clip_image001" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-34-metablogapi/6305.clip_5F00_image001_5F00_70819CD3.png" width="644" height="688" /&gt;&lt;/p&gt;  &lt;p&gt;You can bookmark those links for later use. As I said earlier, we integrated getting to these pages into Visual Studio. You can think of these pages as an extension of Visual Studio. Let’s take a look at the various places within Visual Studio 2012 and Visual Studio 2010 that enable you to quickly get to these pages. I’ll show all of the experiences available, always showing the Visual Studio 2012 experience first. Feel free to try them all.&lt;/p&gt;  &lt;h4&gt;New Project Dialog&lt;/h4&gt;  &lt;p&gt;Selecting “&lt;b&gt;More Frameworks&lt;/b&gt;” will take you to the .NET Framework Multi-Targeting Packs and SDKs download page.&lt;/p&gt;  &lt;p&gt;&lt;img title="clip_image002" style="border: 0px currentcolor; margin-right: auto; margin-left: auto; float: none; display: block; background-image: none;" border="0" alt="clip_image002" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-34-metablogapi/7875.clip_5F00_image002_5F00_1030769C.png" width="602" height="340" /&gt;&lt;/p&gt;  &lt;p&gt;&lt;img title="clip_image003" style="border: 0px currentcolor; margin-right: auto; margin-left: auto; float: none; display: block; background-image: none;" border="0" alt="clip_image003" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-34-metablogapi/5722.clip_5F00_image003_5F00_5B23E76B.png" width="593" height="356" /&gt;&lt;/p&gt;  &lt;h4&gt;Target Framework in Project Properties&lt;/h4&gt;  &lt;p&gt;Selecting “&lt;b&gt;Install&lt;/b&gt; &lt;b&gt;other Frameworks&lt;/b&gt;” will take you to the .NET Framework Multi-Targeting Packs and SDKs download page.&lt;/p&gt;  &lt;p&gt;&lt;img title="clip_image004" style="border: 0px currentcolor; margin-right: auto; margin-left: auto; float: none; display: block; background-image: none;" border="0" alt="clip_image004" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-34-metablogapi/4336.clip_5F00_image004_5F00_4FFA5D21.png" width="530" height="387" /&gt;&lt;/p&gt;  &lt;p&gt;&lt;img title="clip_image005" style="border: 0px currentcolor; margin-right: auto; margin-left: auto; float: none; display: block; background-image: none;" border="0" alt="clip_image005" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-34-metablogapi/2273.clip_5F00_image005_5F00_2FDF5064.png" width="511" height="405" /&gt;&lt;/p&gt;  &lt;h4&gt;Add or Change Target Framework for a Portable Class Library&lt;/h4&gt;  &lt;p&gt;Selecting “&lt;b&gt;Install&lt;/b&gt; &lt;b&gt;additional Frameworks&lt;/b&gt;” will take you to the .NET Framework Multi-Targeting Packs and SDKs download page.&lt;/p&gt;  &lt;p&gt;I only provided one set of screenshots since the UI is almost identical between Visual Studio 2012 and 2010.&lt;/p&gt;  &lt;p&gt;&lt;img title="clip_image006" style="border: 0px currentcolor; margin-right: auto; margin-left: auto; float: none; display: block; background-image: none;" border="0" alt="clip_image006" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-34-metablogapi/5635.clip_5F00_image006_5F00_7AD2C133.png" width="385" height="373" /&gt;&lt;/p&gt;  &lt;p&gt;&lt;img title="clip_image007" style="border: 0px currentcolor; margin-right: auto; margin-left: auto; float: none; display: block; background-image: none;" border="0" alt="clip_image007" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-34-metablogapi/3582.clip_5F00_image007_5F00_6FA936E9.png" width="643" height="535" /&gt;&lt;/p&gt;  &lt;h3&gt;Popular Downloads are available at the .NET Framework Dev Center on MSDN&lt;/h3&gt;  &lt;p&gt;The &lt;a href="http://msdn.com/netframework"&gt;.NET Framework dev center&lt;/a&gt; is another place where we make download links available. In the screenshot below, you can see a set of .NET tools, SDKS and Visual Studio Express Editions that you can download and install. &lt;/p&gt;  &lt;p&gt;&lt;img title="clip_image008" style="border: 0px currentcolor; margin-right: auto; margin-left: auto; float: none; display: block; background-image: none;" border="0" alt="clip_image008" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-34-metablogapi/0447.clip_5F00_image008_5F00_21A0D774.png" width="729" height="427" /&gt;&lt;/p&gt;  &lt;p&gt;On another part of the &lt;a href="http://msdn.com/netframework"&gt;.NET Framework dev center&lt;/a&gt; page, you can find a list of .NET Framework downloads and NuGet Packages&lt;/p&gt;  &lt;p&gt;&lt;img title="clip_image009" style="border: 0px currentcolor; margin-right: auto; margin-left: auto; float: none; display: block; background-image: none;" border="0" alt="clip_image009" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-34-metablogapi/1018.clip_5F00_image009_5F00_2F731D6F.png" width="334" height="580" /&gt;&lt;/p&gt;  &lt;p&gt;You can also find a lot of other useful links on the &lt;a href="http://msdn.microsoft.com/vstudio"&gt;Visual Studio dev center&lt;/a&gt; page.&lt;/p&gt;  &lt;p&gt;&lt;img title="clip_image010" style="border: 0px currentcolor; margin-right: auto; margin-left: auto; float: none; display: block; background-image: none;" border="0" alt="clip_image010" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-34-metablogapi/1031.clip_5F00_image010_5F00_7A668E3E.png" width="629" height="409" /&gt;&lt;/p&gt;  &lt;h3&gt;Wrapping Up&lt;/h3&gt;  &lt;p&gt;The opportunities for .NET developers seems to grow every year. It is easy to target each of the Microsoft platforms with your existing skills and code. You can take the first step by downloading a new version of .NET, an SDK for Visual Studio or a Visual Studio Edition. &lt;/p&gt;  &lt;p&gt;Once again, if you are looking for a .NET version, SDK or tool, take a look at one of these pages:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;.&lt;a href="http://msdn.microsoft.com/en-US/hh487283.aspx"&gt;NET Framework Multi-Targeting Packs and SDKs for Visual Studio 2012&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-US/hh487282.aspx"&gt;.NET Framework Multi-Targeting Packs and SDKs for Visual Studio 2010&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;We intend for these pages to be complete. Please do tell us we’re missing some important Microsoft SDKs or tools for .NET that you feel should be on this page.&lt;/p&gt;  &lt;p&gt;Follow us on Twitter (&lt;a href="https://twitter.com/dotnet"&gt;@dotnet&lt;/a&gt;) and Facebook (&lt;a href="http://facebook.com/dotnet"&gt;dotnet&lt;/a&gt;). You can follow other .NET teams, too: &lt;a href="http://twitter.com/aspnet"&gt;@aspnet&lt;/a&gt;/&lt;a href="http://facebook.com/asp.net"&gt;asp.net&lt;/a&gt;, &lt;a href="https://twitter.com/efmagicunicorns"&gt;@efmagicunicorns&lt;/a&gt;/&lt;a href="https://www.facebook.com/efmagicunicorns"&gt;efmagicunicorns&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=10378831" width="1" height="1"&gt;</description></item><item><title>Talking about .NET in the Community Press</title><link>http://blogs.msdn.com/b/dotnet/archive/2012/12/12/talking-about-net-in-the-community-press.aspx</link><pubDate>Wed, 12 Dec 2012 23:35:12 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10376997</guid><dc:creator>Brandon Bray</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/dotnet/rsscomments.aspx?WeblogPostID=10376997</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/dotnet/commentapi.aspx?WeblogPostID=10376997</wfw:comment><comments>http://blogs.msdn.com/b/dotnet/archive/2012/12/12/talking-about-net-in-the-community-press.aspx#comments</comments><description>&lt;p&gt;We follow a lot of blogs and digital papers relating to both .NET and also technology in general. There is a lot of great content that gets published every day about .NET. We’re starting a new series to publish these great links, doing our part to help give that content a larger audience.&lt;/p&gt;  &lt;p&gt;For this post, I wanted to call out some of the journalists in the community press that have been regularly including links to our posts. We’ve noticed the traffic from your domains! Thanks for expanding awareness to our blog and many other great .NET blogs. &lt;/p&gt;  &lt;p&gt;&lt;b&gt;Paper.Li&lt;/b&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://paper.li/dustin551/1348281219"&gt;Attack of the Stack&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://paper.li/kirtidarji_02/1309284626"&gt;DotnetTips Daily&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://paper.li/nithinmohantk/1306294298"&gt;The Microsoft Azure Times Daily&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://paper.li/dotnetvalley"&gt;The .NET Valley Daily&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://paper.li/mirkopastorelli/1336995277"&gt;The Pastorelli Software Daily&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://paper.li/LACanuck/1337967529"&gt;Today’s Developer&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;b&gt;Blogs&lt;/b&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://www.alvinashcraft.com/"&gt;Alvin Ashcraft's Morning Dew&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://danrigby.com/"&gt;Dan Rigby’s Blog&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://jasonhaley.com/blog/"&gt;Jason Haley Blog&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://themorningbrew.net/"&gt;The Morning Brew&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;If you are not familiar with these papers and blogs, please do visit them.&lt;/p&gt;  &lt;p&gt;Follow us on Twitter (&lt;a href="https://twitter.com/dotnet"&gt;@dotnet&lt;/a&gt;) and Facebook (&lt;a href="http://facebook.com/dotnet"&gt;dotnet&lt;/a&gt;). You can follow other .NET teams, too: &lt;a href="http://twitter.com/aspnet"&gt;@aspnet&lt;/a&gt;/&lt;a href="http://facebook.com/asp.net"&gt;asp.net&lt;/a&gt;, &lt;a href="https://twitter.com/efmagicunicorns"&gt;@efmagicunicorns&lt;/a&gt;/&lt;a href="https://www.facebook.com/efmagicunicorns"&gt;efmagicunicorns&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=10376997" width="1" height="1"&gt;</description></item><item><title>Asynchronous Programming for Windows Store Apps: .NET is up to the Task</title><link>http://blogs.msdn.com/b/dotnet/archive/2012/11/30/asynchronous-programming-for-windows-store-apps-net-is-up-to-the-task.aspx</link><pubDate>Fri, 30 Nov 2012 15:07:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10373135</guid><dc:creator>Brandon Bray</dc:creator><slash:comments>12</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/dotnet/rsscomments.aspx?WeblogPostID=10373135</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/dotnet/commentapi.aspx?WeblogPostID=10373135</wfw:comment><comments>http://blogs.msdn.com/b/dotnet/archive/2012/11/30/asynchronous-programming-for-windows-store-apps-net-is-up-to-the-task.aspx#comments</comments><description>&lt;blockquote&gt;   &lt;p&gt;&lt;i&gt;Programming with async and await is the driving force behind delivering “fast and fluid” user experiences. Compiler diagnostics for async APIs guide developers towards best practices – it’s a wonderful example of platform and tooling working together. Since the Windows Runtime uses a slightly different implementation for async programming than .NET, &lt;b&gt;Greg Paperin – a developer on the .NET base class libraries&lt;/b&gt; – explains how C# and Visual Basic still provide a unified development experience between the two sets of APIs. –Brandon&lt;/i&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;h3&gt;Asynchronous Programming in Windows Runtime APIs&lt;/h3&gt;  &lt;p&gt;Asynchronous programming is one of the central themes of the .NET Framework 4.5 and Visual Studio 2012 releases. The async and await keywords in C# 5 and Visual Basic 11 make writing and consuming asynchronous code easier than ever before. Visual Studio 2012 makes it easier to write and debug asynchronous code. And the .NET Framework 4.5 completes the Async story with new Task-based “XyzAsync” APIs, expansions of the Task Parallel Library and performance enhancements. We have blogged about some of these features recently (e.g. &amp;quot;&lt;a href="http://blogs.msdn.com/b/dotnet/archive/2012/04/03/async-in-4-5-worth-the-await.aspx"&gt;Async in 4.5: Worth the Await&lt;/a&gt;&amp;quot; and &amp;quot;&lt;a href="http://blogs.msdn.com/b/dotnet/archive/2012/06/06/async-in-4-5-enabling-progress-and-cancellation-in-async-apis.aspx"&gt;Enabling Progress and Cancellation in Async APIs&lt;/a&gt;&amp;quot;).&lt;/p&gt;  &lt;p&gt;The Windows Runtime (WinRT) has also adopted the asynchronous programming model. The WinRT APIs, introduced in Windows 8, are central to writing Windows Store apps. Most WinRT APIs that can have an execution time that is noticeable to a human user are asynchronous, and synchronous alternatives are not available. These choices were made to ensure that developers consistently picked APIs that deliver great end-user experiences. &lt;/p&gt;  &lt;p&gt;WinRT async APIs are different than the .NET Task-based async APIs. However, you can use WinRT async APIs as seamlessly as pure .NET APIs, with the same keywords and the same usage principles. In this article we will go into detail on how you can do that.&lt;/p&gt;  &lt;p&gt;We will begin with an overview of the most important types in the WinRT async infrastructure, and how they relate to similar .NET concepts. After that we will walk over creating a complete Windows Store App with several different asynchronous operations. We will learn how to start different kinds of asynchronous operations, how to retrieve results, and how to work with cancellations and progress monitoring. &lt;/p&gt;  &lt;p&gt;This article assumes that you have a basic familiarity with using the async and await keywords with .NET APIs in C#. If you do not, try reading some of our previous articles on this topic first (&lt;a href="http://blogs.msdn.com/b/dotnet/archive/2012/04/03/async-in-4-5-worth-the-await.aspx"&gt;introductory article&lt;/a&gt;; &lt;a href="http://blogs.msdn.com/b/dotnet/archive/2012/06/06/async-in-4-5-enabling-progress-and-cancellation-in-async-apis.aspx"&gt;details on cancellation and on progress monitoring&lt;/a&gt;).&lt;/p&gt;  &lt;h3&gt;The WinRT Asynchronous model for .NET programmers&lt;/h3&gt;  &lt;p&gt;Before we begin, let us take a look at one example of calling an asynchronous WinRT API. The following statement fetches a list of files in the local user's video library folder.&lt;/p&gt;  &lt;div id="codeSnippetWrapper"&gt;   &lt;pre id="codeSnippet" style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &amp;quot;Courier New&amp;quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"&gt;IReadOnlyList&amp;lt;StorageFile&amp;gt; videoFiles = await KnownFolders.VideosLibrary.GetFilesAsync();&lt;/pre&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;Remarkably, GetFilesAsync() is not a .NET Task-based method, but a &lt;a href="http://msdn.microsoft.com/en-us/library/windows/apps/br227276.aspx"&gt;WinRT asynchronous API&lt;/a&gt;:&lt;/div&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;pre id="codeSnippet" style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &amp;quot;Courier New&amp;quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;interface&lt;/span&gt; IStorageFolder : IStorageItem {&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;// ... &lt;/span&gt;&lt;br /&gt;IAsyncOperation&amp;lt;IReadOnlyList&amp;lt;StorageFile&amp;gt;&amp;gt; GetFilesAsync();&lt;br /&gt;}&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;And yet, we use it just like a first class Task-based asynchronous method: with the await keyword. Intrigued? Let's get started.&lt;/p&gt;

&lt;h4&gt;&lt;a name="_Ref333590457"&gt;Key WinRT Async types&lt;/a&gt;&lt;/h4&gt;

&lt;p&gt;In the .NET Framework 4.5, asynchronous await-able APIs return an instance of type Task or Task&amp;lt;TResult&amp;gt;. You can think of a Task object as a receipt certifying that you requested an operation to be undertaken. You hold on to the receipt and use it to determine if and when the operation has completed, whether any errors occurred and what the result was. &lt;/p&gt;

&lt;p&gt;You can use a Task in an await statement to ensure that subsequent statements are executed after the work represented by this &amp;quot;receipt&amp;quot; has completed and the result became available, without blocking. Operations that have a result (e.g., read some data from a stream) return an object of type Task&amp;lt;TResult&amp;gt;, and actions that cause an effect, but do not have a result return an object of type Task (e.g. send some bytes over a wire). &lt;/p&gt;

&lt;p&gt;The proper computer science term for this &amp;quot;receipt&amp;quot;-concept is &lt;i&gt;future&lt;/i&gt;, because the receipt (Task) represents an operation that may be completed in the future. (Note: &lt;i&gt;Futures&lt;/i&gt; are alternatively known as &lt;i&gt;promises&lt;/i&gt; because they represent a promise to complete an operation; some people also make subtle differences between the meaning of &lt;i&gt;futures&lt;/i&gt; and &lt;i&gt;promises&lt;/i&gt;, but that detail is not relevant for our current purposes.)&lt;/p&gt;

&lt;p&gt;WinRT also has a concept of &lt;i&gt;futures&lt;/i&gt;, however it does not use the same types as .NET to represent them. A future type in WinRT must implement one of the four interfaces IAsyncAction, IAsyncActionWithProgress, IAsyncOperation, IAsyncOperationWithProgress, and all of these four interfaces inherit from one common interface IAsyncInfo. The following diagram describes the four interfaces and lists their public APIs (see also the &lt;a href="http://msdn.microsoft.com/en-us/library/windows/apps/br226021.aspx"&gt;API reference on Windows Dev Center&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;&lt;img title="image" style="border: 0px currentcolor; margin-right: auto; margin-left: auto; float: none; display: block; background-image: none;" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-34-metablogapi/6242.image_5F00_708AA494.png" width="478" height="451" /&gt;&lt;/p&gt;

&lt;p align="center"&gt;WinRT types for representing futures&lt;/p&gt;

&lt;p align="center"&gt;&lt;/p&gt;

&lt;h4&gt;Relationship between key Async types in WinRT and .NET&lt;/h4&gt;

&lt;p&gt;These four WinRT types have a close correspondence to tasks in .NET: &lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;IAsyncAction and IAsyncActionWithProgress&amp;lt;TProgress&amp;gt; describe an asynchronous action that does not return anything directly – they correspond to Task. &lt;/li&gt;

  &lt;li&gt;IAsyncOperation&amp;lt;TResult&amp;gt; and IAsyncOperationWithProgress&amp;lt;TResult, TProgress&amp;gt; represent an asynchronous operation that returns a value of type TResult – they correspond to Task&amp;lt;TResult&amp;gt;. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The progress-related versions are identical to each respective interface with the additional ability to publish the progress of an on-going asynchronous operation. We will overlook this detail for now and come back to it later in the section &amp;quot;&lt;u&gt;Cancellation and Progress in WinRT asynchronous operations&lt;/u&gt;&lt;u&gt;&amp;quot;&lt;/u&gt;. The following figure illustrates the conceptual relationship between tasks and WinRT Async Infos.&lt;/p&gt;

&lt;p&gt;&lt;img title="image" style="border: 0px currentcolor; margin-right: auto; margin-left: auto; float: none; display: block; background-image: none;" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-34-metablogapi/2133.image_5F00_45460D8D.png" width="518" height="253" /&gt;&lt;/p&gt;

&lt;p align="center"&gt;Conceptual relationship between asynchronous actions and operations in WinRT and .NET&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;.NET also has the IProgress interface used to monitor the progress of an asynchronous operation and the &lt;a href="http://msdn.microsoft.com/library/system.threading.cancellationtoken.aspx"&gt;CancellationToken&lt;/a&gt;/&lt;a href="http://msdn.microsoft.com/library/System.Threading.CancellationTokenSource.aspx"&gt;CancellationTokenSource&lt;/a&gt; tandem used to signal that a consumer requested an operation to cancel. WinRT does not have corresponding types. Instead, cancellation and progress monitoring is achieved using member APIs of the four IAsyncXxxx types.&lt;/p&gt;

&lt;p&gt;You may wonder about the reason behind these differences between .NET and WinRT Async types. Why not just use Task? Remember that WinRT APIs are available in languages that do not use the .NET Common Language Runtime to execute. For instance, you can write Windows Store apps in non-.NET languages such as JavaScript and native C++, and all WinRT types need to be available there too. &lt;/p&gt;

&lt;p&gt;Task is a CLR type and is only available in languages that rely on the .NET runtime. This is why WinRT introduces its own asynchrony abstractions and .NET makes sure that these abstractions can be seamlessly imported into the Task-based universe while still accounting for their distinctive features. What does this look like? Let's see it first-hand!&lt;/p&gt;

&lt;h4&gt;&lt;a name="_Ref339043824"&gt;Awaiting WinRT Async APIs like Tasks&lt;/a&gt;&lt;/h4&gt;

&lt;p&gt;Recall the earlier example where we used await with the WinRT GetFilesAsync() method that returns an instance of &lt;b&gt;IAsyncOperation&lt;/b&gt;&amp;lt;IReadOnlyList&amp;lt;StorageFile&amp;gt;&amp;gt; and not of &lt;b&gt;Task&lt;/b&gt;&amp;lt;IReadOnlyList&amp;lt;StorageFile&amp;gt;&amp;gt;.&lt;/p&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;pre id="codeSnippet" style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &amp;quot;Courier New&amp;quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"&gt;await KnownFolders.VideosLibrary.GetFilesAsync();&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;If we dig deeper into the &lt;a href="http://blogs.msdn.com/b/pfxteam/archive/2012/04/12/10293335.aspx"&gt;workings of the &amp;quot;await&amp;quot; keyword&lt;/a&gt; we learn that it causes the GetAwaiter() method to be called on the awaited entity. Although the IAsyncOperation&amp;lt;TResult&amp;gt; interface returned from GetFilesAsync() does not define such a method, an equivalent extension method is defined by the .NET Framework in the System.Runtime.WindowsRuntime assembly. This extension method creates a Task that represents the specified WinRT async operation in the Task-based universe and simply uses the task's already existing GetAwaiter() infrastructure. For that, it uses the AsTask() extension method, also defined in the System.Runtime.WindowsRuntime assembly. Here is a simplified implementation of the GetAwaiter() extension method:&lt;/p&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;pre id="codeSnippet" style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &amp;quot;Courier New&amp;quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;static&lt;/span&gt; TaskAwaiter GetAwaiter&amp;lt;TResult&amp;gt;(&lt;span style="color: rgb(0, 0, 255);"&gt;this&lt;/span&gt; IAsyncOperation&amp;lt;TResult&amp;gt; source) {&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt; AsTask(source).GetAwaiter();&lt;br /&gt;}&lt;/pre&gt;

  &lt;br /&gt;The bulk of the logic that allows the .NET and WinRT asynchronous models to interoperate lies within the AsTask(..) extension method. It is a public method that you can use directly, however, in many cases the compiler will make sure it is called automatically on your behalf. Nevertheless, in some scenarios you will need to call AsTask(..) explicitly. We will discuss an example of that in section &amp;quot;&lt;u&gt;Cancellation and Progress in WinRT asynchronous operations&lt;/u&gt;&amp;quot;.&lt;/div&gt;

&lt;h3&gt;Using WinRT Async APIs in a Windows Store App based on .NET&lt;/h3&gt;

&lt;p&gt;Let's walk through writing a small Windows Store app that makes use of several different asynchronous operations. We will use WinRT APIs to create a video file format converter – an application that lets the user select a .WMV file, converts it to an .MP4 file and stores it into the local video library.&lt;/p&gt;

&lt;p&gt;We will focus on the async APIs and will make due with a very simple UI. Our app looks like this:&lt;/p&gt;

&lt;p&gt;&lt;img title="image" style="border: 0px currentcolor; margin-right: auto; margin-left: auto; float: none; display: block; background-image: none;" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-34-metablogapi/1537.image_5F00_773DAE17.png" width="447" height="332" /&gt;&lt;/p&gt;

&lt;p align="center"&gt;Sample video converter Windows Store App user interface after start-up&lt;/p&gt;

&lt;h4&gt;Asynchronous interactions with the user&lt;/h4&gt;

&lt;p&gt;The first thing our app needs to be able to do is to pick an input video file for conversion. Similar to traditional Windows desktop applications, there is an API for displaying a prompt for picking a file from the disk. However, in contrast to classic desktop applications we do not need to deal with modal dialog windows. &lt;/p&gt;

&lt;p&gt;Asynchrony is all about thinking in terms of the operations that need to occur and the results of these operations, while abstracting away when the operation is actually performed (the system decides that for you). WinRT allows us to say &amp;quot;ask the user to pick a video file and use the result when it becomes available&amp;quot;, and the async infrastructure will take care of displaying the input screen at the right time. &lt;/p&gt;

&lt;p&gt;Note that the WinRT file picker takes up the full screen, but it is not modal: it does not block other UI components from working, even though you may not see them on the screen since they are covered. How many of you had to deal with annoying deadlocks caused by modal input dialogs? Asynchronous user input APIs solve this problem! &lt;/p&gt;

&lt;p&gt;The pattern for picking an input file looks like this:&lt;/p&gt;

&lt;div id="codeSnippetWrapper" style="margin: 20px 0px 10px; padding: 4px; border: 1px solid silver; width: 97.5%; text-align: left; line-height: 12pt; overflow: auto; font-family: &amp;quot;Courier New&amp;quot;, courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: rgb(244, 244, 244);"&gt;
  &lt;pre id="codeSnippet" style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &amp;quot;Courier New&amp;quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"&gt;&lt;p&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;// Create a file picker component:&lt;/span&gt;&lt;br /&gt;FileOpenPicker picker = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; Windows.Storage.Pickers.FileOpenPicker();&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;// Configure the file picker:&lt;/span&gt;&lt;br /&gt;picker.SuggestedStartLocation =&lt;br /&gt;Windows.Storage.Pickers.PickerLocationId.VideosLibrary;&lt;br /&gt;picker.FileTypeFilter.Add(&lt;span style="color: rgb(0, 96, 128);"&gt;&amp;quot;.wmv&amp;quot;&lt;/span&gt;);&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;// Request the system to ensure that the user has picked a file:&lt;/span&gt;&lt;br /&gt;StorageFile file = await picker.PickSingleFileAsync();&lt;br /&gt;&lt;/p&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;&lt;/span&gt;&lt;p&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;// The system will get to this line once the user has picked a file,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;// however long this might take.&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;// However, the UI thread does not block and can continue executing other&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;// work in the meantime.&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;// Now the picked file can be used:&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (file == &lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;) {&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;// ...&lt;/span&gt;&lt;br /&gt;} &lt;span style="color: rgb(0, 0, 255);"&gt;else&lt;/span&gt; {&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;// ...&lt;/span&gt;&lt;br /&gt;}&lt;/p&gt;&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;The &lt;a href="http://msdn.microsoft.com/library/windows/apps/BR207852"&gt;PickSingleFileAsync&lt;/a&gt; method delivers the magic that resolves the modal UI problem. Let's take a look at its signature:&lt;/p&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;pre id="codeSnippet" style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &amp;quot;Courier New&amp;quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;sealed&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;class&lt;/span&gt; FileOpenPicker {&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;//... &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; IAsyncOperation&amp;lt;StorageFile&amp;gt; PickSingleFileAsync();&lt;br /&gt;}&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Note that it does not return a Task. Instead, it returns an IAsyncOperation object. However, you can use it with await just like as if it was a Task. Neat, isn't it? By the time you finished reading this article you will gain a general understanding of how this works.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p align="center"&gt;&lt;b&gt;Asynchronous WinRT operations can be awaited 
      &lt;br /&gt;&lt;/b&gt;&lt;b&gt;in the same manner as managed Task-based operations.&lt;/b&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;To complete the picture, here is the complete method body for the &amp;quot;Pick input video&amp;quot;-button handler:&lt;/p&gt;

&lt;div id="codeSnippetWrapper" style="margin: 20px 0px 10px; padding: 4px; border: 1px solid silver; width: 97.5%; text-align: left; line-height: 12pt; overflow: auto; font-family: &amp;quot;Courier New&amp;quot;, courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: rgb(244, 244, 244);"&gt;
  &lt;pre id="codeSnippet" style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &amp;quot;Courier New&amp;quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"&gt;&lt;p&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;private&lt;/span&gt; async &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; PickInputButton_Click(Object sender, RoutedEventArgs e) {&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;// prepare the UI:&lt;/span&gt;&lt;br /&gt;Log(&lt;span style="color: rgb(0, 96, 128);"&gt;&amp;quot;Picking input file.&amp;quot;&lt;/span&gt;);&lt;br /&gt;PageState prevPageState = DisableButtons();&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;// Create and configure the file picker component:&lt;/span&gt;&lt;br /&gt;FileOpenPicker picker = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; Windows.Storage.Pickers.FileOpenPicker();&lt;br /&gt;picker.SuggestedStartLocation =&lt;br /&gt;Windows.Storage.Pickers.PickerLocationId.VideosLibrary;&lt;br /&gt;picker.FileTypeFilter.Add(&lt;span style="color: rgb(0, 96, 128);"&gt;&amp;quot;.wmv&amp;quot;&lt;/span&gt;);&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;// Request the file picker to query the user for an input file asynchronously:&lt;/span&gt;&lt;br /&gt;StorageFile file = await picker.PickSingleFileAsync();&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;// If no file was picked (e.g. picker was aborted), do nothing:&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (file == &lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;) {&lt;br /&gt;Log(&lt;span style="color: rgb(0, 96, 128);"&gt;&amp;quot;Input file selection cancelled.&amp;quot;&lt;/span&gt;);&lt;br /&gt;SetPageState(prevPageState);&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt;;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;// If a file was picked, store the file and enable Start Conversion button:&lt;/span&gt;&lt;br /&gt;Log(&lt;span style="color: rgb(0, 96, 128);"&gt;&amp;quot;Input file selected: &amp;quot;&lt;/span&gt; + file.Path);&lt;br /&gt;_inputFile = file;&lt;br /&gt;SetPageState(PageState.ReadyToConvert);&lt;br /&gt;InputFileName.Text = file.Path;&lt;br /&gt;}&lt;br /&gt;&lt;/p&gt;&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;h4&gt;I/O-bound asynchronous operations&lt;/h4&gt;

&lt;p&gt;In the previous section we saw how to use await to make sure that your Windows Store app is not blocked while the user is performing some input. Another event that may cause an application to become unresponsive is data input and output. While some I/O operations are fast (think reading a few bytes from a modern solid state drive), others can stall your applications for a significant time (think spinning up a disk drive, accessing a network location or a web service, or just reading a very large file). &lt;/p&gt;

&lt;p&gt;In our case we need to transcode a video file. At the very least, it involves reading the entire file and writing the transcoded version back to disk, and you know that video files can very quickly grow up to many megabytes, if not gigabytes. To ensure that your applications remain responsive, all I/O bound operations in WinRT are asynchronous, including video transcoding. Let us see how this is done.&lt;/p&gt;

&lt;p&gt;The transcoding operation consists of two stages. We need to first determine if a specific input file can be converted to a specific output format. In our case, we want to convert a WMV file to MP4. To achieve this, WinRT provides a universal transcoder component that can answer these questions. The transcoder component will examine the required format information and the input file on the disk and it will report whether a conversion is possible by returning a &lt;a href="http://msdn.microsoft.com/library/windows/apps/hh700941.aspx"&gt;PrepareTranscodeResult&lt;/a&gt; object. This process can take a couple of milliseconds up to a few seconds.&lt;/p&gt;

&lt;p&gt;Second, if the conversion is indeed possible, we request from the PrepareTranscodeResult object to actually perform the conversion. At that stage, the entire input file will be read and the output file will be written. Depending on the length of the video, this process can take several minutes.&lt;/p&gt;

&lt;p&gt;The coding pattern for this looks like this:&lt;/p&gt;

&lt;div id="codeSnippetWrapper" style="margin: 20px 0px 10px; padding: 4px; border: 1px solid silver; width: 97.5%; text-align: left; line-height: 12pt; overflow: auto; font-family: &amp;quot;Courier New&amp;quot;, courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: rgb(244, 244, 244);"&gt;
  &lt;pre id="codeSnippet" style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &amp;quot;Courier New&amp;quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;// Create the transcoder component and specify the output video file format:&lt;/span&gt;&lt;br /&gt;MediaTranscoder transcoder = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; MediaTranscoder();&lt;br /&gt;MediaEncodingProfile profile=MediaEncodingProfile.CreateMp4(VideoEncodingQuality.Vga);&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;// Analyze input file and output format to determine if conversion if possible:&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;// (we do this asynchronously, so the app does not freeze)&lt;/span&gt;&lt;br /&gt;PrepareTranscodeResult transcode =&lt;br /&gt;await transcoder.PrepareFileTranscodeAsync(_inputFile, outputFile, profile);&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;// When we continue here, the transcode result is already available.&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (!transcode.CanTranscode) {&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;// Transcoding of the provided input file into the specified output&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;// format is not supported. Report and give up.&lt;/span&gt;&lt;br /&gt;} &lt;span style="color: rgb(0, 0, 255);"&gt;else&lt;/span&gt; {&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;// Transcoding is possible. Call transcode.TranscodeAsync() to perform conversion. &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;// We will see soon how this is done.&lt;/span&gt;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;h4&gt;&lt;a name="_Ref334015563"&gt;Cancellation and Progress in WinRT asynchronous operations&lt;/a&gt;&lt;/h4&gt;

&lt;p&gt;In the previous section, we used the await keyword with the I/O bound asynchronous operation &lt;a href="http://msdn.microsoft.com/library/windows/apps/windows.media.transcoding.mediatranscoder.preparefiletranscodeasync.aspx"&gt;PrepareFileTranscodeAsync&lt;/a&gt;(). Because it is a WinRT method, rather than returning a Task, it returns a WinRT asynchronous type; in this case, IAsyncOperation&amp;lt;PrepareTranscodeResult&amp;gt;. Notably, PrepareFileTranscodeAsync does not need to do much work to provide its result – the system only needs to examine a fraction of the input file to understand its format. Even if the user changes their mind right after clicking the conversion button, they can wait until the operation is completed. &lt;/p&gt;

&lt;p&gt;However, the actual transcoding (achieved by the PrepareTranscodeResult.TranscodeAsync() API) can take many minutes depending on the length of the video file. A user may have every reason to want to cancel the on-going conversion process. Moreover, a user-friendly application will certainly want to communicate the progress of the operation to the user: After all, wouldn't you want to know whether you have the time for an instant coffee, or whether you have enough time to prepare a nice latte? Or perhaps even enough time to finally clean those dishes? No, you don't want to do &lt;i&gt;that&lt;/i&gt;! See – the cancel feature &lt;i&gt;is&lt;/i&gt; essential.&lt;/p&gt;

&lt;p&gt;As discussed in our &lt;a href="http://blogs.msdn.com/b/dotnet/archive/2012/06/06/async-in-4-5-enabling-progress-and-cancellation-in-async-apis.aspx"&gt;previous article on Async&lt;/a&gt;, Task-based asynchronous APIs that support cancellation and/or progress monitoring have overloads that take &lt;a href="http://msdn.microsoft.com/en-us/library/system.threading.cancellationtoken.aspx"&gt;CancellationToken&lt;/a&gt; and/or &lt;a href="http://msdn.microsoft.com/en-us/library/hh138298%28v=vs.110%29.aspx"&gt;IProgress&amp;lt;TProgress&amp;gt;&lt;/a&gt; parameters respectively. But if we inspect the WinRT &lt;a href="http://msdn.microsoft.com/en-us/library/windows/apps/windows.media.transcoding.preparetranscoderesult.transcodeasync.aspx"&gt;PrepareTranscodeResult&lt;/a&gt; class looking for overloads of &lt;a href="http://msdn.microsoft.com/en-us/library/windows/apps/windows.media.transcoding.preparetranscoderesult.transcodeasync.aspx"&gt;TranscodeAsync&lt;/a&gt;, we cannot discover any trace of CancellationToken or IProgress. &lt;/p&gt;

&lt;p&gt;Recall that in section &amp;quot;&lt;u&gt;Key WinRT Async types&lt;/u&gt;&amp;quot; above we mentioned that in contrast to the Task-based universe, the cancellation control and the progress observer in WinRT are located directly on the &lt;i&gt;future&lt;/i&gt; types (IAsyncXzy). So how can we use them? We could use the object returned from TranscodeAsync() directly, instead of awaiting it. There is a Cancel() method we can call and a Progress delegate property we can set. In fact, this would work in many cases. However, this is a different thought model compared to Task-based APIs and this approach would mean that we needed to use different paradigms when consuming WinRT versus Task-based asynchronous APIs. Not ideal. Instead, we want to ensure an identical first-class experience for Task-based and WinRT futures.&lt;/p&gt;

&lt;p&gt;And indeed, such first-class experience is available. TranscodeAsync() returns an instance of IAsyncActionWithProgress&amp;lt;Double&amp;gt;. Let's consider what actually happens when the following line is evaluated:&lt;/p&gt;

&lt;div&gt;
  &lt;pre id="codeSnippet" style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &amp;quot;Courier New&amp;quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"&gt;await transcode.TranscodeAsync();&lt;/pre&gt;
&lt;/div&gt;

&lt;div&gt;
  &lt;br /&gt;Recall from section &amp;quot;&lt;u&gt;Awaiting WinRT Async APIs like Tasks&lt;/u&gt;&amp;quot; that the await keyword causes the compiler to emit a call to a GetAwaiter() method. The .NET Framework defines GetAwaiter() extension methods for all four WinRT async interface types in the System.Runtime.WindowsRuntime assembly. For instance, here is a simplified implementation of GetAwaiter() for &lt;/div&gt;

&lt;div&gt;IAsyncActionWithProgress&amp;lt;TProgress&amp;gt;:&lt;/div&gt;

&lt;div&gt;&amp;#160;&lt;/div&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;pre id="codeSnippet" style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &amp;quot;Courier New&amp;quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;static&lt;/span&gt; TaskAwaiter GetAwaiter&amp;lt;TProgress&amp;gt;(&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;this&lt;/span&gt; IAsyncActionWithProgress&amp;lt;TProgress&amp;gt; source) {&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt; AsTask(source).GetAwaiter();&lt;br /&gt;}&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;GetAwaiter() creates a Task that represents the specified WinRT async action in the Task-based universe and then uses the task's already existing GetAwaiter() infrastructure. The heavy lifting is done by the AsTask(..) extension method. It is a public method that you can use directly and it &lt;i&gt;does&lt;/i&gt; have overloads that support the .NET-style cancellation and progress infrastructure.&lt;/p&gt;

&lt;p&gt;The &lt;a href="http://msdn.microsoft.com/en-us/library/system.windowsruntimesystemextensions.astask.aspx"&gt;WindowsRuntimeSystemExtensions.AsTask()&lt;/a&gt; method family is a collection of factory methods that can take any of the four WinRT &lt;i&gt;future&lt;/i&gt; types and create a Task that represents that WinRT &lt;i&gt;future&lt;/i&gt; in the Task-based universe. In addition, AsTask factories can take CancellationToken and IProgress&amp;lt;TProgress&amp;gt; instances as parameters and appropriately connect them to the returned Task and thus to the underlying WinRT future. As a result, if a cancellation is requested using a token connected in this way, the cancellation request will be automatically forwarded to the WinRT operation by invoking its Cancel() method. Similarly, any progress updates published by the WinRT operation will be automatically forwarded to the specified progress observer.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p align="center"&gt;&lt;b&gt;The WindowsRuntimeSystemExtensions.AsTask() &lt;/b&gt;&lt;b&gt;conversion methods &lt;/b&gt;&lt;b&gt;create a Task that represents &lt;/b&gt;&lt;b&gt;a WinRT asynchronous action or operation in the Task-based .NET world.&lt;/b&gt;&lt;/p&gt;

  &lt;p align="center"&gt;&amp;#160; &lt;b&gt;These conversion methods support optional parameters to specify a CancellationToken for requesting that a WinRT asynchronous procedure be cancelled, and an IProgress&amp;lt;T&amp;gt; observer to monitor the progress of the asynchronous procedure.&lt;/b&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;The following code sample demonstrates how you can use mix-and-match the WinRT async model with the .NET model using AsTask(). &lt;/p&gt;

&lt;div id="codeSnippetWrapper" style="margin: 20px 0px 10px; padding: 4px; border: 1px solid silver; width: 97.5%; text-align: left; line-height: 12pt; overflow: auto; font-family: &amp;quot;Courier New&amp;quot;, courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: rgb(244, 244, 244);"&gt;
  &lt;pre id="codeSnippet" style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &amp;quot;Courier New&amp;quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"&gt;&lt;p&gt;CancellationTokenSource _cancelControl = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; CancellationTokenSource();&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;class&lt;/span&gt; TranscodeProgressHandler : IProgress&amp;lt;Double&amp;gt; {&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; Report(Double progressInfo) {&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;// Display a progress update to the user.&lt;/span&gt;&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;async &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; Transcode() {&lt;br /&gt;PrepareTranscodeResult transcode = . . .&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;try&lt;/span&gt; {&lt;br /&gt;await transcode.TranscodeAsync().AsTask(_cancelControl.Token,&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; TranscodeProgressHandler());&lt;br /&gt;} &lt;span style="color: rgb(0, 0, 255);"&gt;catch&lt;/span&gt; (OperationCanceledException) {&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;// There is no error.&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;// The transcoding simply responded to the cancellation request. Handle it.&lt;/span&gt;&lt;br /&gt;} &lt;span style="color: rgb(0, 0, 255);"&gt;catch&lt;/span&gt; (Exception ex) {&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;// An error has occurred. Handle the error.&lt;/span&gt;&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;/p&gt;&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;&lt;a href="http://blogs.msdn.com/b/dotnet/archive/2012/06/06/async-in-4-5-enabling-progress-and-cancellation-in-async-apis.aspx"&gt;Recall&lt;/a&gt; that in .NET, cancellation – although not an error – is an exceptional condition, and is handled using exceptions. So if you hook up a potential cancellation request, you need to be prepared to handle an &lt;a href="http://devdiv/sites/CLR/BCL/BCL Blog entries/msdn.microsoft.com/en-us/library/system.operationcanceledexception.aspx"&gt;OperationCanceledException&lt;/a&gt;, as shown in the code sample above.&lt;/p&gt;

&lt;p&gt;Let's now take a look at the full implementation of the &amp;quot;Start conversion&amp;quot; and the &amp;quot;Cancel conversion&amp;quot; button handlers and the relevant helper methods in our sample app:&lt;/p&gt;

&lt;div id="codeSnippetWrapper" style="margin: 20px 0px 10px; padding: 4px; border: 1px solid silver; width: 97.5%; text-align: left; line-height: 12pt; overflow: auto; font-family: &amp;quot;Courier New&amp;quot;, courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: rgb(244, 244, 244);"&gt;
  &lt;pre id="codeSnippet" style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &amp;quot;Courier New&amp;quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"&gt;&lt;p&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;// The cancellation controller used to cancel an ongoing transcoding.&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;// May be null when transcoding is idle:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;private&lt;/span&gt; CancellationTokenSource _transcodeCancelControl;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;/// &amp;quot;Cancel conversion&amp;quot;-button handler.&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;/// &amp;lt;param name=&amp;quot;sender&amp;quot;&amp;gt;Ignored.&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;/// &amp;lt;param name=&amp;quot;e&amp;quot;&amp;gt;Ignored&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;private&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; CancelConversionButton_Click(Object sender, RoutedEventArgs e) {&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;// If we have an initialised cancel controller, signal a cancellation request:&lt;/span&gt;&lt;br /&gt;CancellationTokenSource cancelControl =Volatile.Read(&lt;span style="color: rgb(0, 0, 255);"&gt;ref&lt;/span&gt; _transcodeCancelControl);&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (cancelControl != &lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;)&lt;br /&gt;cancelControl.Cancel();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;/// Initialises the output file for the currently selected output file.&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;/// &amp;lt;returns&amp;gt;an initialised output file&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;/// or &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt; if it could not be initialised.&amp;lt;/returns&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;private&lt;/span&gt; async Task&amp;lt;StorageFile&amp;gt; CreateOutputFile() {&lt;br /&gt;StorageFile outputFile = &lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;try&lt;/span&gt; {&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;// Construct the output file name / path:&lt;/span&gt;&lt;br /&gt;String inputPath = _inputFile.Path;&lt;br /&gt;String baseName = Path.GetFileNameWithoutExtension(inputPath);&lt;br /&gt;String extension = Path.GetExtension(inputPath);&lt;br /&gt;String outputFileName = baseName + &lt;span style="color: rgb(0, 96, 128);"&gt;&amp;quot;.ConversionOutput.mp4&amp;quot;&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;// Asynchronously initialise the output file in the pictures library:&lt;/span&gt;&lt;br /&gt;outputFile = await KnownFolders.VideosLibrary.CreateFileAsync(outputFileName,&lt;br /&gt;CreationCollisionOption.GenerateUniqueName);&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;// Done:&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt; outputFile;&lt;br /&gt;} &lt;span style="color: rgb(0, 0, 255);"&gt;catch&lt;/span&gt; (Exception ex) {&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;// If we cannot obtain an output file objects, we cannot convert.&lt;/span&gt;&lt;br /&gt; &lt;span style="color: rgb(0, 128, 0);"&gt;// Need another input file:&lt;/span&gt;&lt;br /&gt;CleanupOutputFile(outputFile); &lt;br /&gt;Log(ex.ToString());&lt;br /&gt;SetPageState(PageState.InputFileNeeded);&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;;&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;/// Deletes unneeded output file.&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;/// &amp;lt;param name=&amp;quot;outputFile&amp;quot;&amp;gt;File to delete.&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;private&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;static&lt;/span&gt; async &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; CleanupOutputFile(StorageFile outputFile) {&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;// Nothing to delete:&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (outputFile == &lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;)&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;try&lt;/span&gt; {&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;// Delete specified file asynchronously.&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;// Deletion errors are benign to this application, we will ignore them.&lt;/span&gt;&lt;br /&gt;await outputFile.DeleteAsync();&lt;br /&gt;} &lt;span style="color: rgb(0, 0, 255);"&gt;catch&lt;/span&gt; { };&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;/// Event handler for the &amp;quot;Start Conversion&amp;quot;-button.&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;/// 1. Init output file.&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;/// 2. Validate transcoding can occur.&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;/// 3. Perform transcoding.&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;/// Also hook up and handle cancellation, progress updates and errors.&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;/// &amp;lt;param name=&amp;quot;sender&amp;quot;&amp;gt;Ignored&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;/// &amp;lt;param name=&amp;quot;e&amp;quot;&amp;gt;Ignored&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;private&lt;/span&gt; async &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; StartConversionButton_Click(Object sender, RoutedEventArgs e) {&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;// Prepare the UI:&lt;/span&gt;&lt;br /&gt;PageState prevPageState = DisableButtons();&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;// Determine the output file.&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;// If we cannot prepare the output, we cannot start conversion:&lt;/span&gt;&lt;br /&gt;StorageFile outputFile = await CreateOutputFile();&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (outputFile == &lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;)&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;// Update the UI:&lt;/span&gt;&lt;br /&gt;OutputFileName.Text = outputFile.Path;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;try&lt;/span&gt; {&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;// Defensive checks for current application state:&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;// (I/O files must be ready)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (_inputFile == &lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;)&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;throw&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; InvalidOperationException(&lt;span style="color: rgb(0, 96, 128);"&gt;&amp;quot;Unexpected state: _inputFile==null&amp;quot;&lt;/span&gt;);&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (outputFile == &lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;)&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;throw&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; InvalidOperationException(&lt;span style="color: rgb(0, 96, 128);"&gt;&amp;quot;Unexpected state: outputFile==null&amp;quot;&lt;/span&gt;);&lt;br /&gt;Log(&lt;span style="color: rgb(0, 96, 128);"&gt;&amp;quot;Preparing transcode to file: &amp;quot;&lt;/span&gt; + outputFile.Path);&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;// Asynchronously check if input file can be transcoded to target format:&lt;/span&gt;&lt;br /&gt;MediaEncodingProfile profile =&lt;br /&gt;MediaEncodingProfile.CreateMp4(VideoEncodingQuality.Vga);&lt;br /&gt;PrepareTranscodeResult transcode =&lt;br /&gt;await _transcoder.PrepareFileTranscodeAsync(_inputFile,&lt;br /&gt;outputFile, profile);&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;// If we cannot transcode specified input to the target format, bail out:&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (!transcode.CanTranscode) {&lt;br /&gt;Log(&lt;span style="color: rgb(0, 96, 128);"&gt;&amp;quot;Transcode preparation failed. Reason: &amp;quot;&lt;/span&gt; +&lt;br /&gt;transcode.FailureReason.ToString());&lt;br /&gt;CleanupOutputFile(outputFile);&lt;br /&gt;SetPageState(PageState.InputFileNeeded);&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt;;&lt;br /&gt;}&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;// OK, we can transcode:&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;// Reset progress bar and set the UI state:&lt;/span&gt;&lt;br /&gt;ProgressBar.Value = 0;&lt;br /&gt;SetPageState(PageState.Converting);&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;// Transcode asynchronously:&lt;/span&gt;&lt;br /&gt;await transcode.TranscodeAsync().AsTask(&lt;br /&gt;_transcodeCancelControl.Token,&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; Progress&amp;lt;Double&amp;gt;((p) =&amp;gt; ProgressBar.Value = p));&lt;br /&gt;&lt;/p&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;&lt;/span&gt;&lt;p&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;// Done transcoding. Update UI:&lt;/span&gt;&lt;br /&gt;Log(&lt;span style="color: rgb(0, 96, 128);"&gt;&amp;quot;Transcoding complete.&amp;quot;&lt;/span&gt;);&lt;br /&gt;SetPageState(PageState.InputFileNeeded);&lt;br /&gt;} &lt;span style="color: rgb(0, 0, 255);"&gt;catch&lt;/span&gt; (OperationCanceledException) {&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;// Transcoding has been cancelled. Delete incomplete output file and reset UI:&lt;/span&gt;&lt;br /&gt;Log(&lt;span style="color: rgb(0, 96, 128);"&gt;&amp;quot;Transcoding has been cancelled&amp;quot;&lt;/span&gt;);&lt;br /&gt;CleanupOutputFile(outputFile);&lt;br /&gt;SetPageState(prevPageState);&lt;br /&gt;} &lt;span style="color: rgb(0, 0, 255);"&gt;catch&lt;/span&gt; (Exception ex) {&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;// Error during transcoding. Delete incomplete output file and reset the UI:&lt;/span&gt;&lt;br /&gt;Log(&lt;span style="color: rgb(0, 96, 128);"&gt;&amp;quot;Error during transcoding: &amp;quot;&lt;/span&gt; + ex.ToString());&lt;br /&gt;CleanupOutputFile(outputFile);&lt;br /&gt;SetPageState(prevPageState);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;/p&gt;&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;There are a few things to note here:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;We do not explicitly create a new type to monitor the progress. Instead we use the &lt;a href="http://msdn.microsoft.com/en-us/library/hh193692.aspx"&gt;Progress&amp;lt;TProgressInfo&amp;gt;&lt;/a&gt; type and a lambda expression to specify the action to occur on progress update in-line.&lt;/li&gt;

  &lt;li&gt;The &amp;quot;Start conversion&amp;quot;-button handler is responsible for several things: First, it asynchronously initialises the output file on the hard drive. If this fails, we bail out before the conversion is even attempted. Then, it verifies whether the specified input file can be successfully converted to the specified format. If the conversion is not possible, it also bails out. Finally, it performs the actual conversion. Note that exceptional conditions such as cancellation and errors are handled in-line using the common try-catch mechanism. During the entire process the hander updates the UI as appropriate.&lt;/li&gt;

  &lt;li&gt;Recall that await causes the &amp;quot;rest of the method&amp;quot;, i.e. the statements that follow the await statement, to be executed as a &lt;i&gt;continuation&lt;/i&gt; of the &lt;i&gt;future&lt;/i&gt; being awaited. I.e., when these subsequent statements are executed we know that the awaited operation has completed. If the execution proceeded with the line directly after the await – the execution has completed normally; if it jumped to the OperationCanceledException's catch clause – the execution has been successfully cancelled; if it jumped to the default catch clause – the operation faulted. Either way – it has finished. As a result, we can do some clean-up. For instance, we will dispose the _transcodeCancelControl in the SetPageState(..) method. It is also the SetPageState(..) method that initialises the cancel control lazily – right in time when the cancel-button is activated. You can see how this is done by downloading the complete source code of the video transcoding application we discussed in this article.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;Summary and Pointers&lt;/h3&gt;

&lt;p&gt;In this article we learned about the Windows Runtime (WinRT) model for asynchronous programming. The model differs from the .NET Task-based universe because it must work well with other WinRT technologies such as JavaScript and native C++. However, .NET makes is simple to await WinRT asynchronous operations in exactly the same manner as you await Task-based operations. In addition, the AsTask(..) method family allows using traditional .NET patterns for cancellation and progress monitoring of asynchronous WinRT operations (i.e. CancellationToken/CancellationTokenSource pattern and the Progress&amp;lt;T&amp;gt; pattern).&lt;/p&gt;

&lt;p&gt;It is possible to use methods and properties available on WinRT &lt;i&gt;future&lt;/i&gt; types directly, but this is discouraged. Doing so would require learning a different set of programming patterns and prevent the use of the await keyword. It is complicated and very easy to get wrong. It also can require explicit use of the &lt;a href="http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.core.coredispatcher"&gt;CoreDispatcher&lt;/a&gt; and/or the &lt;a href="http://msdn.microsoft.com/en-us/library/system.threading.synchronizationcontext.aspx"&gt;SynchronizationContext&lt;/a&gt; – complex concepts that do not need to otherwise be dealt with by most application programmers who use async and await keywords for writing Windows Store apps.&lt;/p&gt;

&lt;p&gt;We walked through writing a complete Windows Store application that uses a number of asynchronous actions and operations exposed by WinRT. You can download the complete source for this project here: &lt;a href="http://aka.ms/dotnetwinrtasynccode"&gt;.NET Asynchronous Programming for Windows Store Apps&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;There is another, larger, transcoding media sample app &lt;a href="http://code.msdn.microsoft.com/windowsapps/media-transcode-sample-f7ba5269"&gt;available from the Windows Dev Center&lt;/a&gt;. While here we primarily focussed on consuming asynchronous APIs, that sample goes into more detail on using WinRT transcoding APIs with different options.&lt;/p&gt;

&lt;p&gt;There is a number of articles that can help you learning more about asynchronous programming in .NET in general and how to apply it in Windows Store Apps specifically:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&amp;quot;&lt;a href="http://msdn.microsoft.com/en-us/library/vstudio/hh191443.aspx"&gt;Asynchronous Programming with Async and Await (C# and Visual Basic)&lt;/a&gt;&amp;quot; docs on MSDN.&lt;/li&gt;

  &lt;li&gt;&amp;quot;&lt;a href="http://msdn.microsoft.com/en-us/library/windows/apps/hh464924.aspx"&gt;Asynchronous programming (Windows Store apps)&lt;/a&gt;&amp;quot; docs in Windows Dev Center.&lt;/li&gt;

  &lt;li&gt;&amp;quot;&lt;a href="http://blogs.msdn.com/b/dotnet/archive/2012/04/03/async-in-4-5-worth-the-await.aspx"&gt;Async in 4.5: Worth the Await&lt;/a&gt;&amp;quot; by Alok Shriram.&lt;/li&gt;

  &lt;li&gt;&amp;quot;&lt;a href="http://blogs.msdn.com/b/dotnet/archive/2012/06/06/async-in-4-5-enabling-progress-and-cancellation-in-async-apis.aspx"&gt;Async in 4.5: Enabling Progress and Cancellation in Async APIs&amp;quot;&lt;/a&gt; by Alok Shriram.&lt;/li&gt;

  &lt;li&gt;&amp;quot;&lt;a href="http://blogs.msdn.com/b/windowsappdev/archive/2012/03/20/keeping-apps-fast-and-fluid-with-asynchrony-in-the-windows-runtime.aspx"&gt;Keeping apps fast and fluid with asynchrony in the Windows Runtime&lt;/a&gt;&amp;quot; by Jason Olson.&lt;/li&gt;

  &lt;li&gt;&amp;quot;&lt;a href="http://blogs.msdn.com/b/windowsappdev/archive/2012/04/24/diving-deep-with-winrt-and-await.aspx"&gt;Diving deep with WinRT and await&lt;/a&gt;&amp;quot; by Steve Toub.&lt;/li&gt;

  &lt;li&gt;&amp;quot;&lt;a href="http://blogs.msdn.com/b/windowsappdev/archive/2012/06/14/exposing-net-tasks-as-winrt-asynchronous-operations.aspx"&gt;Exposing .NET tasks as WinRT asynchronous operations&lt;/a&gt;&amp;quot; by Steve Toub.&lt;/li&gt;

  &lt;li&gt;&lt;a href="http://blogs.msdn.com/b/pfxteam/"&gt;Parallel Programming with .NET&lt;/a&gt; team blog.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Enjoy!&lt;/p&gt;

&lt;p&gt;Follow us or talk to us on &lt;a href="https://twitter.com/dotnet"&gt;Twitter&lt;/a&gt; -- &lt;a href="https://twitter.com/dotnet"&gt;https://twitter.com/dotnet&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=10373135" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/dotnet/archive/tags/-net+framework/">.net framework</category><category domain="http://blogs.msdn.com/b/dotnet/archive/tags/performance/">performance</category><category domain="http://blogs.msdn.com/b/dotnet/archive/tags/async/">async</category><category domain="http://blogs.msdn.com/b/dotnet/archive/tags/windows+store/">windows store</category></item><item><title>Building Windows Store Apps with .NET</title><link>http://blogs.msdn.com/b/dotnet/archive/2012/11/20/building-windows-store-apps-with-net.aspx</link><pubDate>Tue, 20 Nov 2012 21:12:35 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10370357</guid><dc:creator>Brandon Bray</dc:creator><slash:comments>11</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/dotnet/rsscomments.aspx?WeblogPostID=10370357</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/dotnet/commentapi.aspx?WeblogPostID=10370357</wfw:comment><comments>http://blogs.msdn.com/b/dotnet/archive/2012/11/20/building-windows-store-apps-with-net.aspx#comments</comments><description>&lt;blockquote&gt;   &lt;p&gt;&lt;i&gt;For .NET developers, this is the best time to build client apps. Never before have the Windows APIs been so easy to use from C# or Visual Basic without wrapping them with custom libraries. So far, we’ve seen some amazing apps in the Windows Store built using C#. &lt;b&gt;Richard Lander, program manager for the CLR and frequent contributor to this blog&lt;/b&gt;, provides the highlights and references for building your Windows Store apps with .NET. -- Brandon&lt;/i&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;h3&gt;The Windows Store is a new opportunity &lt;/h3&gt;  &lt;p&gt;Windows 8 redefines apps on Windows, improving both the experiences of creating apps and, most importantly, in using them. The Windows Store provides a new opportunity for you to distribute and sell the apps that you create. Millions of end-users around the world will be looking for apps in a single place, in the Windows Store. Your app might be just the one that they are looking for. That’s a new and compelling proposition.&lt;/p&gt;  &lt;p&gt;As a .NET developer, you’ll find that there are multiple new development options available for you to build compelling Windows Store Apps using .NET. .NET code can be used for user interface, business logic and background processing. You can continue to use .NET on the server too, as the implementation of REST, WCF or ODATA APIs that you call from within your Windows Store Apps. For all apps, .NET is a great choice for server logic. &lt;/p&gt;  &lt;p&gt;Your app goes &lt;a href="http://msdn.microsoft.com/library/windows/apps/br230836"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;&lt;img title="image" style="border: 0px currentcolor; margin-right: auto; margin-left: auto; float: none; display: block; background-image: none;" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-34-metablogapi/8424.image_5F00_555853CC.png" width="624" height="351" /&gt;&lt;/p&gt;  &lt;h3&gt;Getting started writing .NET apps&lt;/h3&gt;  &lt;p&gt;The best way to get started is to &lt;a href="http://msdn.microsoft.com/windows/apps/br229516"&gt;download the tools&lt;/a&gt;, including &lt;a href="http://msdn.microsoft.com/en-us/library/windows/apps/br211384.aspx"&gt;Visual Studio 2012&lt;/a&gt; and &lt;a href="http://msdn.microsoft.com/library/windows/apps/jj129478.aspx"&gt;Blend for Visual Studio 2012&lt;/a&gt;. You will find several templates for .NET apps. In less than an hour, you can download and install everything you need, try out the templates and actually build the beginnings of your first real Windows Store app.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.microsoft.com/visualstudio/downloads#d-2012-express"&gt;Visual Studio Express for Windows 8&lt;/a&gt; is free to use and you can build your entire app with it alone. You just need Windows 8 and an internet connection to download the tools. From there, you are on your way. If you don’t have Windows 8, you can install a free &lt;a href="http://msdn.microsoft.com/evalcenter/jj554510.aspx"&gt;evaluation copy for developers&lt;/a&gt;. If you already have Windows 8 and Visual Studio 2012, then you don’t need anything more.&lt;/p&gt;  &lt;p&gt;I have been using the &lt;a href="http://code.msdn.microsoft.com/windowsapps/site/search?f%5B0%5D.Type=Contributors&amp;amp;f%5B0%5D.Value=Official%20Windows%20SDK%20Sample&amp;amp;f%5B0%5D.Text=Windows%20SDK"&gt;Windows SDK samples&lt;/a&gt; a lot. There are samples for most of the UI patterns and technologies that you will use to build Windows Store Apps. For example, there are samples dedicated to &lt;a href="http://code.msdn.microsoft.com/windowsapps/Snap-Sample-2dc21ee3"&gt;snap&lt;/a&gt;, &lt;a href="http://code.msdn.microsoft.com/windowsapps/GroupedGridView-77c59e8e"&gt;semantic zoom&lt;/a&gt;, &lt;a href="http://code.msdn.microsoft.com/windowsapps/Lock-screen-apps-sample-9843dc3a"&gt;Lock screen presence&lt;/a&gt;, &lt;a href="http://code.msdn.microsoft.com/windowsapps/Data-Binding-7b1d67b5"&gt;XAML data binding&lt;/a&gt; and &lt;a href="http://code.msdn.microsoft.com/windowsapps/File-access-sample-d723e597"&gt;file access&lt;/a&gt;. These samples are a great way to quickly learn how to add something to your app or can provide exactly the insight you need to get something to work.&lt;/p&gt;  &lt;p&gt;MSDN documentation has all of the reference topics you need to learn more about APIs and new concepts. The documentation is split into &lt;a href="http://msdn.microsoft.com/library/windows/apps/xaml/br211377.aspx"&gt;Windows.* WinRT APIs&lt;/a&gt; and the &lt;a href="http://msdn.microsoft.com/library/windows/apps/xaml/br230232.aspx"&gt;System.* .NET for Windows Store APIs&lt;/a&gt;. You can also browse the &lt;a href="http://msdn.microsoft.com/library/gg145045.aspx"&gt;.Net Framework APIs&lt;/a&gt; within the .NET Framework 4.5 class library. That documentation clearly displays which APIs are available for Windows Store apps, with a small green shopping bag. You may also want to read the post that we published on the &lt;a href="http://blogs.msdn.com/b/dotnet/archive/2012/04/17/net-for-metro-style-apps.aspx"&gt;.NET APIs for Windows Store Apps&lt;/a&gt; earlier in the year, on this blog.&lt;/p&gt;  &lt;p&gt;There is also another blog dedicated to discussing &lt;a href="http://blogs.msdn.com/b/windowsappdev/"&gt;Windows Store app development&lt;/a&gt;. You will want to consult it, as it goes into a lot more detail about Windows Store apps, including apps written with .NET.&lt;/p&gt;  &lt;h3&gt;Windows 8 adds the Windows Runtime &lt;/h3&gt;  &lt;p&gt;Before you get too far into building Windows Store Apps, you will benefit from a basic understanding of the Windows Runtime (WinRT). The WinRT is a new subsystem within Windows that forms much of the foundation of Windows Store apps. Most of the new Windows APIs that you will use are exposed via the WinRT. The .NET APIs are not exposed via WinRT, but continue to work as they always have, exposed by the CLR.&lt;/p&gt;  &lt;p&gt;.NET developers are not strangers to interop technologies. You can use both COM Interop and P/invoke to call into native APIs from .NET code. The Windows Runtime is a big step ahead for .NET developers, resolving challenges with those existing interop technologies. It establishes a level playing field across languages, enabling C#, Visual Basic, C++ and JavaScript to depend on the same set of APIs in the same way. &lt;/p&gt;  &lt;p&gt;All languages use the same metadata format for describing WinRT APIs. In fact, this metadata form is CLR metadata, with a small set of tweaks. As a result, it was very easy for Visual Studio to create an IntelliSense experience for WinRT APIs that is similar to the .NET Framework and for the .NET compilers to reference WinRT metdata. You can open WinRT metadata files, called WinMDs, in ILDasm and Reflector! In addition to being able to call WinRT APIs, you can use C# and VB to create Windows Runtime Components, which expose WinRT APIs. &lt;/p&gt;  &lt;p&gt;Shawn Farkas, from the Common Language Runtime team, published a very useful and technical MSDN Magazine article on Windows Runtime interop with managed code, called &lt;a href="http://msdn.microsoft.com/magazine/jj651569.aspx"&gt;Underneath the Hood with .NET and the Windows Runtime&lt;/a&gt;. It will answer many of the lower-level questions that you might have about how managed code can WinRT interoperate. &lt;/p&gt;  &lt;p&gt;Resource: &lt;a href="http://msdn.microsoft.com/library/windows/apps/hh694558.aspx"&gt;NET Framework Support for Windows Store Apps and Windows Runtime&lt;/a&gt;&lt;/p&gt;  &lt;h3&gt;Writing Windows Store XAML Apps&lt;/h3&gt;  &lt;p&gt;Many of you will see a natural affinity between &lt;a href="http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh700354.aspx"&gt;XAML&lt;/a&gt; and .NET. You may have experience building Silverlight and WPF apps, which also use XAML. If you are familiar with XAML, then Windows Store XAML apps will be a natural fit. You will find that you are immediately comfortable with and able to build Windows Store XAML apps. The code-behind model is the same and the XAML syntax is largely what you’ve used before. You can continue to use your preferred design pattern, such as MVVM or MVC. You can read about the differences in the &lt;a href="http://msdn.microsoft.com/en-us/library/windows/apps/xaml/br229571.aspx"&gt;XAML porting guide&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Visual Studio 2012 makes it easy to build XAML apps, using a new &lt;a href="http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh921077.aspx"&gt;designer for XAML apps&lt;/a&gt;. You can also use &lt;a href="http://msdn.microsoft.com/en-us/library/windows/apps/jj129478.aspx"&gt;Blend for Visual Studio 2012&lt;/a&gt; to create visually compelling apps. The process of creating XAML apps is very similar in practice to what you are used to. The main difference is that all of the XAML APIs will appear in Windows.UI.XAML instead of System.Windows.&lt;/p&gt;  &lt;p&gt;Resource: &lt;a href="http://msdn.microsoft.com/library/windows/apps/xaml/hh700354.aspx"&gt;XAML overview (Windows Store apps using C#/VB/C++ and XAML)&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Resource: &lt;a href="http://msdn.microsoft.com/library/windows/apps/xaml/hh465340.aspx"&gt;Quickstart: Create a UI with XAML (Windows Store apps using C#/VB/C++ and XAML)&lt;/a&gt;&lt;/p&gt;  &lt;h4&gt;XAML App Windows 8 SDK Sample&lt;/h4&gt;  &lt;p&gt;I’ve included a screenshot of a XAML app from the Windows 8 SDK, called &lt;a href="http://code.msdn.microsoft.com/windowsapps/Basic-Controls-29318599"&gt;XAML essential controls sample&lt;/a&gt;. This sample demonstrates the basic set of XAML controls you have available. The screenshot demonstrates the progress bars available within XAML apps. You can look at the many other XAML samples available in the Windows 8 SDK, in &lt;a href="http://code.msdn.microsoft.com/windowsapps/site/search?f%5B0%5D.Type=Contributors&amp;amp;f%5B0%5D.Value=Official%20Windows%20SDK%20Sample&amp;amp;f%5B0%5D.Text=Windows%20SDK&amp;amp;f%5B1%5D.Type=ProgrammingLanguage&amp;amp;f%5B1%5D.Value=C%23&amp;amp;f%5B1%5D.Text=C%23&amp;amp;f%5B2%5D.Type=Technology&amp;amp;f%5B2%5D.Value=XAML&amp;amp;f%5B3%5D.Type=Technology&amp;amp;f%5B3%5D.Value=Windows%20Runtime"&gt;C#&lt;/a&gt; and &lt;a href="http://code.msdn.microsoft.com/windowsapps/site/search?f%5B0%5D.Type=Contributors&amp;amp;f%5B0%5D.Value=Official%20Windows%20SDK%20Sample&amp;amp;f%5B0%5D.Text=Windows%20SDK&amp;amp;f%5B1%5D.Type=Technology&amp;amp;f%5B1%5D.Value=XAML&amp;amp;f%5B2%5D.Type=Technology&amp;amp;f%5B2%5D.Value=Windows%20Runtime&amp;amp;f%5B3%5D.Type=ProgrammingLanguage&amp;amp;f%5B3%5D.Value=VB&amp;amp;f%5B3%5D.Text=VB.NET"&gt;VB&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;&lt;img title="image" style="border: 0px currentcolor; margin-right: auto; margin-left: auto; float: none; display: block; background-image: none;" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-34-metablogapi/6886.image_5F00_66C8B4A4.png" width="526" height="594" /&gt;&lt;/p&gt;  &lt;h4&gt;Updated XAML App Windows 8 SDK Sample&lt;/h4&gt;  &lt;p&gt;The sample above is largely composed of XAML, without code affecting the presentation of the controls. I’ve made a small update to the sample to show you how you can write C# code that modifies the controls that you see above. All of the XAML controls that are built-in to the platform have been built with C++ and are exposed by WinRT for both .NET and C++ callers. That means, the C# code that you will see below needs to call through WinRT in order to update the controls. You will quickly notice that while the code calls into WinRT, that it looks like .NET code, and is very similar to the code that you would write for the other XAML platforms (Silverlight, WPF) that expose a managed code surface area.&lt;/p&gt;  &lt;p&gt;I added a button to the XAML (“Update Progress”, below), which is intended to update the state of the determinate progress bar, otherwise set to a constant 30%. Within the Visual Studio 2012 XAML designer, I double-clicked on the button to create an event handler for the click event for the button. Last, I wrote this small bit of code to update the progress bar with each button click. All of the code you see below is .NET code, however, the two calls to determinateProgress.Value are interop calls via WinRT. The WinRT calls blend in very cleanly with the rest of the code that you write. The objects in the method signature are also WinRT objects, however, I am not using them in this sample.&lt;/p&gt;  &lt;div id="codeSnippetWrapper"&gt;   &lt;pre id="codeSnippet" style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &amp;quot;Courier New&amp;quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;private&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; Button_Click_1(&lt;span style="color: rgb(0, 0, 255);"&gt;object&lt;/span&gt; sender, RoutedEventArgs e)&lt;br /&gt;{&lt;br /&gt;    var currentProgress = determinateProgress.Value;&lt;br /&gt;    &lt;br /&gt;    currentProgress += 10;&lt;br /&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (currentProgress &amp;gt; 90)&lt;br /&gt;        currentProgress = 10;&lt;br /&gt;&lt;br /&gt;    determinateProgress.Value = currentProgress;&lt;br /&gt;}&lt;/pre&gt;

  &lt;br /&gt;You can see the updated SDK sample below, with the addition of my button. Notice that the progress bar is in a different place. I clicked it a few times before I took this second screenshot.&lt;/div&gt;

&lt;p&gt;&lt;img title="image" style="border: 0px currentcolor; margin-right: auto; margin-left: auto; float: none; display: block; background-image: none;" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-34-metablogapi/4743.image_5F00_2DB1D7A2.png" width="700" height="544" /&gt;&lt;/p&gt;

&lt;h3&gt;Writing Windows Store DirectX Games -- SharpDX&lt;/h3&gt;

&lt;p&gt;DirectX games written in C# are another popular option that we are seeing, including games built by Microsoft. The Windows 8 SDK does not include a DirectX API for .NET developers, but only for C++. Fortunately for you, the open source &lt;a href="http://sharpdx.org/"&gt;SharpDX&lt;/a&gt; DirectX SDK is available to .NET developers to write Windows Store apps. It should also be noted that you can create a XAML app with .NET that hosts DirectX component s written in C++.&lt;/p&gt;

&lt;p&gt;In addition, &lt;a href="http://unity3d.com/"&gt;Unity&lt;/a&gt; has announced that they will &lt;a href="http://www.marketwire.com/press-release/unite-2012-unity-technologies-announces-support-for-windows-8-and-windows-phone-8-1693366.htm"&gt;support their Unity3D game engine for .NET developers&lt;/a&gt;, for Windows Store apps and Windows Phone 8. This release is still pending and doesn’t have a specific release date. &lt;/p&gt;

&lt;p&gt;SharpDX is a low-level set of managed wrappers over the native DirectX API. These wrappers are tool-generated directly from DirectX header files, which enables them to be complete and also results in them including just the minimal interop code necessary to call the native APIs. It &lt;a href="http://sharpdx.org/about/features"&gt;supports all of the DirectX API&lt;/a&gt;, including 2D, 3D, Sound and Input. You can get the SharpDX SDK from the &lt;a href="http://www.sharpdx.org/"&gt;SharpDX site&lt;/a&gt;. An update to &lt;a href="http://www.sharpdx.org/news/new-version-2-4-0"&gt;SharpDX 2.4&lt;/a&gt; was recently released, and also supports Windows Phone.&lt;/p&gt;

&lt;p&gt;The following screenshot demonstrates a sample SharpDX app, which is a spinning cube. &lt;/p&gt;

&lt;p&gt;&lt;img title="image" style="border: 0px currentcolor; margin-right: auto; margin-left: auto; float: none; display: block; background-image: none;" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-34-metablogapi/0523.image_5F00_0D96CAE5.png" width="440" height="278" /&gt;&lt;/p&gt;

&lt;p&gt;The following screenshot shows some of the code that displays the spinning cube above, in Visual Studio 2012.&lt;/p&gt;

&lt;p&gt;&lt;img title="image" style="border: 0px currentcolor; margin-right: auto; margin-left: auto; float: none; display: block; background-image: none;" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-12-34-metablogapi/8037.image_5F00_547FEDE2.png" width="691" height="621" /&gt;&lt;/p&gt;

&lt;h3&gt;Closing&lt;/h3&gt;

&lt;p&gt;The Windows Store creates an important new opportunity for you, with Windows 8. More than &lt;a href="http://www.microsoft.com/news/Press/2012/Oct12/10-25Windows8GAPR.aspx"&gt;1,000 PCs and tablets, including Microsoft Surface, are available&lt;/a&gt; for consumers and businesses to buy. I expect that the Windows Store app within Windows 8 will be a frequent destination for users, wanting to find apps that meet their needs and interests. That app might be yours. The proposition to developers is a unique one, given that there are over 1 billion Windows users worldwide. Many of these users will approach the Windows Store with fresh enthusiasm, having never experienced an app store before. Your app might become part of their daily routine and part of their lives.&lt;/p&gt;

&lt;p&gt;You can create .NET apps with XAML UI. For games, you may choose one of the DirectX SDKs that 3&lt;sup&gt;rd&lt;/sup&gt; party groups have already made available. It really is a great time to be a .NET developer. The Windows Store provides great ways to build your app with the .NET technologies that you’ve already learned. &lt;a href="http://msdn.microsoft.com/windows/apps/br229516.aspx"&gt;Visual Studio 2012 and the .NET Framework 4.5&lt;/a&gt; are available to help you build .NET Windows Store apps. The Windows Store is &lt;a href="http://blogs.msdn.com/b/windowsstore/archive/2012/09/11/windows-store-now-open-to-all-developers.aspx"&gt;open to developers in 120 countries&lt;/a&gt; around the world. Users and businesses will start buying Windows 8 in large numbers. The only thing left is your app.&lt;/p&gt;

&lt;p&gt;Once again, please do check out the &lt;a href="http://blogs.msdn.com/b/windowsappdev/"&gt;Windows 8 app developer blog&lt;/a&gt; to learn more about Windows Store development.&lt;/p&gt;

&lt;p&gt;Follow us or talk to us on &lt;a href="https://twitter.com/dotnet"&gt;Twitter&lt;/a&gt; -- &lt;a href="https://twitter.com/dotnet"&gt;https://twitter.com/dotnet&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=10370357" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/dotnet/archive/tags/-net+framework/">.net framework</category><category domain="http://blogs.msdn.com/b/dotnet/archive/tags/windows+store/">windows store</category></item></channel></rss>