178

In the book Programming C#, it has some sample code about SynchronizationContext:

SynchronizationContext originalContext = SynchronizationContext.Current;
ThreadPool.QueueUserWorkItem(delegate {
    string text = File.ReadAllText(@"c:\temp\log.txt");
    originalContext.Post(delegate {
        myTextBox.Text = text;
    }, null);
});

I'm a beginner in threads, so please answer in detail. First, I don't know what does context mean, what does the program save in the originalContext? And when the Post method is fired, what will the UI thread do?
If I ask some silly things, please correct me, thanks!

EDIT: For example, what if I just write myTextBox.Text = text; in the method, what's the difference?

4
  • 1
    The fine manual has this to say The purpose of the synchronization model implemented by this class is to allow the internal asynchronous/synchronization operations of the common language runtime to behave properly with different synchronization models. This model also simplifies some of the requirements that managed applications have had to follow in order to work correctly under different synchronization environments. Aug 7, 2013 at 7:44
  • IMHO async await already does this
    – Royi Namir
    Aug 7, 2013 at 7:54
  • 9
    @RoyiNamir: Yes, but guess what: async/await relies on SynchronizationContext underneath. Aug 7, 2013 at 8:55
  • 2
    This article by Stephen Toub is excellent, Await, SynchronizationContext, and Console Apps
    – Timeless
    Sep 9, 2021 at 6:11

8 Answers 8

231

What does SynchronizationContext do?

Simply put, SynchronizationContext represents a location "where" code might be executed. Delegates that are passed to its Send or Post method will then be invoked in that location. (Post is the non-blocking / asynchronous version of Send.)

Every thread can have a SynchronizationContext instance associated with it. The running thread can be associated with a synchronization context by calling the static SynchronizationContext.SetSynchronizationContext method, and the current context of the running thread can be queried via the SynchronizationContext.Current property.

Despite what I just wrote (each thread having an associated synchronization context), a SynchronizationContext does not necessarily represent a specific thread; it can also forward invocation of the delegates passed to it to any of several threads (e.g. to a ThreadPool worker thread), or (at least in theory) to a specific CPU core, or even to another network host. Where your delegates end up running is dependent on the type of SynchronizationContext used.

Windows Forms will install a WindowsFormsSynchronizationContext on the thread on which the first form is created. (This thread is commonly called "the UI thread".) This type of synchronization context invokes the delegates passed to it on exactly that thread. This is very useful since Windows Forms, like many other UI frameworks, only permits manipulation of controls on the same thread on which they were created.

What if I just write myTextBox.Text = text; in the method, what's the difference?

The code that you've passed to ThreadPool.QueueUserWorkItem will be run on a thread pool worker thread. That is, it will not execute on the thread on which your myTextBox was created, so Windows Forms will sooner or later (especially in Release builds) throw an exception, telling you that you may not access myTextBox from across another thread.

This is why you have to somehow "switch back" from the worker thread to the "UI thread" (where myTextBox was created) before that particular assignment. This is done as follows:

  1. While you are still on the UI thread, capture Windows Forms' SynchronizationContext there, and store a reference to it in a variable (originalContext) for later use. You must query SynchronizationContext.Current at this point; if you queried it inside the code passed to ThreadPool.QueueUserWorkItem, you might get whatever synchronization context is associated with the thread pool's worker thread. Once you have stored a reference to Windows Forms' context, you can use it anywhere and at any time to "send" code to the UI thread.

  2. Whenever you need to manipulate a UI element (but are not, or might not be, on the UI thread anymore), access Windows Forms' synchronization context via originalContext, and hand off the code that will manipulate the UI to either Send or Post.


