C# Sleep (Thread Sleep)

In c#, sleep method is useful to suspend or pause the current thread execution for the specified amount of time.

 

We can suspend the thread execution either by passing the time in milliseconds or with TimeSpan property like as shown below.

 

// Time in milliseconds

Thread.Sleep(1000);

Or

// Time in hours, minutes, seconds

TimeSpan ts = new TimeSpan(0,0,1);

Thread.Sleep(ts);

If you observe the above code snippet, we trying to suspend the thread for 1 second by using the Sleep method.

C# Sleep(milliseconds) Method Example

Following is the example of Sleep method in c# to pause the thread execution for the specified amount of time by passing the time in milliseconds.

 

using System;

using System.Threading;

 

namespace TutlaneExamples

{

    class Program

    {

        static void Main(string[] args)

        {

            for (int i = 1; i <= 5; i++)

            {

                Console.WriteLine("Thread paused for {0} second", 1);

                // Pause thread for 1 second

                Thread.Sleep(1000);

                Console.WriteLine("i value: {0}", i);

            }

            Console.ReadLine();

        }

    }

}

If you observe the above example, we are suspending the main thread execution (1 second) for every iteration of for loop by passing the time in milliseconds to the Sleep method.

 

When we execute the above program, we will get the result like as shown below.

 

Thread paused for 1 second

i value: 1

Thread paused for 1 second

i value: 2

Thread paused for 1 second

i value: 3

Thread paused for 1 second

i value: 4

Thread paused for 1 second

i value: 5

If you observe the above result, for every iteration the Sleep method has suspended the thread for 1 second before it prints the value.

C# Sleep(TimeSpan) Method Example

Following is the example of Sleep method in c# to pause the thread execution for the specified amount of time by passing the time using TimeSpan property.

 

using System;

using System.Threading;

 

namespace TutlaneExamples

{

    class Program

    {

        static void Main(string[] args)

        {

            TimeSpan ts = new TimeSpan(0, 0, 1);

            for (int i = 1; i <= 5; i++)

            {

                Console.WriteLine("Thread paused for {0} second", 1);

                // Pause thread for 1 second

                Thread.Sleep(ts);

                Console.WriteLine("i value: {0}", i);

            }

            Console.WriteLine("Main Thread Exists");

            Console.ReadLine();

        }

    }

}

If you observe the above example, we are suspending the main thread execution (1 second) for every iteration of for loop by passing the time using TimeSpan property to the Sleep method.

 

When we execute the above program, we will get the result like as shown below.

 

Thread paused for 1 second

i value: 1

Thread paused for 1 second

i value: 2

Thread paused for 1 second

i value: 3

Thread paused for 1 second

i value: 4

Thread paused for 1 second

i value: 5

Main Thread Exists

If you observe the above result, for every iteration the Sleep method has suspended the thread for 1 second before it prints the value.

 

This is how we can use the Sleep method in c# to suspend or pause the thread execution for the specified amount of time based on our requirements.

 
 
 

Topics Covered