Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

2
  • 2
    The code I've shown is (very) simplified. Inside GetWorkAsync() there are multiple further awaits. Some of them access the database and network, which means true I/O. As I understand, the thread switch is a natural consequence (albeit not required) of such awaits, because the initial thread doesn't establish any synchronization context that would govern where the continuations should execute. So they execute on a thread pool thread. Is my reasoning wrong? Commented Feb 19, 2017 at 15:29
  • @aoven Good point - I didn't consider the different kinds of SynchronizationContext - that is certainly important since .ContinueWith() uses the SynchronizationContext to dispatch the continuation; it would indeed explain the behaviour you're seeing if await is called on a ThreadPool thread or an ASP.NET thread. A continuation could certainly be dispatched to a different thread in those cases. On the other hand, calling await on a single-threaded context such as a WPF Dispatcher or Winforms context should be enough to ensure the continuation happens on the original. thread Commented Feb 20, 2017 at 10:43