简体   繁体   中英

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. If I create a nested Task within that task and call this method, will it still refer to the same GUI thread? I think this method calls the TaskScheduler and retrieves a static variable that refers to the GUI. Is this correct?

It refers to whatever SynchronizationContext is installed on the thread you call this method on. When you call it on the thread pool this usually will be null . If you call it on the UI thread it's the UI thread context.

So it depends on what scheduler your parent task runs on.

Also, your code could have modified SynchronizationContext.Current but that is rare.

All of this has nothing to do with tasks. It's about how the current thread is configured.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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