How to create delay in C#

2 Answers

0 votes
using System;
using System.Threading;
    
class Program
{
    static void Main() {
        try {
            Console.WriteLine("Counting...");
 
            int milliseconds = 3000;
            Thread.Sleep(milliseconds); // will lock the thread
            
            Console.WriteLine("Lift Off");
        }
        catch (Exception ex) {
            Console.WriteLine(ex.Message);
        }
    }
}


 
/*
run:
 
Counting...
Lift Off
 
*/
    

 



answered Jul 11, 2015 by avibootz
edited Apr 12, 2024 by avibootz
0 votes
using System;
using System.Threading;
    
class Program
{
    static void Main() {
        try {
            Console.WriteLine("Counting...");
 
            Thread.Sleep((int)TimeSpan.FromSeconds(3).TotalMilliseconds);
 
            Console.WriteLine("Lift Off");
        }
        catch (Exception ex) {
            Console.WriteLine(ex.Message);
        }
    }
}


 
/*
run:
 
Counting...
Lift Off
 
*/

 



answered Jul 11, 2015 by avibootz
edited Apr 12, 2024 by avibootz
...