Is the phrase from the book "Current SynchronizationContext is a property of the current thread" correct "?

After reading the phrase "The current SynchronizationContext is a property of the current thread" correct " , I got a little confused ...

In C # application code in VS2010 when I type Thread.CurrentThread. , I do not find in the drop-down list of options set by Intellisense for any context-related properties for the stream.

I know that the current synchronization context can be obtained through " = SynchronizationContext.Current; ". But this is not entirely successful with simultaneous execution in parallel threads, tasks, etc.

Suppose that from a console or WPF (*) application, I create and run several Windows forms in my own main UI threads , as well as TPL tasks.

I underestimated that each winform should have its own WindowsFormaSynchronizationContext , WPF should have its own DispatcherSynchronizationContext (subclasses of the SynchronizationContext class ), tasks are executed in ThreadPool with their own synchronization context, LongRunning task can be executed from the thread pool in its own synchronization context ...

So why can't you define a SynchronizationContext from stream (s)? All the answers to the question "Get synchronization from a given stream" seem unperturbed in denying the possibility ...

And last but not least:
Is the phrase "Current SynchronizationContext a property of the current thread" correct " correctly?
Then, how can I get the value of this property for different specific thread instances?

(*)
Recently, I was assigned a C # WPF application code, mainly using winforms.

+2
multithreading c # task-parallel-library synchronizationcontext thread-synchronization
source share
1 answer

That's for sure. The SynchronizationContext.Current property uses the current field m_ExecutionContext of the stream. Which is a private field of the Thread class, so you do not see it in the IntelliSense drop-down list.

It is important that it works that way; by default, the SynchronizationContext does not synchronize anything. Its Post () target runs in threadpool thread. Marching a target call to a specific thread is a very non-trivial thing. This requires help from the target topic, it should provide a solution to the problem of the producer-consumer . A common solution is a loop that retrieves messages from a streaming network. Exactly the way the Winforms or WPF application user interface works, they "accumulate a message loop." Application.Run () starts this loop.

Thus, only the user interface thread of such an application can support a synchronization provider that does not use threadpool threads to run the Post () task. Accordingly, Winforms and WPF install their own synchronization provider as soon as you create a form or window. And only code that runs in the user interface thread will see that the non-standard provider is from the SynchronizationContext.Current property.

The consequence is that you must initialize code that must reroute calls back to the user interface thread in the user interface thread. So, for example, the creation of BackgroundWorker should be done in the user interface thread. Or a task created using TaskScheduler.FromCurrentSynchronizationContext. Technically, there can be more than one thread that displays the user interface, regardless of which thread runs the initialization code, determines where the Post () delegation goal will be executed. This probably explains your problem, if the initialization code is running on a worker thread, then the Post () target runs on the threadpool thread. You can pass the link to the Synchronization.Current object to the workflow if you received this link in the user interface thread.

+7
source share

All Articles