Ad space (banner)
#๏ธโƒฃC# Lessons
Lesson 56 / 68

Waiting for a Fixed Amount of Time (Thread.Sleep)

In this lesson you will learn how to pause for a set time with Thread.Sleep in C#, covering the basics of programs that deal with time. This is for anyone searching for "C# Thread.Sleep how to use".

Thread.Sleep(milliseconds) pauses processing for the time you specify. Use it to space out calls to an API, or to slow a process down deliberately.

The sample code prints "Starting", waits one second with Thread.Sleep(1000), and then prints "Ran after 1 second". Run it and you will feel the one-second gap between the two messages.

A common stumbling block for beginners is over-using Sleep and making the whole program feel unresponsive. It is also the foundation of programs that deal with the passage of time, such as GUI applications and game frame timing.

In real-world development, Task.Delay combined with async/await is preferred for asynchronous waiting in real applications.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run C# directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)