C# Thread Sleep

王林
王林Original
2024-09-03 15:25:49789browse

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.

Working of C# Thread Sleep

  • Whenever there is a need to pause the execution of a thread so that other thread can take over and start execution, we make use of the method called Sleep() method.
  • The amount of time a thread needs to be paused can be specified in either millisecond and passed as a parameter to Sleep() method or it can be specified in hours, minutes, and seconds using timespan property.
  • ArgumentOutOfRangeException is thrown by the Sleep() method if the time in milliseconds passed as a parameter to it is negative.
  • ArgumentOutOfRangeException is thrown by the Sleep() method if the time passed as a parameter to timespan property is negative.
  • If the time in milliseconds passed as a parameter to the Sleep() method is zero, then the other thread of the same priority which is ready to run starts execution.
  • If the time passed as a parameter to the timespan property is zero, then the other thread of the same priority which is ready to run starts execution.

Examples to Implement Thread Sleep in C#

Below are the examples of C#Thread Sleep:

Example #1

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:

C# Thread Sleep

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.

Example #2

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:

C# Thread Sleep

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.

Conclusion

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.

Recommended Article

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 –

  1. What is Random Number Generator in C#?
  2. Static Constructor in Java | Working | Applications
  3. TextWriter in C# | Examples
  4. How to Work Static Constructor in C#?

The above is the detailed content of C# Thread Sleep. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:C# Thread JoinNext article:C# Thread Join