This question already has an answer here:
I was messing around with async/await in C# just to dig into some of the thread control flow and stumbled upon an unusual behavior that I would really appreciate clarification on.
It would make sense that the execution after await continues on a calling thread even if the Task itself was executed in background. And in fact that's exactly what happens with, let's say, WPF.
The following code:
private async void Button_Click(object sender, RoutedEventArgs e)
{
Console.WriteLine($"Start. Thread: {Thread.CurrentThread.ManagedThreadId}");
await Task.Run(async () => await Task.Delay(1000));
Console.WriteLine($"End. Thread: {Thread.CurrentThread.ManagedThreadId}");
}
Results in:
Start. Thread: 1
End. Thread: 1
I realize that that's the way to make the program flow predictable etc.
But what surprised me is that the async Main method feature of .NET Console applications shows somewhat different behavior.
The same code:
static async Task Main(string[] args)
{
Console.WriteLine($"Start. Thread: {Thread.CurrentThread.ManagedThreadId}");
await Task.Run(async () => await Task.Delay(1000));
Console.WriteLine($"End. Thread: {Thread.CurrentThread.ManagedThreadId}");
}
Results in a different thread control flow:
Start. Thread: 1
End. Thread: 5
My guess is that the console application has a different concept of a synchronization context and are not bound to a main "UI" thread unlike WPF. But i'm actually struggling to find some clear info apropos of this.
In short, When the
SynchronizationContext.Current
not is set, (which is the case on a console application). The await response is invoked on theThreadPool
.On a Winforms/WPF a SynchronizationContext is implemented to queue the response to either the winforms
controlToSendTo.BeginInvoke();
or the WPFDispatcher.BeginInvoke();
.Reference:
Await, SynchronizationContext, and Console Apps (a blog post by a member of the dev team):
Parallel Computing - It's All About the SynchronizationContext (an article referenced from the official documentation for the
SynchronizationContext
class):