Await Inside Loop - CSharp Thread Asynchronous

CSharp examples for Thread Asynchronous:Async

Description

Await Inside Loop

Demo Code

using System;//from   w  w w.ja  v  a 2s . c  o m
using System.ComponentModel;
using System.Threading.Tasks;
class AwaitInsideLoop
{
   static async Task AwaitInLoop(TimeSpan delay)
   {
      Console.WriteLine("Before loop");
      for (int i = 0; i < 3; i++)
      {
         Console.WriteLine("Before await in loop");
         await Task.Delay(delay);
         Console.WriteLine("After await in loop");
      }
      Console.WriteLine("After loop delay");
   }
   static void Main()
   {
      AwaitInLoop(TimeSpan.FromSeconds(1)).Wait();
   }
}

Result


Related Tutorials