Tuesday, November 27, 2012

Pack it in, pack it out with the Async Pack and Windows Phone 8 projects.

qmatteoq.com - Async pack for Visual Studio 2012: why it’s useful also for Windows Phone 8 projects

"Microsoft has recently released the Async Pack for Visual Studio 2012: it’s a very helpful package and it’s the successor of the old Async CTP that was available for Visual Studio 2010. Its purpose is to add support to the new asynchronous pattern introduced in C# 5 (based on the async and await keywords) also to “old” technologies that, since are still based on C# 4, couldn’t make use of it. Some example of these technologies are Silverlight or Windows Phone 7: basically, we’re talking about all the stuff that relies on .NET Framework 4, since the new .NET Framework 4.5 (that has been shipped with Windows 8) is based on C# 5.

Installing it is very easy: you simply have to use NuGet, by right clicking on the project ...

But why this package should be interesting also for developing Windows Phone 8 applications? Windows Phone 8 is a new technology, based on a subset of the Windows Runtime, so the async and await support is built in.

The answer is that, due to maintain compatibility with the old platform (that didn’t have async and await support), some old classes doesn’t offer a way to use them with the new pattern. Take the WebClient class for example, which is very popular to make network operations (like downloading a file). Unlike the other WinRT APIs (like the ones to work with storage or sensors), it still relies on the old callback pattern: you register for an event handler that is invoked when the operation is finished, you launch the network operation and, inside the event handler (that is the callback), you manage all the operations that should be done with the result (like storing the file in the local storage or displaying it).



Plus, probably we’ll have some sort of “spaghetti” code: we’re using the callback approach to manage network operations but, for example, we’ll have to use the async and await pattern if we want to use the new storage APIs to save the HTML in a file in the local storage.

Here comes the Async pack: other than adding support to async and await (that is not needed, since Windows Runtime for Windows Phone already supports it) it adds a bunch of extension methods, that add to many “old style” classes new methods to work with Task objects and the new async pattern.

Some examples of classes that can take benefit of these extension methods are WebClient, HttpWebRequest and Stream. This way we change the previous example to use the new approach:


For all of the reasons I’ve explained in this post, I strongly suggest you to add the Async targeting pack to every Windows Phone 8 project you’re going to develop. Have fun

I've sad to say that I've forgotten about the Async Targeting Pack. I've been pushing for our move to Net 4.5 and async/await is one of the reasons I'm using. I'd totally forgotten about the Async Pack. Sigh... stupid old brain. Now I'm going to have to go over to my co-worker's cube (who's more than happy in VS2010/.Net 4.0 land) and suggest this. While it's taken me a bit to wrap my head about async/await in my LOB world, now's it has quickly become my new shiny, that I use everywhere I can. BackgroundWorker, off with you! :P


Related Past Post XRef:
No more of that sinking feeling when using async for .Net 4/Silverlight 5 Projects - The Async Targeting Pack for Visual Studio 11 now available (Nuget: "Microsoft.CompilerServices.AsyncTargetingPack")
Visual Studio Async CTP (Version 3) Released - Lots of compatibility added, bugs squashed, features added and the EULA updated to allow Production usage (not encouraged, but it IS allowed now)
PDC10 Async Round-up

No comments: