- 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
The Modern Way: The Async Pattern
Visual Basic 2012 introduced a new pattern that solves some problems related to threading and enables you to write better and cleaner code. It does this with two keywords: Async and Await. Async is a modifier you use to mark methods that run asynchronous operations. Await is an operator that gets a placeholder for the result of an asynchronous operation and waits for the result, which will be sent back at a later time while other operations are executed. This enables you to keep the caller thread responsive. For a first understanding of how this pattern works, let’s take a look at the following function that downloads the content of a website as a string, returning the result as an XDocument that can be manipulated with LINQ:
Function DownloadSite() As XDocument Dim client As New System.Net.WebClient Dim content As String = client.DownloadString("http://www.microsoft.com") Dim document As XDocument = XDocument.Parse(content) Return document End Function
This code is pretty easy because it creates an instance of the WebClient class, then downloads the content of the specified website, and finally returns the XML document converted through XDocument.Parse. This code is synchronous, meaning that the caller thread will remain blocked until all the operations in the method body are completed. If the caller thread is the UI thread, the user interface will remain blocked. Figure 42.1 shows a graphical representation of how a synchronous call works.
FIGURE 42.1 Representation of a synchronous call.
This is how you can rewrite the previous code using the Async pattern:
Async Function DownloadSiteAsync() As Task(Of XDocument) Dim client As New System.Net.WebClient Dim content As String = Await client.DownloadStringTaskAsync("http://www.microsoft.com") Dim document As XDocument = XDocument.Parse(content) Return document End Function
This code is asynchronous, so it will never block the caller thread because not all the code is executed at the same time. Figure 42.2 shows a representation of an asynchronous call.
FIGURE 42.2 Representation of an asynchronous call.
The following is a list of important considerations that will be discussed in the next section:
- A method that runs asynchronous code must be marked with the Async modifier. When the compiler encounters this modifier, it expects an Await expression inside the method body.
- Methods marked with Async are also referred to as asynchronous methods.
- By convention, names of asynchronous methods must end with the Async suffix.
- Asynchronous methods must return a Task (if they return no value) or a Task(Of T), where T is the type that you would return with synchronous methods. See the previous XDocument example.
- Asynchronous methods support the standard access modifiers (such as Private, Public, and so on), but they cannot be iterator methods at the same time, so the Iterator modifier cannot be used along with Async.
- The Main method of an application can never be asynchronous. Notice that if you mark the Main method as asynchronous and write asynchronous calls in its body, the background compiler will not report any warnings or exceptions. It will report an error when you compile or try to run the code.
- Any method that returns a Task or Task(Of T) can be used along with Await (“awaitable”).
- The Await expression puts a placeholder for the result of the invoked task. The result will be returned later at some time, making the application remain responsive. However, the next code will actually run when the result has been returned. This is because the compiler ideally splits the method into two parts; the second part is nothing but a callback that is executed when the awaited task notifies the caller of its completion.
- Although you should return a Task or Task(Of T), the compiler automatically infers the task-based type even if you return the original type. The second code snippet in the previous example demonstrates how the Return statement returns XDocument but the compiler automatically returns Task(Of XDocument).
Although these first important considerations might sound confusing, you will soon appreciate the benefits of using the Async pattern. It enables you to avoid multithreading and explicit callbacks, enabling you to write much easier and cleaner code. In the next section, you get started with the Async pattern with a practical example so that all the concepts described so far will be explained better.
Where Do I Use Async?
The Async libraries are in the .NET Framework 4.6, so you can use the pattern in whatever technology uses .NET 4.6. WPF, Windows Forms, ASP.NET, and even Windows Store apps. These are all technologies that can leverage the power of these libraries.
In other words, you have no limits in using the new pattern and should always use this new way to asynchrony.
When and Why to Use Async/Await and Comparisons with the TPL
Using the Async pattern enables you to write applications that are more responsive and that perform better, but you will not use it everywhere. In fact, there are specific situations in which Async has benefits. As a general rule, Async’s main purpose is to keep the UI responsive while tasks running in the UI thread might become potentially blocking. You use Async in the following scenarios:
- Potentially blocking tasks running in the user interface thread
- Image processing
- I/O operations (disk, networking, web access)
- Working with sockets
Using Async and Await differs from parallel programming because the purpose is not to have pieces of code that run concurrently; instead, the purpose is keeping the user interface responsive. In parallel programming, you have code that is CPU-consuming, so you use Task instances to split the execution of your code into multiple units of work. Thus, most of the code is executed at the same time by using a multicore architecture. In Async, instead, you do not have CPU-consuming code. You might have potentially blocking tasks, though, so your goal is to keep the UI thread free. This is possible because the result of an Await expression is delayed and control is yielded to the caller while waiting.