Maderas
Maderas

Reputation: 241

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?

Upvotes: 2

Views: 836

Answers (1)

usr
usr

Reputation: 171178

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.

Upvotes: 2

Related Questions