c# delay 1 second code example

Example 1: c# wait for seconds

using System;
using System.Threading;

class Example
{
    static void Main()
    {
        for (int i = 0; i < 5; i++)
        {
            Console.WriteLine("Sleep for 2 seconds.");
            Thread.Sleep(2000);
        }

        Console.WriteLine("Main thread exits.");
    }
}

/* This example produces the following output:

Sleep for 2 seconds.
Sleep for 2 seconds.
Sleep for 2 seconds.
Sleep for 2 seconds.
Sleep for 2 seconds.
Main thread exits.
 */

Example 2: how to delay execution in c#

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

Example 3: c# wait seconds

//wait 2 seconds
Thread.Sleep(2000);
Task.Delay(2000);

//Both are valid options but I would recommend Task.Delay() as you can still use your UI while waiting

Example 4: C# 1 minute delay

DateTime now = DateTime.Now;
while (DateTime.Now.Subtract(now).Seconds < 60)
{
        // wait for 60 seconds
}
// 60 seconds passed, continue