- Overview of Asynchrony
- The Old-Fashioned Way: Event-Based Asynchrony
- The Old-Fashioned Way: The Asynchronous Programming Model
- The Modern Way: The Async Pattern
- Getting Started with Async/ Await
- Exception Handling in Async
- Implementing Task-Based Asynchrony
- Cancellation and Progress
- Asynchronous Lambda Expressions
- Asynchronous I/O File Operations in .NET 4.6
- Debugging Tasks
- Summary
Summary
Building applications that remain responsive whatever task they are executing is something that developers must take care of, especially from the user interface perspective. This chapter explained how to use asynchrony to build responsive applications by first discussing old-style programming models such as the Event-based Asynchronous Pattern and the Asynchronous Programming Model. Both provide techniques to write asynchronous code that runs on separate threads. However, both have some limitations, such as code complexity, issues in returning information to caller threads or functions, and managing errors effectively. Visual Basic 2015 offers the asynchronous pattern based on the Async and Await keywords, which enable you to keep the UI thread free and write code that is similar to the synchronous approach and is much easier to read and maintain. You invoke an asynchronous task, and then its result is returned some time later, but the control is immediately returned to the caller. When the result is available, an implicit callback enables you to consume the result of the asynchronous operation effectively. The Async pattern relies on the concept of task and on the Task class, which means that asynchrony easily includes support for cancellation, progress, anonymous delegates, and concurrent operations. The .NET Framework 4.6 itself exposes built-in classes that use the new Async pattern for making asynchronous I/O operations much easier with particular regard to streams and network requests. The Async pattern and the Async/Await keywords can be used across multiple platforms and presentation technologies.