Need advice about async

Dondon510 261 Reputation points
2022-08-15T07:08:09.003+00:00

Which one is good for performance and manageable? and do we need to always Task.WaitAll()?

internal static async Task<JObject> Learning()  
{  
    var dr = await conn.ExecuteReaderAsync(sql, new  
    {  
         Organizations_Pid = organizationsPid,  
         Period = dataUsagePeriod + "%"  
    }) ;  
  
    while (dr.Read())  
    {  
  
     }         
}  
  
or  
  
internal static async Task<JObject> Learning()  
{  
   var dr = await conn.ExecuteReaderAsync(sql, new  
   {  
       Organizations_Pid = organizationsPid,  
       Period = dataUsagePeriod + "%"  
   }) ;  
  
   while (await dr.ReadAsync())  
   {  
  
   }          
  
     Task.WaitAll()  
  }  
Developer technologies ASP.NET ASP.NET Core
{count} votes

Accepted answer
  1. AgaveJoe 30,126 Reputation points
    2022-08-16T11:20:55.597+00:00

    What if I don't use the list? like this:

    Task.WhenAll() is used to await a list of tasks. If you do not have a list of tasks then you don't need await Task.WhenAll(). I can't read your mind. I have no idea what you're trying to do. One of your posts has a ContinueWith which is also implemented incorrectly. Are you trying to implement ContinueWith and need an example?

    Console.WriteLine("Async Example");  
    Console.WriteLine();  
      
      
    Task continuationTask =  WidgetSizeReturnColorAsync(10).ContinueWith(c => Console.WriteLine($"The widget color is set to {c.Result}"));  
      
    Console.WriteLine("Doing some work while Widgets tasks are executing");  
    Console.WriteLine(DateTime.Now.ToString("hh:mm:ss"));  
    await continuationTask;  
    Console.WriteLine(DateTime.Now.ToString("hh:mm:ss"));  
      
    Console.WriteLine("The Widget tasks are complete.");  
      
      
    static async Task<string> WidgetSizeReturnColorAsync(int size)  
    {  
        await Task.Delay(5000);  
        Console.WriteLine($"The widget size is set to {size}");  
        return "Red";  
    }  
      
    

    It is much easier to help of you tell us what you are trying to do rather than telling us what you are NOT trying to do.

    will it do the same?

    No. If you read the openly published Task.WhenAll() reference documentation you would find the method requires at least a collection of tasks to await. If you don't pass the task collection then how does the method know what tasks to await?

    Secondly, I provided sample code where you can test what happens if no parameters are passed to Task.WhenAll().

    I'm not sure how to help you. It seems like you are not making an effort to learn the asycn/await pattern and you are ignoring my advice.


1 additional answer

Sort by: Most helpful
  1. AgaveJoe 30,126 Reputation points
    2022-08-15T18:10:34.9+00:00

    How if I do the following use case: (learning process)

    Clearly, you've made no effort to learn the async/await pattern. Your code is logically incorrect and does not follow any standard pattern found in the reference documentation.

    Console.WriteLine("Async Example");  
    Console.WriteLine();  
      
    List<Task> tasks = new List<Task>();  
      
    Task color = WidgetColorAsync("green");  
    Task size = WidgetSizeAsync(10);  
      
    tasks.Add(color);  
    tasks.Add(size);  
      
    Console.WriteLine("Doing some work while Widgets tasks are executing");  
      
    await Task.WhenAll(tasks);  
      
    Console.WriteLine("The Widget tasks are complete.");  
      
      
    static async Task WidgetColorAsync(string color)  
    {  
        await Task.Delay(3000);  
        Console.WriteLine($"The widget color is set to {color}");  
    }  
      
    static async Task WidgetSizeAsync(int size)  
    {  
        await Task.Delay(2000);  
        Console.WriteLine($"The widget size is set to {size}");  
    }  
    

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.