Task.Factory.StartNew vs new Task

C#C# 4.0Asynchronous

C# Problem Overview


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.StartNewreturns a task that is already started. Is this correct?

I know that Task.Factory.StartNewand Task.Run have different default options and Task.Run is the preferred option for .Net 4.5.

C# Solutions


Solution 1 - C#

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.

Solution 2 - C#

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNeddySpaghettiView Question on Stackoverflow
Solution 1 - C#NeddySpaghettiView Answer on Stackoverflow
Solution 2 - C#o_cView Answer on Stackoverflow