Mark A. Donohoe
Mark A. Donohoe

Reputation: 30478

How do you get the current SynchronizationContext for a specific WPF window?

Ok, I'm a little confused here. Going over code relating to the SynchronzationContext, specifically when used in WPF so it's actually a DispatcherSynchronizationContext. Below is an excerpt from MSDN Magazine about this class:

DispatcherSynchronizationContext (WindowsBase.dll: System.Windows.Threading) WPF and Silverlight applications use a DispatcherSynchronizationContext, which queues delegates to the UI thread’s Dispatcher with “Normal” priority. This SynchronizationContext is installed as the current context when a thread begins its Dispatcher loop by calling Dispatcher.Run. The context for DispatcherSynchronizationContext is a single UI thread.

All delegates queued to the DispatcherSynchronizationContext are executed one at a time by a specific UI thread in the order they were queued. The current implementation creates one DispatcherSynchronizationContext for each top-level window, even if they all share the same underlying Dispatcher.

(From here: https://msdn.microsoft.com/magazine/gg598924.aspx)

What confuses me is the two parts that I bolded/italicized seem to be in contradiction to one another. The first seemingly says one DispatcherSynchronizationContext is created per thread and set as the thread's current sync context. However, the second excerpt says one DispatcherSynchronizationContext is created per window even if they all share the same dispatcher/UI thread.

What am I missing/not understanding?

And if it really is one per window, not just per thread, how do you get the DispatcherSynchronizationContext for a specific window?

Upvotes: 3

Views: 6014

Answers (2)

d.moncada
d.moncada

Reputation: 17402

In WPF, the SynchronizationContext is essentially a wrapper for the Dispatcher. If you look at the implementation here, you can see that the Send and Post methods wrap Invoke and BeginInvoke respectively.

What the documentation is referring to regarding the DispatcherSynchronizationContext is that it's grabbing the current UI thread to do the work, i.e., using

Application.Current.Dispatcher

while, SyncrhonizationContext is actually using the current thread's dispatcher, regardless of whethere it is UI related or not.

Thread.CurrentContext

Since all Windows run on the UI thread, there is no way to get a specific DispatcherSynchronizationContext for each.

Upvotes: 1

Aleksandr Zolotov
Aleksandr Zolotov

Reputation: 1100

All UI application ( like WinForms and WPF) have special UI thread (for current Window), and SynchronzationContext.Post ( that translate in case WPF - to Dispatcher.BeginInvoke) set sended delegate as parameter into the UI thread queue for current window. So - each Window have own SynchronzationContext, but in thread you can use different SynchronzationContext.

And one point more - each thread have ExecutionContext, but only UI threads have SynchronzationContext ( in other - for example from CLR ThredPool - SynchronzationContext=null). And when you capture context:

ExecutionContext ec = ExecutionContext.Capture();

inside ExecutionContext you will have SynchronzationContext, so

Thread.CurrentContext

it's about ExecutionContext , SynchronzationContext you can grab by

var sc = SynchronizationContext.Current; 

Upvotes: 0

Related Questions