Coder Perfect

What is the difference between Task.Run() and Task.Factory.StartNew in Task.Run() and Task.Factory.StartNew in Task.Run() and Task.Fa ()


I’m using the following strategy:

private static void Method()
    Console.WriteLine("Method() started");

    for (var i = 0; i < 20; i++)
        Console.WriteLine("Method() Counter = " + i);

    Console.WriteLine("Method() finished");

And I’d like to begin using this way in a new Task. I can begin a new task such as this.

var task = Task.Factory.StartNew(new Action(Method));

or this

var task = Task.Run(new Action(Method));

Is there, however, a distinction between Task and Task? Run() and Task are two methods for executing code. Factory. StartNew(). Both of them use ThreadPool and call Method() as soon as the Task instance is created. When should we utilize the first variant and when should we use the second?

Asked by Sergiy Lichenko

Solution #1

The second method, Task.Run, was added to the.NET framework in a later version (in .NET 4.5).

Task.Factory, on the other hand, is the first way. StartNew allows you to specify a number of relevant parameters for the thread you want to create, but Task.Run does not.

Let’s imagine you want to make a task thread that runs for a long time. This could be deemed an abuse of the thread pool if a thread from the thread pool is used for this task.

You could avoid this by performing the task on a different thread. A newly established thread that will be dedicated to this work and will be deleted once it has been done. You won’t be able to do it using the Task. While using the Task.Factory, you can run. Begin anew, as seen below:

Task.Factory.StartNew(..., TaskCreationOptions.LongRunning);

As stated in this article:

    CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default);

Answered by Christos

Solution #2

That has already been said.


Is equivalent to

Task.Factory.StartNew(A, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default);

No one, however, brought it up.


Is equivalent to:

Task.Factory.StartNew(A, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Current);

As you can see, Task.Run and Task.Factory.StartNew have two different parameters:

Even if we set TaskCreationOptions, childTask will not be awaited when we call parentTask.Wait(). Because TaskCreationOptions, it’s attached to the parent. DenyChildAttach prevents kids from attaching to it. If you use Task.Factory instead of Task.Factory, you’ll get the same result. Instead of Task, use StartNew. Because Task.Factory is true, parentTask.Wait() will wait for childTask. TaskCreationOptions is used by StartNew. None

So, if you’re not using nested children tasks and always want your tasks to run on Thread Pool, Task is the way to go. Unless you have some more complicated situations in mind, run.

Answered by Mykhailo Seniutovych

Solution #3

See this blog post for a detailed explanation of the differences. Basically, I’m:


Is the same thing as:

Task.Factory.StartNew(A, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default);   

Answered by Scott Chamberlain

Solution #4

Task.Run is a newer.NET foundation feature that comes highly recommended.

Task.Factory is a task factory. Task.Run is a shortcut for StartNew, which includes extra options:

And when I say shorthand, I’m referring to a technical shortcut:

public static Task Run(Action action)
    return Task.InternalStartNew(null, action, null, default(CancellationToken), TaskScheduler.Default,
        TaskCreationOptions.DenyChildAttach, InternalTaskOptions.None, ref stackMark);

Answered by Zein Makki

Solution #5

Task.Factory.StartNew() is harmful, according to this post by Stephen Cleary:

The true causes are as follows:


private static void A() { }

As a result, if an operation completes and it marshals back to the UI thread owing to a continuation, as Stephen Cleary discusses in his post, it might potentially run on the UI thread.

In my scenario, I was trying to load a datagrid for a view while also displaying a busy animation in the background. When using Task.Factory, the busy animation did not appear. When I moved from StartNew() to Task, though, the animation worked fine. Run().

Please check for more information.

Answered by user8128167

Post is based on