Final remarks and hints:

  • What synchronization contexts won't do for you is telling you which code must run in a specific location / context, and which code can just be executed normally, without passing it to a SynchronizationContext. In order to decide that, you must know the rules and requirements of the framework you're programming against — Windows Forms in this case.

    So remember this simple rule for Windows Forms: DO NOT access controls or forms from a thread other than the one that created them. If you must do this, use the SynchronizationContext mechanism as described above, or Control.BeginInvoke (which is a Windows Forms-specific way of doing exactly the same thing).

  • If you're programming against .NET 4.5 or later, you can make your life much easier by converting your code that explicitly uses SynchronizationContext, ThreadPool.QueueUserWorkItem, control.BeginInvoke, etc. over to the new async / await keywords and the Task Parallel Library (TPL), i.e. the API surrounding the Task and Task<TResult> classes. These will, to a very high degree, take care of capturing the UI thread's synchronization context, starting an asynchronous operation, then getting back onto the UI thread so you can process the operation's result.

8
  • You say Windows Forms, like many other UI frameworks, only permits manipulation of controls on the same thread but all windows in Windows must be accessed by the same thread that created it.
    – Sam Hobbs
    Jan 16, 2017 at 1:40
  • 5
    @user34660: No, that is not correct. You can have several threads that create Windows Forms controls. But each control is associated with the one thread that created it, and must only be accessed by that one thread. Controls from different UI threads are also very limited in how they interact with each other: one cannot be the parent/child of the other, data binding between them isn't possible, etc. Finally, each thread that creates controls needs its own message loop (which gets started by Application.Run, IIRC). This is a fairly advanced topic and not something casually done. Jan 24, 2017 at 8:55
  • My first comment is due to you saying "like many other UI frameworks" implying that some windows allow "manipulation of controls" from a different thread but no Windows windows do. You cannot "have several threads that create Windows Forms controls" for the same window and "must be accessed by the same thread" and "must only be accessed by that one thread" are saying the same thing. I doubt that it is possible to create "Controls from different UI threads" for the same window. All of this is not advanced for those of us experienced with Windows programming before .Net.
    – Sam Hobbs
    Jan 24, 2017 at 22:09
  • 4
    All this talk about "windows" and "Windows windows" is making me rather dizzy. Did I mention any of these "windows"? I don't think so... Jan 24, 2017 at 23:43
  • 1
    @ibubi: I'm not sure I understand your question. Any thread's synchronization context is either not set (null) or an instance of SynchronizationContext (or a subclass of it). The point of that quote was not what you get, but what you won't get: the UI thread's synchronization context. Aug 2, 2017 at 19:20
31

I'd like to add to other answers, SynchronizationContext.Post just queues a callback for later execution on the target thread (normally during the next cycle of the target thread's message loop), and then execution continues on the calling thread. On the other hand, SynchronizationContext.Send tries to execute the callback on the target thread immediately, which blocks the calling thread and may result in deadlock. In both cases, there is a possibility for code reentrancy (entering a class method on the same thread of execution before the previous call to the same method has returned).

If you're familiar with Win32 programming model, a very close analogy would be PostMessage and SendMessage APIs, which you can call to dispatch a message from a thread different from the target window's one.

Here is a very good explanation of what synchronization contexts are: It's All About the SynchronizationContext.

20

It stores the synchronization provider, a class derived from SynchronizationContext. In this case that will probably be an instance of WindowsFormsSynchronizationContext. That class uses the Control.Invoke() and Control.BeginInvoke() methods to implement the Send() and Post() methods. Or it can be DispatcherSynchronizationContext, it uses Dispatcher.Invoke() and BeginInvoke(). In a Winforms or WPF app, that provider is automatically installed as soon as you create a window.

When you run code on another thread, like the thread-pool thread used in the snippet, then you have to be careful that you don't directly use objects that are thread-unsafe. Like any user interface object, you must update the TextBox.Text property from the thread that created the TextBox. The Post() method ensures that the delegate target runs on that thread.

Beware that this snippet is a bit dangerous, it will only work correctly when you call it from the UI thread. SynchronizationContext.Current has different values in different threads. Only the UI thread has a usable value. And is the reason the code had to copy it. A more readable and safer way to do it, in a Winforms app:

    ThreadPool.QueueUserWorkItem(delegate {
        string text = File.ReadAllText(@"c:\temp\log.txt");
        myTextBox.BeginInvoke(new Action(() => {
            myTextBox.Text = text;
        }));
    });

