In short, When the SynchronizationContext.Current
not is set, (which is the case on a console application). The await response is invoked on the ThreadPool
.
On a Winforms/WPF a SynchronizationContext is implemented to queue the response to either the winforms controlToSendTo.BeginInvoke();
or the WPF Dispatcher.BeginInvoke();
.
Reference:
Await, SynchronizationContext, and Console Apps (a blog post by a member of the dev team):
But there's one common kind of application that doesn't have a SynchronizationContext
: console apps. When your console application's Main
method is invoked, SynchronizationContext.Current
will return null
. That means that if you invoke an asynchronous method in your console app, unless you do something special, your asynchronous methods will not have thread affinity: the continuations within those asynchronous methods could end up running "anywhere."
Parallel Computing - It's All About the SynchronizationContext (an article referenced from the official documentation for the SynchronizationContext
class):
By default, all threads in console applications and Windows Services only have the default SynchronizationContext
.
...
Figure 4 Summary of SynchronizationContext
Implementations
...
╔═════════╦═══════════╦════════════╦════════════╦══════════╦══════════╗
║ ║ Specific ║ Exclusive ║ Ordered ║ Send May ║ Post May ║
║ ║ Thread ║ (Delegates ║ (Delegates ║ Invoke ║ Invoke ║
║ ║ Used to ║ Execute ║ Execute ║ Delegate ║ Delegate ║
║ ║ Execute ║ One at ║ in Queue ║ Directly ║ Directly ║
║ ║ Delegates ║ a Time) ║ Order) ║ ║ ║
╠═════════╬═══════════╬════════════╬════════════╬══════════╬══════════╣
║ ... ║ ║ ║ ║ ║ ║
╠═════════╬═══════════╬════════════╬════════════╬══════════╬══════════╣
║ Default ║ No ║ No ║ No ║ Always ║ Never ║
╚═════════╩═══════════╩════════════╩════════════╩══════════╩══════════╝