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.
Hans passant
source share