I have async / await intro on my blog where I will explain exactly what context is:
This is SynchronizationContext.Current , if it is not null , in which case it is TaskScheduler.Current . Note: if TaskScheduler no TaskScheduler current, then TaskScheduler.Current same as TaskScheduler.Default , which is a thread pool task scheduler.
In today's code, it usually comes down to whether you have a SynchronizationContext ; Task schedulers are not used today today (but are likely to become more common in the future). I have an article on the SynchronizationContext that describes how it works, and some of the implementations provided by .NET.
WinRT and other user interface interfaces (WinForms, WPF, Silverlight) provide a SynchronizationContext for the main user interface thread. This context is only one thread, so if you mix lock and asynchronous code, you can quickly see deadlocks. I describe why this happens in more detail in a blog post , but in general the reason it is deadlocked is because the async method tries to re-enter its SynchronizationContext (in this case, resume execution in the user interface thread), but the user interface thread The interface is blocked, waiting for the completion of this async method.
The thread pool does not have a SynchronizationContext (or TaskScheduler , usually). Therefore, if you execute a thread thread stream and block asynchronous code, it will not block. This is because the context captured is a thread pool context (which is not tied to a specific thread), so the async method can re-enter its context (just by working in a thread pool thread) while another thread pool thread is blocked waiting for its completion.
The answer to this question will determine if I really mess up our code by inserting a lot of conditional asynchronous / pending keywords, or I will keep it clean and just use the thread that executes Wait () here.
If your async code is complete, it should not look messy. I'm not sure what you mean by "conditional"; I would just do all this async . await has a "fast path" implementation that makes it synchronous if the operation is already completed.
Blocking asynchronous code using a background thread is possible, but it has some caveats:
- You do not have a user interface context, so you cannot do many user interfaces.
- You still have to “synchronize” with the user interface thread, and your user interface thread should not be blocked (for example, it should
await Task.Run(..) , not Task.Run(..).Wait() ). This is especially true for WinRT applications.