C# Sleep

Threads illustration

Sleep pauses programs. It is found in the System.Threading namespace. It receives a value indicating the number of milliseconds to wait. This can be useful for waiting on an external application or task. It does not cause CPU usage during the pause.

Example

First, the Thread.Sleep method is found on the Thread class and is a static method that receives one parameter. You must either including the using directive for System.Threading or use the fully qualified name for the method invocation, which is System.Threading.Thread.Sleep. You do not need to create a new Thread() to use the Sleep method as it is static. This example shows the runtime behavior of the Thread.Sleep method in the .NET Framework.

This C# example program shows the effect of the Sleep method from System.Threading.

Program that sleeps [C#]

using System;
using System.Diagnostics;
using System.Threading;

class Program
{
    static void Main()
    {
	//
	// Demonstrates three different ways of calling Sleep.
	//
	var stopwatch = Stopwatch.StartNew();
	Thread.Sleep(0);
	stopwatch.Stop();
	Console.WriteLine(stopwatch.ElapsedMilliseconds);
	Console.WriteLine(DateTime.Now.ToLongTimeString());

	stopwatch = Stopwatch.StartNew();
	Thread.Sleep(5000);
	stopwatch.Stop();
	Console.WriteLine(stopwatch.ElapsedMilliseconds);
	Console.WriteLine(DateTime.Now.ToLongTimeString());

	stopwatch = Stopwatch.StartNew();
	System.Threading.Thread.Sleep(1000);
	stopwatch.Stop();
	Console.WriteLine(stopwatch.ElapsedMilliseconds);

	//
	// Bonus: shows SpinWait method.
	//
	stopwatch = Stopwatch.StartNew();
	Thread.SpinWait(100000 * 10000);
	stopwatch.Stop();
	Console.WriteLine(stopwatch.ElapsedMilliseconds);
    }
}

Output

0              ElapsedMilliseconds after Sleep(0)
8:14:43 AM     Time after Sleep(0)
4999           ElapsedMilliseconds after Sleep(5000)
8:14:48 AM     Time after Sleep(5000)
999            ElapsedMilliseconds after Sleep(1000)
3144           ElapsedMilliseconds after SpinWait(Int32)
Main method

Overview. The program defines the Main entry point in the Program class, which contains three method invocations to the Thread.Sleep method with varying calling syntaxes and parameters. The surrounding code around the Sleep invocations simply takes the system's time and uses a Stopwatch instance to time the Thread.Sleep calls accurately.

Stopwatch Examples

Implementation

.NET Framework information

Here we note the implementation of the Thread.Sleep method and the Thread.SpinLock method in the .NET Framework. These two methods call into the SleepInternal and SpinWaitInternal functions in mscorlib. It is probably safe to assume that Thread.Sleep will end up calling the same code in the Windows kernel that the Sleep call in any programming language will use.

Idle loops

When you execute the above program, you will notice that the program does not require significant CPU time when executing the Thread.Sleep calls. It only starts consuming the entire CPU core when the Thread.SpinWait method is invoked. This means that Thread.Sleep uses the operating system to "pause" the program, resulting in 0% CPU, while Thread.SpinWait actually executes useless instructions.

Thread.SpinWait Example

Summary

The C# programming language

We looked at how you can use the Thread.Sleep method in the C# programming language and how it is fairly accurate in pausing the program for the specified number of milliseconds. We saw that the Thread.Sleep method does not cause your program to consume 100% CPU, which will save power and provide more resources for other programs. Finally, we noted the implementation in the .NET Framework and also talked about idle loops and various uses of speedup loops.

Thread Overview
.NET