Home »
.Net »
C# Programs
C# program to pause a thread
Pausing a thread in C#: Here, we are going to learn how to pause a thread in C#?
Submitted by Nidhi, on August 16, 2020
To solve the above problem, we need to use Thread.Sleep() method, this method takes an argument in milliseconds to pause the thread.
Program:
/*
* Program to Pause a Thread in C#
*/
using System;
using System.Threading;
class Program
{
static void Main()
{
int loop=0;
for (loop = 1; loop <= 4; loop++)
{
Console.WriteLine("Sleep Main thread for 1 Second");
Thread.Sleep(1000);
}
Console.WriteLine("Main thread Finished");
}
}
Output:
Sleep Main thread for 1 Second
Sleep Main thread for 1 Second
Sleep Main thread for 1 Second
Sleep Main thread for 1 Second
Main thread Finished
Press any key to continue . . .
Explanation:
In the above program, we created a program class that contains a Main() method. In the Main() method, we created a for loop that will execute 4 times, here we used Thread.Sleep() method that will pause or sleep the Main thread for 1 second when Thread.Sleep() method gets called.
C# Thread Programs »