Synchronization context for Task.Delay

2019-07-28 20:55发布

问题:

I could find out that, Task.Run executes always on threads from .NET Framework threads pool (TaskScheduler.Default). I suppose, that it is the same with Task.Delay, but I'm not sure.

MSDN says for Task.Delay only:

Creates a task that will complete after a time delay

Therefore the question: Where (in which synchronization context) runs Task.Delay?

回答1:

Task.Delay doesn't run anywhere. It just creates a task that completes after the specified time. Unlike Task.Run it's not accepting a delegate of yours to run somewhere. Most tasks won't represent the execution of some method on another thread. Task.Run is one of few methods that do that.