简体   繁体   English

这本书中的短语“当前同步文本是当前线程的属性是否正确”?

[英]Is the phrase from a book “The current SynchronizationContext is a property of the current thread” correct"?

Having read the phrase "The current SynchronizationContext is a property of the current thread" correct" , I am a little confused... 阅读短语“当前的SynchronizationContext是当前线程的属性”更正“ ,我有点困惑......

In a C# app code in VS2010, when I type Thread.CurrentThread. 在VS2010中的C#应用​​程序代码中,当我键入Thread.CurrentThread. I am not finding in the drop-down list of choices given by Intellisense any context-related properties for a thread. 我在Intellisense给出的选项的下拉列表中找不到线程的任何与上下文相关的属性。

I know that current synchronization context can be got through " = SynchronizationContext.Current; " . 我知道当前的同步上下文可以通过“ = SynchronizationContext.Current; ”获得。 But this is not quite fortunate with simultaneously executed in parallel threads, tasks, etc. 但是,并行线程,任务等同时执行并不是很幸运。

Suppose from a console or WPF (*) app I create and launch a few Windows forms in its own main UI threads as well as TPL tasks. 假设从一个控制台或WPF (*)应用程序,我在自己的主UI线程和TPL任务中创建并启动一些Windows窗体

I undermisabovestand that each winform should have its own WindowsFormaSynchronizationContext , WPF should have its own DispatcherSynchronizationContext (subclasses of SynchronizationContext class ) instances, the tasks are executed in a ThreadPool with its own synchronization context, LongRunning task orobably will be executed out of thread pool in its own synchronization context... 我认为每个winform都应该有自己的WindowsFormaSynchronizationContext ,WPF应该有自己的DispatcherSynchronizationContextSynchronizationContext类的 )实例,任务在ThreadPool中执行,并且有自己的同步上下文, LongRunning任务也可以在其中的线程池中执行拥有同步上下文......

So, why cannot the SynchronizationContext be defined from thread(s)? 那么,为什么不能从线程定义SynchronizationContext All answers to "Get SynchronizationContext from a given Thread" question seem to unanmous in negating tis possibility... “从给定线程中获取同步语言”问题的所有答案似乎都不一致否定了这种可能性......

And the last, but not least: 最后但并非最不重要的是:
Is the phrase "The current SynchronizationContext is a property of the current thread" correct" correct? 短语“当前SynchronizationContext是当前线程的属性”正确吗?
Then, how can I get the value of this property for different particular thread instances? 然后,如何为不同的特定线程实例获取此属性的值?

(*) (*)
Recently, I was given C# WPF app code essentially using winforms. 最近,我基本上使用winforms得到了C#WPF应用程序代码。

This is accurate. 这是准确的。 The SynchronizationContext.Current property uses the current thread's m_ExecutionContext field. SynchronizationContext.Current属性使用当前线程的m_ExecutionContext字段。 Which is a private field of the Thread class, that's why you don't see it in the IntelliSense dropdown. 这是Thread类的私有字段,这就是为什么你没有在IntelliSense下拉列表中看到它。

It is important it works that way, the default SynchronizationContext doesn't synchronize anything. 重要的是它以这种方式工作,默认的SynchronizationContext不会同步任何东西。 Its Post() method target runs on a threadpool thread. 它的Post()方法目标在线程池线程上运行。 Marshaling the target call to a specific thread is a very nontrivial thing to do. 将目标调用封送到特定线程是一项非常重要的事情。 That requires help from the target thread, it needs to provide a solution to the producer-consumer problem . 这需要目标线程的帮助,它需要为生产者 - 消费者问题提供解决方案。 The generic solution is a loop that retrieves messages from a thread-safe queue. 通用解决方案是一个从线程安全队列中检索消息的循环。 Exactly the way that the UI thread of a Winforms or a WPF app works, they "pump the message loop". 正是Winforms或WPF应用程序的UI线程的工作方式,它们“抽取消息循环”。 Application.Run() starts that loop. Application.Run()启动该循环。

So only the UI thread of such an app can support a synchronization provider that doesn't use threadpool threads to run the Post() delegate target. 因此,只有这样的应用程序的UI线程可以支持不使用线程池线程来运行Post()委托目标的同步提供程序。 Accordingly, Winforms and WPF install their own synchronization provider as soon as you create a Form or Window. 因此,Winforms和WPF会在您创建表单或窗口时立即安装自己的同步提供程序。 And only code that runs on the UI thread will see that non-default provider from the SynchronizationContext.Current property. 并且只有在UI线程上运行的代码才能从SynchronizationContext.Current属性中看到非默认提供程序。

The consequence is that you must initialize code that need to marshal calls back to the UI thread on the UI thread. 结果是您必须初始化需要将调用封送回UI线程上的UI线程的代码。 So for example creating a BackgroundWorker must be done on the UI thread. 因此,例如,必须在UI线程上创建BackgroundWorker。 Or a task created with TaskScheduler.FromCurrentSynchronizationContext. 或者使用TaskScheduler.FromCurrentSynchronizationContext创建的任务。 There can technically be more than one thread that displays UI, whatever thread the init code runs on determines where the Post() delegate target will run. 从技术上讲,可以有多个显示UI的线程,init代码运行的任何线程都决定了Post()委托目标的运行位置。 Which probably explains your problem, if your init code runs on a worker thread then the Post() target runs on a threadpool thread. 这可能解释了您的问题,如果您的init代码在工作线程上运行,那么Post()目标在线程池线程上运行。 You can pass a reference to the Synchronization.Current object to a worker thread, as long as you obtained that reference on the UI thread. 当你获得的UI线程上的参考,你可以传递给Synchronization.Current对象的引用到一个工作线程,只要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 主线程上的SynchronizationContext.Current为null - SynchronizationContext.Current is null on Main thread 线程中的C#线程:如何获取SynchronizationContext.Current? - C# Thread in Thread: how to get SynchronizationContext.Current? 等待不使用当前的SynchronizationContext - await not using current SynchronizationContext 当前的SynchronizationContext可以为null吗? - Can the current SynchronizationContext be null? 当线程返回到线程池时,SynchronizationContext.Current是否重置 - Is SynchronizationContext.Current reset when a thread goes back to threadpool SynchronizationContext.Current 在主 UI 线程上的 Continuation 中为 null - SynchronizationContext.Current is null in Continuation on the main UI thread 当前的SynchronizationContext可能不会用作TaskScheduler - The current SynchronizationContext may not be used as a TaskScheduler 当前的SynchronizationContext可能不用作TaskScheduler。 来自CEFsharp EvaluateScriptAsync - The current SynchronizationContext may not be used as a TaskScheduler. from CEFsharp EvaluateScriptAsync 为什么是 SynchronizationContext.Current null? - Why is SynchronizationContext.Current null? 主线程的SynchronizationContext.Current如何在Windows窗体应用程序中变为空? - How can SynchronizationContext.Current of the main thread become null in a Windows Forms application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM