Task.Factory.StartNew 与新任务 [英] Task.Factory.StartNew vs new Task
问题描述
有谁知道在执行 Task.Factory.StartNew
和 new Task
之后在任务上调用 Start
之间是否有任何区别.看着反射器,似乎没有太大区别.所以也许唯一的区别是 Task.Factory.StartNew
返回一个已经启动的任务.这是正确的吗?
Does anyone know if there is any difference between doing Task.Factory.StartNew
vs new Task
followed by calling Start
on the task. Looking at reflector there doesn't seem to be much difference. So perhaps the only difference is that Task.Factory.StartNew
returns a task that is already started. Is this correct?
我知道 Task.Factory.StartNew
和 Task.Run
有不同的默认选项,Task.Run
是 .净 4.5.
I know that Task.Factory.StartNew
and Task.Run
have different default options and Task.Run
is the preferred option for .Net 4.5.
推荐答案
我发现 this Stephen Toub 的好文章,它解释了使用 new Task(...).Start()
时实际上会有性能损失,因为 start 方法需要使用同步确保任务只安排一次.
I found this great article by Stephen Toub, which explains that there is actually a performance penalty when using new Task(...).Start()
, as the start method needs to use synchronization to make sure the task is only scheduled once.
他的建议是在 .net 4.0 中更喜欢使用 Task.Factory.StartNew
.对于 .net 4.5 Task.Run
是 更好的选择.
His advice is to prefer using Task.Factory.StartNew
for .net 4.0. For .net 4.5 Task.Run
is the better option.
这篇关于Task.Factory.StartNew 与新任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!