none
Difference between the calls Task.Run and Task.Factory.StartNew RRS feed

  • Question

  • Hello!
    Can someone please explain to me visually (in graphic) what the differences are.
    When should I use the call?
       Task.Run(()=>
    When should I use the other call?
       Task.Factory.StartNew(async ()

    Does this have to do with the Factory Pattern?

    If so, does anyone know a good understandable example?

    Thank you very much for the explanation in advance.

    Greetings Markus

    Task<Person> taskOne = Task.Factory.StartNew(async () =>
                {
    			
    			
    			
    public Task<int> SlowWork()
    {
       return Task.Run(()=>{……});
    }
    
    public async Task CallSlowTask()
    {
       // 1. Call the async code, let it run for a while in another thread 
       //    (Task) in the async mode. The work is usually very slow
      var runningTask = SlowWork();
    
       // 2. Here we can run the sync code, it will block the UI or main 
       //    thread, so please make them as simple and fast as possible.
      ……
    
       // 3. After this, when we wanna get the result from SlowWork(), 
       //    we can use await to force that we want to fetch the result. 
       //    Because "SlowWork()" has been running for a while, maybe the 
       //    result comes in. But if the result doesn't come in yet, it 
       //    will wait in the async mode (NOT blocking the current main thread)
      var result = await runningTask;
    }

    Tuesday, February 18, 2020 5:30 PM

Answers

  • This is not graphical but right from Microsoft.

    https://devblogs.microsoft.com/pfxteam/task-run-vs-task-factory-startnew/


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, February 18, 2020 5:38 PM
    Moderator
  • Hi Markus,

    Thank you for posting here.

    These two methods are very similar, but Task.Run is a newer technique.

    In contrast, Task.Factory.StartNew can pass more parameters, so it allows you to have more fine-grained control over the Task, but it has many disadvantages, and Task.Run is generally recommended.

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

    StartNew is Dangerous

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Ehsan Sajjad Wednesday, February 19, 2020 5:01 AM
    • Marked as answer by Markus Freitag Wednesday, February 19, 2020 6:07 PM
    Wednesday, February 19, 2020 3:26 AM

All replies