Metro style applications built on the Windows Runtime are especially sensitive to any delays in the UI thread. Users will immediately notice when the UI trails their touch input and “doesn’t stick to their finger.” Animations that stutter and skip are immediately visible. Any lag in processing negatively affects the experience and the perception of not only your application, but the platform as a whole. A good way to help ensure your application doesn’t get into this state is to make long-running calls asynchronous. In this article, based on chapter 3 of Windows 8 XAML in Action, author Pete Brown discusses the Windows Runtime approach to asynchronous code.
The Windows Runtime was built with the concept of asynchronous operations. It was also built with the idea that these asynchronous operations must work not only for C#, but also for JavaScript, C++, and any number of other, very different languages. For those reasons, the team didn’t take a dependency on the Task Parallel Library in .NET, but, instead, created an interface-based approach consistent with the rest of the Windows Runtime.
The Windows.Foundation namespace contains the types and interfaces required to support asynchronous functions in WinRT. Every asynchronous operation implements, at a minimum, the IAsyncInfo interface. Like most programming problems, there is an easy but limited way to do async and a more complex but capable way.
In this article, we’ll look at the different approaches for using the WinRT asynchronous APIs; that is, the ones which return IAsync* interfaces. I’ll cover the easiest approach and then look into what’s needed to check progress or cancel an asynchronous operation.
Read full article
here