Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to use the sleep method in C#?
The Thread.Sleep() method in C# is used to pause the execution of the current thread for a specified period of time. This is commonly used to introduce delays in program execution or to simulate time-consuming operations.
Syntax
The Thread.Sleep() method has two overloads −
Thread.Sleep(int millisecondsTimeout); Thread.Sleep(TimeSpan timeout);
Parameters
-
millisecondsTimeout − The number of milliseconds for which the thread is suspended. Use
Timeout.Infinite(-1) to suspend the thread indefinitely. -
timeout − A
TimeSpanthat sets the amount of time for which the thread is suspended.
Using Thread.Sleep() with Milliseconds
The most common way to use Thread.Sleep() is by specifying the sleep duration in milliseconds −
using System;
using System.Threading;
class Program {
static void Main(string[] args) {
Console.WriteLine("Program started at: " + DateTime.Now.ToString("HH:mm:ss"));
int sleepfor = 2000; // 2 seconds
Console.WriteLine("Thread will sleep for {0} seconds", sleepfor / 1000);
Thread.Sleep(sleepfor);
Console.WriteLine("Program resumed at: " + DateTime.Now.ToString("HH:mm:ss"));
}
}
The output of the above code is −
Program started at: 14:30:15 Thread will sleep for 2 seconds Program resumed at: 14:30:17
Using Thread.Sleep() with TimeSpan
You can also use TimeSpan for more readable time specifications −
using System;
using System.Threading;
class Program {
static void Main(string[] args) {
Console.WriteLine("Starting countdown...");
for (int i = 5; i >= 1; i--) {
Console.WriteLine(i);
Thread.Sleep(TimeSpan.FromSeconds(1));
}
Console.WriteLine("Time's up!");
}
}
The output of the above code is −
Starting countdown... 5 4 3 2 1 Time's up!
Using Thread.Sleep() in Multithreaded Applications
When working with multiple threads, Thread.Sleep() only pauses the current thread, not the entire application −
using System;
using System.Threading;
class ThreadSleepExample {
public static void WorkerThread(string threadName) {
for (int i = 1; i <= 3; i++) {
Console.WriteLine("{0}: Step {1}", threadName, i);
Thread.Sleep(1000); // Sleep for 1 second
}
Console.WriteLine("{0}: Completed", threadName);
}
static void Main(string[] args) {
Thread thread1 = new Thread(() => WorkerThread("Thread-A"));
Thread thread2 = new Thread(() => WorkerThread("Thread-B"));
Console.WriteLine("Starting both threads...");
thread1.Start();
thread2.Start();
thread1.Join();
thread2.Join();
Console.WriteLine("All threads completed");
}
}
The output of the above code is −
Starting both threads... Thread-A: Step 1 Thread-B: Step 1 Thread-A: Step 2 Thread-B: Step 2 Thread-A: Step 3 Thread-B: Step 3 Thread-A: Completed Thread-B: Completed All threads completed
Common Use Cases
- Simulating delays − Testing time-dependent functionality
- Rate limiting − Controlling the frequency of operations
- Polling intervals − Checking status at regular intervals
- Animation timing − Creating smooth transitions in UI applications
Conclusion
Thread.Sleep() is a simple and effective method to pause thread execution for a specified duration. It accepts either milliseconds as an integer or a TimeSpan object, making it flexible for various timing requirements in both single-threaded and multithreaded applications.
