20

Programming the Task-Based Asynchronous Pattern

As we saw in Chapter 19, tasks provide an abstraction for the manipulation of asynchronous work. Tasks are automatically scheduled to the right number of threads, and large tasks can be composed by chaining together small tasks, just as large programs can be composed from multiple small methods.

However, there are some drawbacks to tasks. The principal difficulty with tasks is that they turn your program logic “inside out.” To illustrate this, we begin the chapter with a synchronous method that is blocked on an I/O-bound, high-latency operation—a web request. We then revise this method by leveraging the async/await contextual keywords,1 demonstrating a significant simplification in authoring and readability of asynchronous code.

We finish the chapter with a look at asynchronous streams—a C# 8.0–introduced feature for defining and leveraging asynchronous iterators.

Synchronously Invoking a High-Latency Operation

In Listing 20.1, the code uses a HttpClient to download a web page and search for the number of times some text appears. Output 20.1 shows the results.

Listing 20.1: A Synchronous Web Request
1. using System;
2. using System.IO;
3. using System.Net;
4.  
5. public static class Program
6. {
7.     public static HttpClient HttpClient { getset; } = new();
8.     public const string DefaultUrl = "https://IntelliTect.com";
9.  
10.     public static void Main(string[] args)
11.     {
12.         if (args.Length == 0)
13.         {
14.             Console.WriteLine("ERROR: No findText argument specified.");
15.             return;
16.         }
17.         string findText = args[0];
18.  
19.         string url = DefaultUrl;
20.         if (args.Length > 1)
21.         {
22.             url = args[1];
23.             // Ignore additional parameters
24.         }
25.         Console.WriteLine(
26.             $"Searching for '{findText}' at URL '{url}'.");
27.  
28.         Console.WriteLine("Downloading...");
29.         byte[] downloadData =
30.             HttpClient.GetByteArrayAsync(url).Result;
31.  
32.         Console.WriteLine("Searching...");
33.         int textOccurrenceCount = CountOccurrences(
34.             downloadData, findText);
35.  
36.         Console.WriteLine(
37.             @$"'{findText}' appears {
38.                 textOccurrenceCount} times at URL '{url}'.");
39.     }
40.  
41.     private static int CountOccurrences(byte[] downloadData, string findText)
42.     {
43.         int textOccurrenceCount = 0;
44.  
45.         using MemoryStream stream = new(downloadData);
46.         using StreamReader reader = new(stream);
47.  
48.         int findIndex = 0;
49.         int length = 0;
50.         do
51.         {
52.             char[] data = new char[reader.BaseStream.Length];
53.             length = reader.Read(data);
54.             for (int i = 0; i < length; i++)
55.             {
56.                 if (findText[findIndex] == data[i])
57.                 {
58.                     findIndex++;
59.                     if (findIndex == findText.Length)
60.                     {
61.                         // Text was found
62.                         textOccurrenceCount++;
63.                         findIndex = 0;
64.                     }
65.                 }
66.                 else
67.                 {
68.                     findIndex = 0;
69.                 }
70.             }
71.         }
72.         while (length != 0);
73.  
74.         return textOccurrenceCount;
75.     }
76. }
Output 20.1
Searching for 'IntelliTect'...
http://www.IntelliTect.com
Downloading...
Searching...
'IntelliTect' appears 35 times at URL 'http://www.IntelliTect.com'.

The logic in Listing 20.1 is relatively straightforward—using common C# idioms. After determining the url and findText values, Main(), using an existing HttpClient instance, it invokes the asynchronous method GetByteArrayAsync() to download the content. It calls .Result to force the asynchronous method to execute synchronously and return the result. This approach to making asynchronous methods run synchronously is not recommend as it can cause deadlocks. Given the downloaded data, it passes this data to CountOccurrences(), which loads it into a MemoryStream and leverages a StreamReader’s Read() method to retrieve a block of data and search it for the findText value. (We use the GetByteArrayAsync() method rather than the simpler GetStringAsync() method so that we can demonstrate an additional asynchronous invocation when reading from a stream in Listing 20.2 and Listing 20.3.)

The problem with this approach is, of course, that the calling thread is blocked until the I/O operation completes; this is wasting a thread that could be doing useful work while the operation executes. For this reason, we cannot, for example, execute any other code, such as code that asynchronously indicates progress. In other words, “Download…” and “Searching…” are invoked before their corresponding operations, not during the operation. While doing so would be irrelevant here, imagine that we wanted to concurrently execute additional work or, at a minimum, provide an animated busy indicator.

________________________________________

1. Introduced in C# 5.0.
{{ snackbarMessage }}
;