What SynchronizationContext does Task.ContinueWith

2020-04-10 03:13发布

问题:

Some overloads of Task.ContinueWith do not take a SynchronizationContext. What SynchronizationContext do they use to schedule the new task?

回答1:

Some overloads of Task.ContinueWith do not take a SynchronizationContext.

Actually, none of them take a SynchronizationContext, but some take a TaskScheduler.

What SynchronizationContext do they use to schedule the new task?

None! By default, the continuation is scheduled by the current scheduler (TaskScheduler.Current), which, when not called from a Task, is TaskScheduler.Default. So the continuation is run on a thread from the thread pool. ThreadPool threads don't have an associated synchronization context (unless you explicitly set one).



回答2:

By default, ContinueWith uses TaskScheduler.Current for the continuation task, as can be seen in the Reference Source.

This may be a source of confusion, because the current (ambient) task scheduler can be different from TaskScheduler.Default, and it can also be different from the scheduler of the task the continuation is attached to. That's why options like TaskCreationOptions.HideScheduler and TaskContinuationOptions.HideScheduler were intriduced in .NET 4.5.

It is recommended to always specify the task scheduler explicitly when callingTask.Factory.StartNew and Task.ContinueWith. Most commonly, you'd specify TaskScheduler.Default (for the thread pool task scheduler) or TaskScheduler.FromCurrentSynchronizationContext().