问题描述
我发现,Task.Run
总是在来自 .NET Framework 线程池 (TaskScheduler.Default
) 的线程上执行.我想,它与 Task.Delay
相同,但我不确定.
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 表示 Task.Delay
仅:
MSDN says for Task.Delay
only:
创建一个将在一段时间延迟后完成的任务
因此问题:Task.Delay
在哪里(在哪个同步上下文中)运行?
Therefore the question: Where (in which synchronization context) runs Task.Delay
?
推荐答案
Task.Delay
不会在任何地方运行.它只是创建一个在指定时间后完成的任务.与 Task.Run
不同的是,它不接受您的委托在某处运行.大多数任务不会代表在另一个线程上执行某些方法.Task.Run
是执行此操作的少数方法之一.
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.
这篇关于Task.Delay 的同步上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!