Which has the advantage that it works when called from any thread. The advantage of using SynchronizationContext.Current is that it still works whether the code is used in Winforms or WPF, it matters in a library. This is certainly not a good example of such code, you always know what kind of TextBox you have here so you always know whether to use Control.BeginInvoke or Dispatcher.BeginInvoke. Actually using SynchronizationContext.Current is not that common.

The book is trying to teach you about threading, so using this flawed example is okayish. In real life, in the few cases where you might consider using SynchronizationContext.Current, you'd still leave it up to C#'s async/await keywords or TaskScheduler.FromCurrentSynchronizationContext() to do it for you. But do note that they still misbehave the way the snippet does when you use them on the wrong thread, for the exact same reason. A very common question around here, the extra level of abstraction is useful but makes it harder to figure out why they don't work correctly. Hopefully the book also tells you when not to use it :)

4
  • I'm sorry, why let the UI thread handle is thread-safe? i.e I think the UI thread could be using myTextBox when the Post() fired, is that safe?
    – cloudyFan
    Aug 7, 2013 at 9:34
  • 5
    Your English is hard to decode. Your original snippet only works correctly when it is called from the UI thread. Which is a very common case. Only then will it post back to the UI thread. If it is called from a worker thread then the Post() delegate target will run on a threadpool thread. Kaboom. This is something you want to try for yourself. Start a thread and let the thread call this code. You did it right if the code crashes with a NullReferenceException. Aug 7, 2013 at 9:40
  • "you must update the TextBox.Text property from the thread that created the TextBox" - Why did they design it that way? Sep 28, 2021 at 22:54
  • @DavidKlempfner Probably to avoid excessive and complex locking. See this question.
    – Alex Che
    Jun 26 at 15:31
7

The purpose of the synchronization context here is to make sure that myTextbox.Text = text; gets called on the main UI thread.

Windows requires that GUI controls be accessed only by the thread they were created with. If you try assign the text in a background thread without first synchronizing (through any of several means, such as this or the Invoke pattern) then an exception will be thrown.

What this does is save the synchronization context prior to creating the background thread, then the background thread uses the context.Post method execute the GUI code.

Yes, the code you've shown is basically useless. Why create a background thread, only to immediately need to go back to the main UI thread? It's just an example.

3
  • 8
    "Yes, the code you've shown is basically useless. Why create a background thread, only to immediately need to go back to the main UI thread? It's just an example." - Reading from a file might be a long task if the file is big, something that may block the UI thread and make it unresponsive
    – Yair Nevet
    Jan 20, 2016 at 15:43
  • I have a stupid question. Every thread has an Id, and I suppose the UI thread also have an ID= 2 for example. Then, when I'm on thread pool thread, Can I do something like that: var thread = GetThread(2); thread.Execute(() => textbox1.Text = "foo") ?
    – John
    Aug 15, 2019 at 22:44
  • @John - No, I don't think that works because the thread is already executing. You can't execute an already executing thread. Execute only works when a thread is not running (IIRC) Aug 26, 2019 at 16:04
7

SynchronizationContext basically is a provider of callback delegates' execution. It is responsible for ensuring that the delegates are run in a given execution context after a particular portion of code (encapsulated inside a Task object in .Net TPL) in a program has completed its execution.

From technical point of view, SC is a simple C# class that is oriented to support and provide its function specifically for Task Parallel Library objects.

Every .Net application except for console applications has a tailored implementation of this class based on the specific underlying framework, eg: WPF, WindowsForm, Asp Net, Silverlight, etc.

The importance of this object is bound to the synchronization between results returning from asynchronous execution of code, and the execution of dependent code that is waiting for results from that asynchronous work.

And the word "context" stands for execution context. That is, the current execution context where that waiting code will be executed- namely the synchronization between async code and its waiting code happens in a specific execution context. Thus this object is named SynchronizationContext.

It represents the execution context that will look after syncronization of async code and waiting code execution.

5

To the Source

Every thread has a context associated with it -- this is also known as the "current" context -- and these contexts can be shared across threads. The ExecutionContext contains relevant metadata of the current environment or context in which the program is in execution. The SynchronizationContext represents an abstraction -- it denotes the location where your application's code is executed.

