Difference between Task.Factory.StartNew and Task.Run in .NET 4.5

Posted: (EET/GMT+2)

 

In .NET 4.5, both "Task.Run" and "Task.Factory.StartNew" can start a background operation, but they behave a bit differently.

Firstly, Task.Run is the simpler and safer choice for running CPU-bound work on the thread pool. Here's a simple example:

Task.Run(() => DoWork());

Task.Run uses the default scheduler ("TaskScheduler.Default"), handles exceptions correctly, and unwraps nested tasks automatically.

On the other hand, Task.Factory.StartNew is older and more flexible. It lets you pick the scheduler, choose cancellation options, set creation options, etc. Be careful though, as you need to be "on top of" the available settings.

Task.Factory.StartNew(() => DoWork(),
    CancellationToken.None,
    TaskCreationOptions.DenyChildAttach,
    TaskScheduler.Default);

Starting with .NET 4.5, Task.Run internally just calls Task.Factory.StartNew with safe defaults. So for most code, prefer Task.Run. Use StartNew() only if you need custom scheduling or options.