简体   繁体   English

TaskScheduler.FromCurrentSynchronizationContext() 在嵌套任务中指的是什么?

[英]What does the TaskScheduler.FromCurrentSynchronizationContext() refer to in a nested Task?

If I create and start a task and call如果我创建并启动任务并调用

TaskScheduler.FromCurrentSynchronizationContext()

I know that, in WPF, it refers to the GUI thread.我知道,在 WPF 中,它指的是 GUI 线程。 If I create a nested Task within that task and call this method, will it still refer to the same GUI thread?如果我在该任务中创建一个嵌套任务并调用此方法,它是否仍会引用同一个 GUI 线程? I think this method calls the TaskScheduler and retrieves a static variable that refers to the GUI.我认为此方法调用 TaskScheduler 并检索引用 GUI 的静态变量。 Is this correct?这样对吗?

It refers to whatever SynchronizationContext is installed on the thread you call this method on.它指的是在调用此方法的线程上安装的任何SynchronizationContext When you call it on the thread pool this usually will be null .当您在线程池上调用它时,这通常为null If you call it on the UI thread it's the UI thread context.如果您在 UI 线程上调用它,则它是 UI 线程上下文。

So it depends on what scheduler your parent task runs on.因此,这取决于您的父任务在哪个调度程序上运行。

Also, your code could have modified SynchronizationContext.Current but that is rare.此外,您的代码可能已修改SynchronizationContext.Current但这种情况很少见。

All of this has nothing to do with tasks.所有这些都与任务无关。 It's about how the current thread is configured.它是关于如何配置当前线程的。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM