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 »


ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.