C# Reference ยท Async and Concurrency
SynchronizationContext
Reference for SynchronizationContext in C#: how await captures it, Post versus Send, writing a single-threaded context, TaskScheduler and deadlock causes.
SynchronizationContext is an abstraction for "where should this piece of code run?". UI frameworks install one on the UI thread so that work posted from other threads is queued back to it. When you await inside a UI event handler, the continuation is posted to that context, which is why you can touch controls after the await.
Console apps and ASP.NET Core have no context (SynchronizationContext.Current is null), so continuations just run on the thread pool. You will meet this type mostly when reading UI code, writing a library, or building a custom scheduler for tests.
SynchronizationContext? ctx = SynchronizationContext.Current; ctx?.Post(_ => UpdateUi(), null);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
public class SynchronizationContext { public static SynchronizationContext? Current { get; } public static void SetSynchronizationContext(SynchronizationContext? syncContext); public virtual void Post(SendOrPostCallback d, object? state); public virtual void Send(SendOrPostCallback d, object? state); public virtual SynchronizationContext CreateCopy(); } public delegate void SendOrPostCallback(object? state);
| Member | Description |
|---|---|
Current |
The context of the current thread, or null. |
SetSynchronizationContext |
Installs a context on the current thread. |
Post(d, state) |
Queues d to run on the target context and returns immediately (asynchronous). |
Send(d, state) |
Runs d on the target context and waits until it finishes (synchronous). Dangerous on the same thread. |
CreateCopy() |
Returns a copy that flows with ExecutionContext. |
Returns: Current is a nullable reference; Post and Send return nothing.
Errors: exceptions thrown by the posted callback reach whatever the context's implementation does with them (UI frameworks usually raise them on the UI thread). Send can deadlock when the target thread is the one waiting for it.
Usage notes
awaitcaptures it. IfSynchronizationContext.Currentis notnullat theawait, the continuation is posted to it; otherwise the currentTaskScheduleris used.ConfigureAwait(false)skips both.- Deadlock recipe. On a context that runs one item at a time, calling
task.Wait()or.Resulton the context thread blocks it, while the task's continuation waits to be posted back to that same thread. Neither can move. - No context in ASP.NET Core. Older ASP.NET (System.Web) had a context that restored
HttpContext; modern ASP.NET Core and console apps do not, so blocking there usually "works" but wastes pool threads. - Writing your own is rare. It is useful for tests that need single-threaded determinism or for libraries that host an event loop. For tests, prefer injecting a
TaskSchedulerorTimeProvider. - It is per thread, stored in
ExecutionContext, and flows acrossawaitinto continuations unless you suppress it. Postshould preserve order if your code depends on messages running in sequence; the default implementation uses the thread pool and does not.- UI frameworks provide their own (
DispatcherSynchronizationContext,WindowsFormsSynchronizationContext); their types are not available on our runner.