Just as a lambda expression converted to a delegate can be used as a concise syntax for declaring a normal method, lambdas containing await expressions can be converted to delegates14. To do so, just precede the lambda expression with the async keyword. In Listing 20.10, we first assign an async lambdato a Func<string, Task> writeWebRequestSizeAsync variable. We then use the await operator to invoke it.
Similarly, the same effect can be achieved with a local function15. For example, in Listing 20.10, you could change the lambda expression header (everything up to and including the => operator) to
async Task WriteWebRequestSizeAsync(string webRequestUrl)
leaving everything in the body, including the curly braces, unchanged.
Note that an async lambda expression has the same restrictions as the named async method:
Implementing an asynchronous method by relying on other asynchronous methods (which, in turn, rely on more asynchronous methods) is relatively easy with the await keyword. However, at some point in the call hierarchy, it becomes necessary to write a “leaf” asynchronous Task-returning method. Consider, for example, an asynchronous method for running a command-line program with the eventual goal that the output could be accessed. Such a method would be declared as follows:
public static Task<Process> RunProcessAsync(string filename)
The simplest implementation would, of course, be to rely on Task.Run() again and call both the System.Diagnostics.Process’s Start() and WaitForExit() methods. However, creating an additional thread in the current process is unnecessary when the invoked process itself will have its own collection of one or more threads. To implement the RunProcessAsync() method and return to the caller’s synchronization context when the invoked process completes, we can rely on a TaskCompletionSource<T> object, as shown in Listing 20.11.
Ignore the highlighting for the moment and instead focus on the pattern of using an event for notification when the process completes. Since System.Diagnostics.Process includes a notification upon exit, we register for this notification and use it as a callback from which we can invoke TaskCompletionSource.SetResult(). The code in Listing 20.11 follows a fairly common pattern that you can use to create an asynchronous method without having to resort to Task.Run().
Another important characteristic that an async method might require is cancellation. TAP relies on the same methods for cancellation as the TPL does—namely, a System.Threading.CancellationToken. Listing 20.11 highlights the code necessary to support cancellation. In this example, we allow for canceling before the process ever starts, as well as an attempt to close the application’s main window (if there is one). A more aggressive approach would be to call Process.Kill(), but this method could potentially cause problems for the program that is executing.
Notice that we don’t register for the cancellation event until after the process is started. This avoids any race conditions that might occur if cancellation is triggered before the process actually begins.
One last feature to consider supporting is a progress update. Listing 20.12 is the full version of RunProcessAsync() with just such an update.
Wrapping your head around precisely what is happening in an async method can be difficult, but it is far less difficult than trying to figure out what asynchronous code written with explicit continuations in lambdas is doing. The key points to remember are as follows: