What SynchronizationContext does Task.ContinueWith

2020-04-10 03:30发布

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

2条回答
We Are One
2楼-- · 2020-04-10 03:53

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).

查看更多
姐就是有狂的资本
3楼-- · 2020-04-10 03:55

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().

查看更多
登录 后发表回答