-2

I have the following C# .NET 5 code. Basically I intend to call the three methods, namely Fun1, Fun2 & Fun3 asynchronously. The 3 methods are independent so are candidate to asynchronous execution. Do I really to use the key word 'await' here? Because if I use the await keyword while calling Task.Run method for all the 3 method calls , it will behave synchronously. So I am using Task.WaitAll instead to wait for all the 3 task runs to complete (block the main thread till all the 3 tasks finish asynchronously). Is there any problem with this approach? I am not sure what would be an alternative method to achieve this using 'await'. As of now the following code runs in roughly 5 seconds , which shows that it's running asynchronously.

using System;
using System.Threading;
using System.Threading.Tasks;
using static System.Console;

    namespace MyTask
    {
        class Program
        {
            static async Task Main(string[] args)
            {
                //Most articles use lamba expression but not sure why as I can directly pass function name to the Run method
                //var task1 = Task.Run(()=> { Fun1(); });
                //var task2 = Task.Run(()=> { Fun2(); });
                //var task3 = Task.Run(()=> { Fun3(); });
    
                var startTime = DateTime.Now;
                var task1 = Task.Run(Fun1);
                var task2 = Task.Run(Fun2);
                var task3 = Task.Run(Fun3);
                Task.WaitAll(task1, task2, task3);
                var duration = DateTime.Now.Subtract(startTime).TotalSeconds;
    
                WriteLine($"all done --total time taken is {duration}");
            }
            static int Fun1()
            {
                WriteLine("Within Fun1");
                Thread.Sleep(5000);
                return 1;
            }
            static int Fun2()
            {
                WriteLine("Within Fun2");
                Thread.Sleep(5000);
                return 2;
            }
            static int Fun3()
            {
                WriteLine("Within Fun3");
                Thread.Sleep(5000);
                return 3;
            }
        }
    }
Dhiraj
  • 3,396
  • 4
  • 41
  • 80
  • 1
    `await Task.WhenAll(...)` is usually better approach (though it should not matter in current case). As for "alternative" - `await task1; await task2; await task3;` also can be used. – Guru Stron Jul 06 '21 at 23:38

1 Answers1

1

Task.WaitAll() will block you main thread. You should use await Task.WhenAll()

bit
  • 4,407
  • 1
  • 28
  • 50
  • I understand that , I wanted main thread to wait for the result, that's fine lets say I use WhenAll , but my actual question is -- is it correct to say that we don't need await keyword in this scenario ? – Dhiraj Jul 06 '21 at 23:45
  • await Task.WhenAll(task1, task2, task3) is also blocking my main thread because the duration printing statement won't run before 5 seconds, which is fine , because I wanted to wait for the result. But then it is as good as using Task.WaitAll since that's also blocking main anyways. – Dhiraj Jul 06 '21 at 23:55
  • 2
    @Dhiraj this behaviour will vastly change if you try to use this code in some WPF app in some callback for example) – Guru Stron Jul 06 '21 at 23:58
  • 1
    When you say "I want to wait", you can either Task.WaitAll() which blocks the main thread untill all the tasks are complete, OR you can await Task.whenAll() which frees your main thread while waiting from tasks to complete and resumes back at the same line of code when all your tasks are complete. Thus the latter is a non-blocking wait. – bit Jul 07 '21 at 00:09