C# regarding running multiple task

T.Zacks 3,996 Reputation points
2021-11-01T17:56:42.48+00:00

State1 & state2 will run parallel or State1 will run first and later State2 will run ?

please guide me. thanks

    var stage1 = Task.Run(() =>
    {

    });

    var stage2 = Task.Run(() =>
    {

    });

    // Block until both tasks have completed.
    // This makes this method prone to deadlocking.
    // Consider using 'await Task.WhenAll' instead.
    Task.WaitAll(stage1, stage2);
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,029 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jack J Jun 24,516 Reputation points Microsoft Vendor
    2021-11-03T06:59:05.623+00:00

    @T.Zacks , based on my test, the code you provided will run parallel.

    I make a code example and you could have a look.

    Code:

    146101-image.png

        var timer = new Stopwatch();  
        timer.Start();  
        Task.WaitAll(stage1, stage2);  
        timer.Stop();  
        TimeSpan timeTaken = timer.Elapsed;  
        string foo = "Time taken: " + timeTaken.TotalMilliseconds;  
        Console.WriteLine(foo);  
    

    Result:

    146082-image.png

    As the above result showed, we spend about 5000 Milliseconds to complete the two tasks. The stage2 will be completed first, because it only needs 1000 Milliseconds.

    Note: I can not code the first two line's code, so I make a picture about the code.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Michael Taylor 54,901 Reputation points
    2021-11-01T19:31:40.497+00:00

    Each time you "run" a task it begins running at some point in the future. When that is depends on different factors but as far as your code is concerned you can assume it starts running immediately while your main thread continues running. Therefore stage1 will start running then stage2 will start running and then your main thread will block waiting for both to complete. Which one runs first and which one completes first is non-deterministic as it depends on factors related to what is running and what is going on in the system right now.

    If you want to make these tasks run in sequential order then start one and then wait for it to finish before starting the second one. Of course the downside is that the total execution times is now : S1 + S2 instead of MAX(S1, S2) of running them in parallel.

    var stage1 = await Task.Run(...);   //Run task 1 and wait for it to complete
    var stage2 = await Task.Run(...);   //Run task 2 and wait for it to complete
    
    //Both tasks are done so don't need to wait anymore.
    

    Also note that, depending on what they are doing, you tasks may not even run async at all. Again, this is dependent upon multiple factors but for purposes of your code assume they run async as you coded for.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.