none
Task.Run vs Task.WhenAll vs Parallel.Invoke vs others: Run tasks in parallel and Get result in C# RRS feed

  • Question

  • Is the code in Option 1 below the best way to achieve in running tasks in parallel?

    **Option 1**

        var w = Task.Run(async () => await Work1());
        var w2 = Task.Run(async () => await Work2());
        Console.WriteLine("end: " + DateTime.Now.ToString("hh:mm:ss.fff"));
        await w;
        await w2;
        Console.WriteLine("last: " + DateTime.Now.ToString("hh:mm:ss.fff") + " " + w.Result + " " + w2.Result);
        Console.WriteLine("end");

     
    **Result:**

        end: 06:07:13.054
        Work2 s 06:07:13.057
        Work1 s 06:07:13.057
        Work1 e 06:07:16.072
        Work2 e 06:07:17.066
        last: 06:07:17.066 1 2

    **option 2:**
    It swithes to other work upon await, ie. not really in parallel.   
               
                    var tasks = new List<Task<int>>();
                    tasks.Add(Work1());
                    tasks.Add(Work2());
                    Console.WriteLine("end: " + DateTime.Now.ToString("hh:mm:ss.fff"));
                    await Task.WhenAll(tasks);
                    Console.WriteLine("end2: " + DateTime.Now.ToString("hh:mm:ss.fff"));
                    Console.WriteLine("last: " + DateTime.Now.ToString("hh:mm:ss.fff") + " " + tasks[0].Result + " " + tasks[1].Result);

    **Result:**

        Work1 s 06:11:42.565
        Work2 s 06:11:44.571
        Work1 e 06:11:45.579
        end: 06:11:47.572
        Work2 e 06:11:48.574
        end2: 06:11:48.575
        last: 06:11:48.579 1 2


    Tasks

          private static async Task<int> Work1()
                {
                    Console.WriteLine("Work1 s " + DateTime.Now.ToString("hh:mm:ss.fff"));
                    Thread.Sleep(2000);
                    await Task.Delay(1000);
                    Console.WriteLine("Work1 e " + DateTime.Now.ToString("hh:mm:ss.fff"));
                    return 1;
                }
        
                private static async Task<int> Work2()
                {
                    Console.WriteLine("Work2 s " + DateTime.Now.ToString("hh:mm:ss.fff"));
                    Thread.Sleep(3000);
                    await Task.Delay(1000);
                    Console.WriteLine("Work2 e " + DateTime.Now.ToString("hh:mm:ss.fff"));
                    return 2;
                }

    Parallel.Invoke() doesn't return result.
    • Edited by Pingpong689 Thursday, August 8, 2019 5:28 PM
    Thursday, August 8, 2019 5:24 PM

All replies

  • Hi Pingpong689,

    Thank you for posting here.

    Based on your description, you want to find the best way to achieve in running tasks in parallel.

    I rewrite a code example to make it, you could have a look.

      static void Main(string[] args)
            {
                Go();
            }
            public static void Go()
            {
                GoAsync();
                Console.ReadLine();
            }
            public static async void GoAsync()
            {
    
                Console.WriteLine("Starting");
                Console.WriteLine(DateTime.Now.ToString("hh:mm:ss.fff"));
                var task1 = Sleep(5000);
                var task2 = Sleep(3000);
    
                int[] result = await Task.WhenAll(task1, task2);
    
                Console.WriteLine("Slept for a total of " + result.Sum() + " ms");
                Console.WriteLine(DateTime.Now.ToString("hh:mm:ss.fff"));
    
    
            }
    
            private async static Task<int> Sleep(int ms)
            {
                Console.WriteLine("Sleeping for {0} at {1}", ms, Environment.TickCount);
                await Task.Delay(ms);
                Console.WriteLine("Sleeping for {0} finished at {1}", ms, Environment.TickCount);
                return ms;
            }
    

    Result:

    Best Regards,

    Jack


    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.

    Friday, August 9, 2019 1:50 AM
    Moderator