-1

I am thinking a way to handle and expensive process which would require me to call and get multiple data from multiple Industrial field. So for each career I will make them into one individual task inside the Generate Report Class. I would like to ask that when using Task.WhenAll is it that task 1,task 2,task 3 will run together without waiting task1 to be completed. But if using Task.WaitAll it will wait for MiningSector to Complete in order to run ChemicalSector. Am I correct since this is what I wanted to achieve.

public static async Task<bool> GenerateReport()
       {
           static async Task<string> WriteText(string name)
           {
               var start = DateTime.Now;
               Console.WriteLine("Enter {0}, {1}", name, start);
               return name;
           }
           static async Task MiningSector()
           {
               var task1 = WriteText("task1");
               var task2 = WriteText("task2");
               var task3 = WriteText("task3");
 
               await Task.WhenAll(task1, task2, task3); //Run All 3 task together
 
               Console.WriteLine("MiningSectorresults: {0}", String.Join(", ", t1.Result, t2.Result, t3.Result));
           }
           static async Task ChemicalsSector()
           {
               var task4 = WriteText("task4");
               var task5 = WriteText("task5");
               var task6 = WriteText("task6");
 
               await Task.WhenAll(task4 , task5 , task6 ); //Run All 3 task together
 
               Console.WriteLine("ChemicalsSectorresults: {0}", String.Join(", ", t1.Result, t2.Result, t3.Result));
           }
           static void Main(string[] args)
           {
               Task.WaitAll(MiningSector(), ChemicalsSector()); //Wait when MiningSectoris complete then start ChemicalsSector.
           }
           return true;
       }

The thing I wanted to achieve is, inside MiningSector have 10 or more functions need to be run and ChemicalSector is same as well. I wanted to run all the functions inside the MiningSector at parallel once it's done then the ChemicalSector will start running the function.

  • Does this answer your question? [WaitAll vs WhenAll](https://stackoverflow.com/questions/6123406/waitall-vs-whenall) – Johnathan Barclay Apr 15 '21 at 14:11
  • @JohnathanBarclay I asked is my thinking path is correct or not I know waitAll and whenAll. I just not sure whether It will achieve the result I want does this make it clear? – SdoAna GnGmnk Apr 15 '21 at 14:21
  • Updated the question to you read again – SdoAna GnGmnk Apr 15 '21 at 14:30
  • 1
    No, ``WaitAll`` does not mean that it care about order of execution of tasks. Both of them do same thing but the main difference is ``WaitAll`` block the main thread unit all tasks done but ``WhenAll`` not do and main thread can process other tasks. – sa-es-ir Apr 15 '21 at 14:38
  • 2
    The code you have posted will run synchronously, because nothing asynchronous is happening. If `WriteText` were a truly asynchronous method i.e. involved I/O, then `task2` will commence at the point that the thread is released, and will not wait for `task1` to complete. – Johnathan Barclay Apr 15 '21 at 14:43

1 Answers1

1

But if using Task.WaitAll it will wait for MiningSector to Complete in order to run ChemicalSector. Am I correct since this is what I wanted to achieve.

No. The key thing to understand here is that arguments are evaluated first, and then the method call. This is the same way all arguments and method calls work across the entire C# language.

So this code:

Task.WaitAll(MiningSector(), ChemicalsSector());

has the same semantics as this code:

var miningTask = MiningSector();
var sectorTask = ChemicalsSector();
Task.WaitAll(miningTask, sectorTask);

In other words, both methods are called (and both tasks started) before Task.WaitAll is called.

If you want to do one then the other, then don't call the second method until the first task has completed. This is most easily done using async Main, e.g.:

static async Task Main(string[] args)
{
  await MiningSector();
  await ChemicalsSector();
}
Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810