SomeLongJobAsync starts execution on the thread that called it, and if it is, the current SynchronizationContext stored in the state machine generated by the await mechanism.
Upon completion of the first await continuation of the method is published in the current SynchronizationContext . In a graphical application, this means that the continuation is performed in the user interface thread. There is no SyncrhronizatonContext in the console application, so the continuation is done in the thread pool thread.
You can verify this by printing ManagedThreadId Thread.CurrentThread as your program Thread.CurrentThread . Consider this modified version of your code (which I ran from a console application on Linqpad):
private async void btnSlowPoke_Click(object sender, EventArgs e) { await DoItAsync(); } private async Task<int> SomeLongJobAsync() { Console.WriteLine("Start SomeLongJobAsync, threadId = " + Thread.CurrentThread.ManagedThreadId); for (int x = 0; x < 9; x++) {
Exiting the console application:
She'll be coming round the mountain, threadId = 21 Start SomeLongJobAsync, threadId = 21 when she comes., threadId = 21 Continue SomeLongJobAsync, threadId = 11 Continue SomeLongJobAsync, threadId = 11 Continue SomeLongJobAsync, threadId = 11 Continue SomeLongJobAsync, threadId = 11 Continue SomeLongJobAsync, threadId = 11 Continue SomeLongJobAsync, threadId = 12 Continue SomeLongJobAsync, threadId = 12 Continue SomeLongJobAsync, threadId = 12 Continue SomeLongJobAsync, threadId = 12
As you can see, the method started runnung in thread 21, but as each of them completed, it continued in the thread pool thread and was not always the same. In this case, 11, 12. If I ran this in a Windows Forms application, it will be as follows:
Exit the Windows Forms application:
She'll be coming round the mountain, threadId = 8 Start SomeLongJobAsync, threadId = 8 when she comes., threadId = 8 Continue SomeLongJobAsync, threadId = 8 Continue SomeLongJobAsync, threadId = 8 Continue SomeLongJobAsync, threadId = 8 Continue SomeLongJobAsync, threadId = 8 Continue SomeLongJobAsync, threadId = 8 Continue SomeLongJobAsync, threadId = 8 Continue SomeLongJobAsync, threadId = 8 Continue SomeLongJobAsync, threadId = 8 Continue SomeLongJobAsync, threadId = 8