In C sharp, we use thread to carry out different tasks in the operating system. These threads can perform many functions depending on the task we have assigned. Each task is done depending on the priority basis. At a time, more than one thread is processing, making the processor multithreaded. So while prioritizing the tasks in multithreading, we use a method that suspends one thread temporarily for some time, making it rest for a while, so that in that time, other threads get a chance to execute first.
After that, those threads that were suspended carried out the process. This is done through a sleep method. In C sharp, the sleep method seizes the current execution of the thread for milliseconds specified in the program. A sleep() method can be used in two ways.
This guide will use both methods in explaining the concept of sleep() function in threading by using C sharp in the Linux operating system.
Syntax
The int type variable for milliseconds contains a number in milliseconds to suspend the thread for that specified time.
While initializing the time in milliseconds as a parameter for the sleep function, if the value of the millisecond’s argument is zero, the thread will call another process with the equal priority that is ready to be executed. If no other thread has the same priority, then the execution of the first thread is not ceased and continues to execute.
Example 1: Sleep Function With Milliseconds
As we know, the sleep method is concerned with the multithreading process. Threads are an important part of our operating system that carries many functions at a time, performing multitasking. As described above, the sleep function helps prioritize the task by giving some rest to one thread in multithreading. The same concept will be performed in this example. To continue with the example, we will first explain the usage of the libraries. The system library is for the classes, objects, and other container types. Whereas the system.threading is an additional library that will be included specifically for the threading process.
# Using System.Threading;
Two functions are declared here with thread1() and thread2(). Both functions are non-static. That means they are not inherited or cannot be further used in any other class. In other words, both functions are independent. Inside the first function, thread1(), we have used a for loop that will iterate only two times and display the message that thread1 is working. Inside the loop, the sleep() method is used. This is used to pause the working of the thread for some time. We have declared this function in 4 seconds.
So by using the sleep method, the thread’s working will be seized for 4 seconds.
This thread method is accessed through the thread itself.
Similarly, another function is declared, which is a thread (). This also contains a ‘for’ loop so that it can also iterate two times, and each time it will display the message that thread2 is working. This method is also a non-static method. There we will not use the sleep function inside the loop.
After both the methods, we will declare the main program. As the threads in C sharp are declared in the class, it is mandatory to create the instance of the thread. That object instance will be used in creating the threads furthermore. The name of the class will be mentioned along.
The method mythread will be created. We will use this object to create the threads and then initialize them.
By using appended two statements, the objects for the threads are created. After that, we will start the threads by using the thr1 and thr2 objects.
# Thr2.start();
Now save the file. And we will use a compiler to compile the above source code. This is MCS used for the compilation in C sharp. Furthermore, after the compilation, the execution of the code takes place. Mono is used to execute the code with the .exe extension of the file, making it executable.
$ mono file.exe
On execution of the code, you can see that the first and second threads are executed, and the first thread is executed later, whereas the object of the first thread is created before the second one. And the first thread starts first rather than the second thread. This is because of the sleep function we used in the first thread loop.
This program will work so that when the first thread is called to start, the sleep function is initialized, and the thread goes to sleep for 4 seconds. The second thread gets executed and displays the statement 2 times according to the loop. During this time, the sleep function has completed its time of 4 seconds. So now it will be processed and will display the statement two times.
Example 2: Sleep Function Using Timespan
This example uses a timespan of some seconds. Each statement inside the thread function will be executed with a gap in time for the specified period. This shows us that the thread is sleeping for a specific period. Let us see this concept by demonstrating an example. Inside the class, in the main program, we will first declare the timeout value in the variable. This value is declared dynamically using the ‘new’ operator. The keyword timespan shows that the timeout variable is of timespan type.
We will use a for loop to iterate till the iteration reaches the 3rd time. The loop will display the message that the thread is sleeping for 3 seconds. Inside the for loop, we will use the sleep method of timespan. This method will be called through the thread and contain the Timespan as the parameter.
A console statement will display the message that the main thread exits outside the loop.
When we execute the code, the first-time message will be displayed. Then there will be a pause of 3 seconds; then again, another line is displayed. Similarly, this line is displayed after 3 seconds have passed.
Whereas the last line is declared outside the loop body, the main thread will appear abruptly after the third line. We have not used the sleep timespan method for this last line.
Conclusion
The sleep() method in C sharp is used to cease the process for some seconds or to make the process execute after a specified pause. This suspension of execution for some seconds is good for the working of threads in the operating system, as it makes one thread wait and rests for some time to work later effectively. Secondly, it gives a chance to the other threads to get executed in this time interval. This sleep method is effective to use while the CPU is multitasking. Both the methods for sleep function are used through the milliseconds and through the timespan.