Sleep pauses programs. 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. Sleep is found on the Thread class. It is a static method that receives one parameter. You must either include System.Threading or use the fully qualified name for the method invocation, which is System.Threading.Thread.Sleep.

Tip: You do not need to create a new Thread to use the Sleep method as it is static.

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 results of the Console.WriteLine calls are shown.

Implementation. Next we review 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.

Tip: Thread.Sleep likely will end up calling the same code in the Windows kernel that the Sleep call in any language uses.

SpinWait. 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.

So: Sleep uses the operating system to "pause" the program, resulting in 0% CPU, while Thread.SpinWait executes useless instructions.

Summary. Thread.Sleep is fairly accurate in pausing the program for the specified number of milliseconds. It does not cause your program to consume 100% CPU. This 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 SpinWait.