A SynchronizationContext enables you to queue a task onto another context. Note that every thread can have its own SynchronizatonContext.

For example: Suppose you have two threads, Thread1 and Thread2. Say, Thread1 is doing some work, and then Thread1 wishes to execute code on Thread2. One possible way to do it is to ask Thread2 for its SynchronizationContext object, give it to Thread1, and then Thread1 can call SynchronizationContext.Send to execute the code on Thread2.

3
  • 4
    A synchronization context isn't necessarily tied to a particular thread. It's possible for multiple threads to be handling requests to a single synchronization context, and for a single thread to be handling requests for multiple synchronization contexts.
    – Servy
    Jan 31, 2017 at 15:46
  • @Servy he wrote "Note that every thread can have its own SynchronizatonContext." which is true isn't it? "can" means "it's possible". Sep 28, 2021 at 23:07
  • 2
    @DavidKlempfner The problem isn't with the quote in the answer, but author's commentary on it. The bit you quoted isn't the problem. It's the author's statement that if you take the sync context from a thread it means it will execute the posted delegate on that thread. The bit that the answer quotes just says that a thread may have a context, without commenting on what actually happens when posting to it.
    – Servy
    Sep 29, 2021 at 14:08
3

SynchronizationContext provides us a way to update a UI from a different thread (synchronously via the Send method or asynchronously via the Post method).

Take a look at the following example:

    private void SynchronizationContext SyncContext = SynchronizationContext.Current;
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        Thread thread = new Thread(Work1);
        thread.Start(SyncContext);
    }

    private void Work1(object state)
    {
        SynchronizationContext syncContext = state as SynchronizationContext;
        syncContext.Post(UpdateTextBox, syncContext);
    }

    private void UpdateTextBox(object state)
    {
        Thread.Sleep(1000);
        string text = File.ReadAllText(@"c:\temp\log.txt");
        myTextBox.Text = text;
    }

SynchronizationContext.Current will return the UI thread's sync context. How do I know this? At the start of every form or WPF app, the context will be set on the UI thread. If you create a WPF app and run my example, you'll see that when you click the button, it sleeps for roughly 1 second, then it will show the file's content. You might expect it won't because the caller of UpdateTextBox method (which is Work1) is a method passed to a Thread, therefore it should sleep that thread not the main UI thread, NOPE! Even though Work1 method is passed to a thread, notice that it also accepts an object which is the SyncContext. If you look at it, you'll see that the UpdateTextBox method is executed through the syncContext.Post method and not the Work1 method. Take a look at the following:

private void Button_Click(object sender, RoutedEventArgs e) 
{
    Thread.Sleep(1000);
    string text = File.ReadAllText(@"c:\temp\log.txt");
    myTextBox.Text = text;
}

The last example and this one executes the same. Both doesn't block the UI while it does it jobs.

In conclusion, think of SynchronizationContext as a thread. It's not a thread, it defines a thread (Note that not all thread has a SyncContext). Whenever we call the Post or Send method on it to update a UI, it's just like updating the UI normally from the main UI thread. If, for some reasons, you need to update the UI from a different thread, make sure that thread has the main UI thread's SyncContext and just call the Send or Post method on it with the method that you want to execute and you're all set.

Hope this helps you, mate!

1

This example is from Linqpad examples from Joseph Albahari but it really helps in understanding what Synchronization context does.

void WaitForTwoSecondsAsync (Action continuation)
{
    continuation.Dump();
    var syncContext = AsyncOperationManager.SynchronizationContext;
    new Timer (_ => syncContext.Post (o => continuation(), _)).Change (2000, -1);
}

void Main()
{
    Util.CreateSynchronizationContext();
    ("Waiting on thread " + Thread.CurrentThread.ManagedThreadId).Dump();
    for (int i = 0; i < 10; i++)
        WaitForTwoSecondsAsync (() => ("Done on thread " + Thread.CurrentThread.ManagedThreadId).Dump());
}
2
  • 1
    Does it?? Can you explain what's going on with this code?
    – Hugh W
    Apr 12, 2022 at 18:51
  • depends on who looks at it :) Sep 2, 2022 at 16:47

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.