Summary
In this chapter, we began with a high-level overview of the task-based asynchronous pattern. Things we covered were naming, parameters, return types, initializing asynchronous operations, exceptions, and optionally providing ways to report progress updates and cancel operations. We saw that we can have asynchronous operations that allow cancellation, and those that don't allow cancellation. Plus, we learned that when a cancellation has been requested, the cancellation will either go ahead or be ignored. Completed tasks can have a completed state of Canceled
, RanToCompletion
, or Faulted
.
We then benchmarked three different ways of calling a method synchronously, using Task.Run
, and asynchronously. Using Task.Run
took the longest time, followed by running the method synchronously, and running the method asynchronously was by far the quickest way to run the method.
Then we benchmarked GetAwaiter.GetResult()
, Result
, and Wait
for both Task
and TaskValue
. We saw that...