Task.Factory.StartNew vs new Task

后端 未结 2 1255
囚心锁ツ
囚心锁ツ 2020-12-04 23:15

Does anyone know if there is any difference between doing Task.Factory.StartNew vs new Task followed by calling Start on the task. Loo

相关标签:
2条回答
  • 2020-12-05 00:01

    Actually in the article by Stephen Toub he specifies that Task.Run() is exactly equivalent to using Task.Factory.StartNew() with the default parameters:

    Task.Factory.StartNew(someAction, 
    CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default);
    
    0 讨论(0)
  • 2020-12-05 00:06

    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.

    His advice is to prefer using Task.Factory.StartNew for .net 4.0. For .net 4.5 Task.Run is the better option.

    0 讨论(0)
提交回复
热议问题