-1

i have a great confusion while working with the async and await.

My logic is simple, i have a method which will cause a delay of 15 seconds, like this

public static Task<int> delay(int num)
{
    Task.Delay(15000);
    return Task.FromResult(num);
}

now i am calling this method like this

 public static async void delayAsync(int num)
{
    Console.WriteLine(await delay(num)+"time :"+DateTime.Now);

}
static void Main(string[] args)
{
    var details = new details();
    Console.WriteLine("## start ##");
    for (var i = 0; i < 5; i++)
    {
        Console.WriteLine("counter: "+i);
        delayAsync(i);
    }
    Console.WriteLine("## finished ##");
    Console.Read();
}

my desired output is to get the number one by one after the delay of 15 seconds, but i am getting the results all at once without any pause. am i missing something.

I have gone through this blog and couldnt understand a thing

Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
Lijin Durairaj
  • 3,341
  • 11
  • 31
  • 50
  • First of all don't return void from `delayAsync`. return a Task instead. Second you need to await `Task.Delay` – Alex Sep 14 '17 at 06:52

3 Answers3

4

Here are the steps that you need to achieve this:

  1. Await Task.Delay and return num instead of Task.

    public static async Task<int> delay(int num)
    {
        await Task.Delay(15000);
        return num;
    }
    
  2. Make your delayAsync method return Task.

    public static async Task delayAsync(int num)
    {
        Console.WriteLine(await delay(num) + "time :" + DateTime.Now);
    }
    
  3. Wait for the result of the delayAsync in you Main method.

    public static void Main(string[] args)
    {
        Console.WriteLine("## start ##");
        for (var i = 0; i < 5; i++)
        {
            Console.WriteLine("counter: " + i);
            delayAsync(i).Wait();
        }
        Console.WriteLine("## finished ##");
        Console.Read();
    }
    
Samvel Petrosov
  • 7,580
  • 2
  • 22
  • 46
2

You are not awaiting the Task.Delay

public static async Task<int> delay(int num){
   await Task.Delay(15000);
   return num;
}

Note that the signature of the method needs to include async. Additionally since you are now using await inside the method, you don't need to explicitly create a Task for the return statement.

Additionally you have to await the delayAsync call in your Main method (note that you cannot make Main async so you have to use a workaround like this: https://stackoverflow.com/a/9212343/868361

Sylence
  • 2,975
  • 2
  • 24
  • 31
1

my desired output is to get the number one by one after the delay of 15 seconds

When you call an async method that you don't await then the compiler will start it and continue with the rest of the procedures. In your case with the rest of the loop. So the simple solution to get your desired output would be here:

Don't use async methods! Simply make it a serial processing.

To achieve that what you want using async you would have to wait in the loop for the delayAsync method. But this would make the asynchronous processing synchronous and you would end up again in the normal 1 thread serial processing, only having invested a lot of effort to end up from where you started.

Mong Zhu
  • 23,309
  • 10
  • 44
  • 76