I thought await continued on the same thread as the caller, but it seems not to

When you await, by default the await operator will capture the current "context" and use that to resume the async method.

This "context" is SynchronizationContext.Current unless it is null, in which case it is TaskScheduler.Current. (If there is no currently-running task, then TaskScheduler.Current is the same as TaskScheduler.Default, the thread pool task scheduler).

It's important to note that a SynchronizationContext or TaskScheduler does not necessarily imply a particular thread. A UI SynchronizationContext will schedule work to the UI thread; but the ASP.NET SynchronizationContext will not schedule work to a particular thread.

I suspect that the cause of your problem is that you are invoking the async code too early. When an application starts, it just has a plain old regular thread. That thread only becomes the UI thread when it does something like Application.Run.


The await expression will use the value of SynchronizationContext.Current to return control flow back to the thread on which it occurred. In cases where this is null it will default to the TaskScheduler.Current. The implementation relies solely on this value to change the thread context when the Task value completes. It sounds like in this case the await is capturing a context that isn't bound to the UI thread