You are previewing Async in C# 5.0.

Async in C# 5.0

Cover of Async in C# 5.0 by Alex Davies Published by O'Reilly Media, Inc.
  1. Async in C# 5.0
  2. SPECIAL OFFER: Upgrade this ebook with O’Reilly
  3. Preface
    1. Intended Audience
    2. How to Read This Book
    3. Conventions Used in This Book
    4. Using Code Examples
    5. Safari® Books Online
    6. How to Contact Us
    7. Acknowledgments
  4. 1. Introduction
    1. Asynchronous Programming
    2. What’s So Great About Asynchronous Code?
    3. What Is Async?
    4. What Async Does
    5. Async Doesn’t Solve Everything
  5. 2. Why Programs Need to Be Asynchronous
    1. Desktop User Interface Applications
      1. An Analogy: The Cafe
    2. Web Application Server Code
      1. Another Analogy: The Restaurant Kitchen
    3. Silverlight, Windows Phone, and Windows 8
    4. Parallel Code
    5. An Example
  6. 3. Writing Asynchronous Code Manually
    1. Some Asynchronous Patterns Used in .NET
    2. The Simplest Asynchronous Pattern
    3. An Introduction to Task
    4. The Problem with Manual Asynchrony
    5. Converting the Example to Use Manual Asynchronous Code
  7. 4. Writing Async Methods
    1. Converting the Favicon Example to Async
    2. Task and await
    3. Async Method Return Types
    4. Async, Method Signatures, and Interfaces
    5. The return Statement in Async Methods
    6. Async Methods Are Contagious
    7. Async Anonymous Delegates and Lambdas
  8. 5. What await Actually Does
    1. Hibernating and Resuming a Method
    2. The State of the Method
    3. Context
    4. Where await Can’t Be Used
      1. catch and finally Blocks
      2. lock Blocks
      3. LINQ Query Expressions
      4. Unsafe Code
    5. Exception Capture
    6. Async Methods Are Synchronous Until Needed
  9. 6. The Task-Based Asynchronous Pattern
    1. What the TAP Specifies
    2. Using Task for Compute-Intensive Operations
    3. Creating a Puppet Task
    4. Interacting with Old Asynchronous Patterns
    5. Cold and Hot Tasks
    6. Up-Front Work
  10. 7. Utilities for Async Code
    1. Delaying for a Period of Time
    2. Waiting for a Collection of Tasks
    3. Waiting for Any One Task from a Collection
    4. Creating Your Own Combinators
    5. Cancelling Asynchronous Operations
    6. Returning Progress During an Asynchronous Operation
  11. 8. Which Thread Runs My Code?
    1. Before the First await
    2. During the Asynchronous Operation
    3. SynchronizationContext in Detail
    4. await and SynchronizationContext
    5. The Lifecycle of an Async Operation
    6. Choosing Not to Use SynchronizationContext
    7. Interacting with Synchronous Code
  12. 9. Exceptions in Async Code
    1. Exceptions in Async Task-Returning Methods
    2. Unobserved Exceptions
    3. Exceptions in Async void Methods
    4. Fire and Forget
    5. AggregateException and WhenAll
    6. Throwing Exceptions Synchronously
    7. finally in Async Methods
  13. 10. Parallelism Using Async
    1. await and locks
    2. Actors
    3. Using Actors in C#
    4. Task Parallel Library Dataflow
  14. 11. Unit Testing Async Code
    1. The Problem with Unit Testing in Async
    2. Writing Working Async Tests Manually
    3. Using Unit Test Framework Support
  15. 12. Async in ASP.NET Applications
    1. Advantages of Asynchronous Web Server Code
    2. Using Async in ASP.NET MVC 4
    3. Using Async in Older Versions of ASP.NET MVC
    4. Using Async in ASP.NET Web Forms
  16. 13. Async in WinRT Applications
    1. What Is WinRT?
    2. IAsyncAction and IAsyncOperation<T>
    3. Cancellation
    4. Progress
    5. Providing Asynchronous Methods in a WinRT Component
  17. 14. The Async Compiler Transform—in Depth
    1. The stub Method
    2. The State Machine Struct
    3. The MoveNext Method
      1. Your Code
      2. Transforming Returns to Completions
      3. Get to the Right Place in the Method
      4. Pausing the Method for the await
      5. Resuming after the Await
      6. Completing Synchronously
      7. Catching Exceptions
      8. More Complicated Code
    4. Writing Custom Awaitable Types
    5. Interacting with the Debugger
  18. 15. The Performance of Async Code
    1. Measuring Async Overhead
    2. Async Versus Blocking for a Long-Running Operation
    3. Optimizing Async Code for a Long-Running Operation
    4. Async Versus Manual Asynchronous Code
    5. Async Versus Blocking Without a Long-Running Operation
    6. Optimizing Async Code Without a Long-Running Operation
    7. Async Performance Summary
  19. About the Author
  20. SPECIAL OFFER: Upgrade this ebook with O’Reilly
  21. Copyright
O'Reilly logo

Chapter 13. Async in WinRT Applications

For those who aren’t already familiar with WinRT, I’ll give a brief overview of the technology before moving on to explore how async and WinRT work together.

What Is WinRT?

WinRT (or Windows Runtime) is a group of APIs that are used in Windows 8 applications that run on Windows 8 and Windows RT for ARM processors. One of the design goals of the WinRT APIs is responsiveness, achieved by asynchronous programming. All methods that could take longer than 50ms are asynchronous.

It is designed to be used uniformly from three completely different technology stacks: .NET, JavaScript, and native code (usually C++). To achieve this, the APIs are all defined in a common metadata format called WinMD. Each of the languages can then compile against the WinMD definition of the API, without any need for a language-specific wrapper. This system is called projection, where each compiler or interpreter projects the WinRT type to be used as a normal type in the language.

Note

WinMD is based on the .NET assembly metadata format, so the constructs that are available are very similar to .NET: classes interfaces, methods, properties, attributes, etc. There are differences, though; for example, generic types are legal, but generic methods aren’t.

The majority of WinRT is implemented in native code, but you can also write WinRT components in C#, which you or others can then consume from any of the supported languages.

Because the WinRT interfaces are not .NET, the API provided ...

The best content for your career. Discover unlimited learning on demand for around $1/day.