Pergunta

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

Foi útil?

Solução 2

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

Outras dicas

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

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top