C# Thread Sleep() Method

In the C# programming language, the Thread.Sleep method is a static method inside the System.Threading namespace. It pauses the execution of the current thread for a specified duration. The thread does not consume CPU resources, which allows other threads to execute during this pause.

In C#, the Thread.Sleep methods can take an integer value. It represents a millisecond or a TimeSpan object to specify the duration. For Example, Thread.Sleep(1000) means to pause the thread for 1 second, while Thread.Sleep(Timeout.Infinite) can be used to pause the thread indefinitely.

Syntax of the Thread.Sleep function in C#

In C#, the Thread.Sleep method is used to pause the execution of the current thread for a specific duration. It has two main overloads, which have the following syntaxes.

Using int millisecondsTimeout:

This parameter takes an integer value that specifies the amount of time for which the thread will pause using a milliseconds object.

Example

Thread.Sleep(int millisecondsTimeout);

In this syntax,

  • Sleep: The Thread.Sleep method is commonly used to pause the execution of the current thread.
  • millisecondsTimeout: It is an integer value that specifies the number of milliseconds for which the current thread should pause.

Using Timespan timeout:

In C#, the TimeSpan parameter specifies the duration of the sleep in the form of a TimeSpan object. It provides better readability because we can specify time in seconds, minutes, hours, or days.

Example

Thread.Sleep(TimeSpan timeout);

C# Simple Thread.Sleep Example using Main Thread

Let us take an example to illustrate the Thread.Sleep method using the Main Thread in C#.

Example

Example

using System;

using System.Threading;

class C# Tutorial

{

    static void Main()

    {

        Console.WriteLine("The Main Thread Starts here");

        // Loop in the main thread

        for (int m = 1; m <= 10; m++)

        {

            Console.WriteLine("The Main Thread Count is " + m);

            Thread.Sleep(1000); // Pause for 1 second

        }

        Console.WriteLine("The Main Thread Ends.");

    }

}

Output:

Output

The Main Thread Starts here

The Main Thread Count is 1

The Main Thread Count is 2

The Main Thread Count is 3

The Main Thread Count is 4

The Main Thread Count is 5

The Main Thread Count is 6

The Main Thread Count is 7

The Main Thread Count is 8

The Main Thread Count is 9

The Main Thread Count is 10

The Main Thread Ends.

Explanation:

In this example, we demonstrate the use of the Thread.Sleep method in C#. It starts by printing a message, and then we use a for loop from 1 to 10 to iterate over each number, where it prints the count and pauses for 1 second using the Thread.Sleep(1000) function. Finally, it displays a message, which indicates that the Main thread has ended.

C# Thread.Sleep Example using int millisecondsTimeout

Let us take an example to illustrate the Thread.Sleep function using the int milliseconds in the C# programming language .

Example

Example

using System;

using System.Threading;

class C# Tutorial

{

    static void Main()

    {

        Console.WriteLine("Thread starts...");

        // Pause the thread for 1 second

        Thread.Sleep(1000);

        Console.WriteLine("1 second passed");

        // Pause the thread for 5 seconds 

        Thread.Sleep(5000);

        Console.WriteLine("5 seconds passed");

        Console.WriteLine("Thread ends...");

    }

}

Output:

Output

Thread starts...

1 second passed

5 seconds passed

Thread ends...

Explanation:

In this example, we demonstrate how the Thread.Sleep(int milliseconds) methods work in C#. First, the program starts by printing "Thread starts...". After that, we use the Thread.Sleep(1000) function and prints the message "1 second passed". Next, we use the Thread.Sleep(5000) function to pause for 5 seconds and prints the message "5 seconds passed". Finally, we print the message "Thread ends...".

C# Thread.Sleep Example using Timespan Parameter

Let us take an example to illustrate the timespan parameter in Thread.Sleep method in C#.

Example

Example

using System;

using System.Threading;

class C# Tutorial

{

    static void Main()

    {

        Console.WriteLine("Main Thread starts...");



        // Sleep using TimeSpan of 2 seconds

        TimeSpan ts = TimeSpan.FromSeconds(2);

        Thread.Sleep(ts);

        Console.WriteLine("2 seconds passed");

        // Sleep using TimeSpan of 1 minute

        Thread.Sleep(TimeSpan.FromMinutes(1));

        Console.WriteLine("1 minute passed");

    }

}

Output:

Output

Main Thread starts...

2 seconds passed

1 minute passed

Explanation:

In this example, we demonstrate how to use the TimeSpan parameter with the Thread.Sleep method in C#. First, the program starts by printing the message "Main Threads starts...". After that, we use the TimeSpan.FromSeconds(2) to pause the thread for 2 seconds. Next, we use the (TimeSpan.FromMinutes(1)) to pause the thread for 1 minute and print "1 minute passed".

Features of Thread.Sleep Method in C#

There are several features of the Thread.Sleep method in C#. Some main features are as follows:

  • In C#, the Thread.Sleep method is a blocking method, which means that the current thread stops executing for the specified duration.
  • It supports two parameters: int millisecondTimeout and Timespan timeout.
  • It is a static method of a thread class. It is directly called using the Thread.Sleep method.
  • It saves CPU resources because the sleeping thread does not consume processor time.
  • Conclusion

In conclusion, the C# Thread.Sleep method is a static method inside the System.Threading namespace. It is used to pause the execution of the current thread for a specified time. It has two overloads: int millisecondTimeout and Timespan timeout. It provides a simple way to control the timing and flow of thread execution in C#.

C# Thread.Sleep Method FAQs

1) What is the Thread.Sleep method in C#?

In the C# programming language, the Thread.Sleep method is a static method inside the System.Threading namespace. It pauses the execution of the current thread for a specified duration. The thread does not consume CPU resources, which allows other threads to execute during this pause.

2) Which namespace contains the Thread.Sleep method in C#?

The Thread.Sleep method is defined in the System.Threading namespace in C#.

Example

using System.Threading;

3) What are the overloads of Thread.Sleep methods in C#?

In C#, there are mainly two overloads of Thread.Sleep methods in C#. These are as follows:

  • int millisecondTimeout
  • Timespan timeout

4) How can we make a thread sleep indefinitely in C#?

We make a thread sleep indefinitely, using the following approaches.

Example

Thread.Sleep (Timeout.Infinite);

Thread.Sleep (Timeout.InfiniteTimeSpan);

5) How does the Thread.Sleep function differ from the Task.Delay function?

In C#, the Thread.Sleep method differs from Task.Delay method because the Thread.Sleep method is blocking the current thread for a specific duration. On the other hand, the Task.Delay method is a non-blocking and asynchronous method, which allows other tasks or code to execute while waiting for the delay to complete.

6) Is the Thread.Sleep method a static or an instance method in C#?

In C#, the Thread.Sleep method is a static method within the System.Threading namespace is called directly using the Thread class without creating a thread instance.

Input Required

This code uses input(). Please provide values below: