task sleep c# code example

Example 1: c# Sleep

using System.Threading;

static void Main()
{
  //do stuff
  Thread.Sleep(5000) //will sleep for 5 sec
}

Example 2: c# async sleep

// Async
await Task.Delay(1000); //when you want a logical delay without blocking the current thread
// Not Async
Thread.Sleep(1000) //when you want to block the current thread.

Example 3: c# thread sleep

Thread.Sleep(2000); //in ms

Example 4: how to delay execution in c#

int sleepTime = 1000; // in mills
Task.Delay(sleepTime).Wait();
// or
Thread.Sleep(sleepTime);