From my understanding one of the main things that async and await do is to make code easy to write and read - but is using them equal to spawning background threads to perfo
Here is a quick example of async
/await
at a high level. There are a lot more details to consider beyond this.
Note: Task.Delay(1000)
simulates doing work for 1 second. I think it's best to think of this as waiting for a response from an external resource. Since our code is waiting for a response, the system can set the running task off to the side and come back to it once it's finished. Meanwhile, it can do some other work on that thread.
In the example below, the first block is doing exactly that. It starts all the tasks immediately (the Task.Delay
lines) and sets them off to the side. The code will pause on the await a
line until the 1 second delay is done before going to the next line. Since b
, c
, d
, and e
all started executing at almost the exact same time as a
(due to lack of the await), they should finish at roughly the same time in this case.
In the example below, the second block is starting a task and waiting for it to finish (that is what await
does) before starting the subsequent tasks. Each iteration of this takes 1 second. The await
is pausing the program and waiting for the result before continuing. This is the main difference between the first and second blocks.
Console.WriteLine(DateTime.Now);
// This block takes 1 second to run because all
// 5 tasks are running simultaneously
{
var a = Task.Delay(1000);
var b = Task.Delay(1000);
var c = Task.Delay(1000);
var d = Task.Delay(1000);
var e = Task.Delay(1000);
await a;
await b;
await c;
await d;
await e;
}
Console.WriteLine(DateTime.Now);
// This block takes 5 seconds to run because each "await"
// pauses the code until the task finishes
{
await Task.Delay(1000);
await Task.Delay(1000);
await Task.Delay(1000);
await Task.Delay(1000);
await Task.Delay(1000);
}
Console.WriteLine(DateTime.Now);
OUTPUT:
5/24/2017 2:22:50 PM
5/24/2017 2:22:51 PM (First block took 1 second)
5/24/2017 2:22:56 PM (Second block took 5 seconds)
Note: This is where things get a little foggy for me, so if I'm wrong on anything, please correct me and I will update the answer. It's important to have a basic understanding of how this works but you can get by without being an expert on it as long as you never use ConfigureAwait(false)
, although you will likely lose out on some opportunity for optimization, I assume.
There is one aspect of this which makes the async
/await
concept somewhat trickier to grasp. That's the fact that in this example, this is all happening on the same thread (or at least what appears to be the same thread in regards to its SynchronizationContext
). By default, await
will restore the synchronization context of the original thread that it was running on. For example, in ASP.NET you have an HttpContext
which is tied to a thread when a request comes in. This context contains things specific to the original Http request such as the original Request object which has things like language, IP address, headers, etc. If you switch threads halfway through processing something, you could potentially end up trying to pull information out of this object on a different HttpContext
which could be disastrous. If you know you won't be using the context for anything, you can choose to "not care" about it. This basically allows your code to run on a separate thread without bringing the context around with it.
How do you achieve this? By default, the await a;
code actually is making an assumption that you DO want to capture and restore the context:
await a; //Same as the line below
await a.ConfigureAwait(true);
If you want to allow the main code to continue on a new thread without the original context, you simply use false instead of true so it knows it doesn't need to restore the context.
await a.ConfigureAwait(false);
After the program is done being paused, it will continue potentially on an entirely different thread with a different context. This is where the performance improvement would come from -- it could continue on on any available thread without having to restore the original context it started with.
Is this stuff confusing? Hell yeah! Can you figure it out? Probably! Once you have a grasp of the concepts, then move on to Stephen Cleary's explanations which tend to be geared more toward someone with a technical understanding of async
/await
already.