Why was “SwitchTo” removed from Async CTP / Release?

Stephen Toub has some more information on the reasoning in this thread.

To summarize, it’s not a good idea for two reasons:

  1. It promotes unstructured code. If you have “heavy processing” that you need to do, it should be placed in a Task.Run. Even better, separate your business logic from your UI logic.
  2. Error handling and (some) continuations run in an unknown context. catch/finally blocks in Test would need to handle running in a thread pool or UI context (and if they’re running in the thread pool context, they can’t use SwitchTo to jump on the UI context). Also, as long as you await the returned Task you should be OK (await will correct the continuation context if necessary), but if you have explicit ContinueWith continuations that use ExecuteSynchronously, then they’ll have the same problem as the catch/finally blocks.

In short, the code is cleaner and more predictable without SwitchTo.

Leave a Comment