15 Asynchronous Programming

What’s In This Chapter?

  • Why asynchronous programming is important
  • Asynchronous patterns
  • Foundations of the async and await keywords
  • Creating and using asynchronous methods
  • Error handling with asynchronous methods
  • Cancelling long-running tasks

Wrox.com Code Downloads for This Chapter

The wrox.com code downloads for this chapter are found at www.wrox.com/go/professionalcsharp6 on the Download Code tab. The code for this chapter is divided into the following major examples:

  • Async Patterns
  • Foundations
  • Error Handling

Why Asynchronous Programming Is Important

C# 6 adds a lot of new keywords, whereas with C# 5 there were only two new keywords: async and await. These two keywords are the main focus of this chapter.

With asynchronous programming a method is called that runs in the background (typically with the help of a thread or task), and the calling thread is not blocked.

In this chapter, you can read about different patterns on asynchronous programming such as the asynchronous pattern, the event-based asynchronous pattern, and the task-based asynchronous pattern (TAP). TAP makes use of the async and await keywords. Comparing these patterns you can see the real advantage of the new style of asynchronous programming.

After discussing the different patterns, you see the foundation of asynchronous programming by creating tasks and invoking asynchronous methods. You find out what’s behind the scenes with continuation tasks and the synchronization context. ...

Get Professional C# 6 and .NET Core 1.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.