C# – sleep() method in Thread

sleep():

  • It is a static method define in Thread class.
  • It is used to stop the thread execution for specified number of milliseconds.
using System;
using System.Threading;
public class Program
{
            static void Main(string[] args)
            {
                        First.print1to10();
                        Second.print10to1();
            }
}
public class First
{
            public static void print1to10()
            {
                        for (int i=1; i<=10; i++)
                        {
                                    Console.WriteLine(“i value : ” + i);
                                    Thread.Sleep(1000);
                        }
            }
}
class Second
{
            public static void print10to1()
            {
                        for(int j=10; j>=1; j–)
                        {
                                    Console.WriteLine(“j value : ” + j);
                                    Thread.Sleep(1000);
                        }
            }
}
Scroll to Top