Home >Backend Development >C#.Net Tutorial >C# Thread Sleep
The thread that is executing currently can be paused or temporarily suspended for a specified amount of time using a method in C# called Sleep() method and the amount of time must be specified in milliseconds and passed as a parameter to the thread we are trying to suspend or by using timespan property in which there is a privilege to specify the time in hours, minutes and seconds so that the thread can be paused for a longer time as well as per our need and not just for milliseconds.
Syntax:
Thread.Sleep(Time_in_milliseconds);
Or
TimeSpaninstance_name = new TimeSpan(Time_in_hours_minutes_seconds); Thread.Sleep(instance_name);
Where instance_name is the name of the instance of TimeSpan class.
Below are the examples of C#Thread Sleep:
C# program to demonstrate Sleep() method with time in milliseconds passed as a parameter.
Code:
using System; using System.Threading; //a class called program is created public class program { //a method called samplemethod which accepts a parameter is created public void samplemethod(object str) { for (int z = 0; z < 5; z++) { Console.WriteLine("The thread that is executing is {0}", str); Thread.Sleep(200); } } } //a class called check is created public class check { //main method is called public static void Main() { //two string variables are created which are passed as parameter previously created method in program class string Iamthread1 = null; string Iamthread2 = null; //an instance of the program class is created program d = new program(); Thread firstthread = new Thread(new ParameterizedThreadStart(d.samplemethod)); Thread secondthread=new Thread(new ParameterizedThreadStart(d.samplemethod)); firstthread.Start("Iamthread1"); secondthread.Start("Iamthread2"); } }
Output:
Explanation: In the above program, a namespace called a program is created within which a method called sample method, which accepts a parameter is created inside which the thread that operates on the method is paused for a certain time by using Sleep() method. Then a class called check is created within which the main method is called in which the two instances of threads are created and then begins their execution on sample method using Start() method. Since Sleep() method is used on the threads operating on the sampling method, the threads are not executed in a row.
C# program to demonstrate Sleep() method using TimeSpan property.
Code:
using System; using System.Threading; //a class called program is created public class program { //a method called samplemethod which accepts a parameter is created public void samplemethod(object str) { //TimeSpan property is used to specify the duration of time for which the thread must be paused TimeSpan timeout = new TimeSpan(0, 0, 3); for (int z = 0; z < 3; z++) { Console.WriteLine("The thread that is executing is {0}", str); Thread.Sleep(timeout); } } } //a class called check is created public class check { //main method is called public static void Main() { //two string variables are created which are passed as parameter previously created method in program class string Iamthread1 = null; string Iamthread2 = null; //an instance of the program class is created program d = new program(); Thread firstthread = new Thread(new ParameterizedThreadStart(d.samplemethod)); Thread secondthread = new Thread(new ParameterizedThreadStart(d.samplemethod)); firstthread.Start("Iamthread1"); secondthread.Start("Iamthread2"); } }
Output:
Explanation: In the above program, a namespace called a program is created within which a method called sample method which accepts a parameter is created inside which the thread that operates on the method is paused for a certain time by using TimeSpan property. Then a class called check is created within which the main method is called in which the two instances of threads are created and then begins their execution on sample method using Start() method. Since the Sleep() method is used on the threads operating on the sampling method, the threads are not executed in a row. The output is shown in the snapshot above.
In this tutorial, we understand the concept of the ThreadSleep method in C# through definition, syntax, and working of the ThreadSleep method through programming examples and their outputs.
This is a guide to C# Thread Sleep. Here we discuss the Introduction to C# Thread Sleep and its working along with its examples and Code Implementation. You can also go through our other suggested articles to learn more –
The above is the detailed content of C# Thread Sleep. For more information, please follow other related articles on the PHP Chinese website!