Threading Tasks in C#
In the C# programming language, we use the “Systems Threading” and “Systems Threading Task” namespace to break down every command in the program and handle it as a thread that will be executed according to the flow of the program. By using the keyword “Thread” or “Task,” we specify the current task and give it instructions for the execution, and in this case, the methods of Sleep and Delay will come in handy.
Now we will look at the syntax of both these methods in the C# programming language.
Sleep() Function
In the above snippet, we can see the syntax for the Sleep() function. The sleep function takes in an integer variable as a parameter which specifies the time for a task to be stopped, and it processes values in milliseconds, so for a second of time, this function is present in the “Systems Threading” namespace so that it will be used with the “Thread” keyword.
Delay() Function
The above snippet shows the syntax for the Delay() function. This function is present in the “Systems Threading Tasks” namespace, so that this function will be used with the “Task” keyword. The Delay() function has the parameter, an integer variable that specifies the time for a task to be stopped and processes values in milliseconds.
Now we are going to implement both these functions to stop the program flow for several seconds in the Ubuntu 20.04 environment.
Example 01: Using the Sleep() Function to Freeze a Thread for a Few Seconds in Ubuntu 20.04
In this example, we will be implementing a simplified Sleep() method to freeze a thread for some seconds in a C# program. In this example, we will be using the “Systems Threading” namespace to call the Sleep() function. The delay would be observed at the runtime of the Program as it will take the given time to get to the next task when the delay is initiated.
In the above C# code, we have written three commands for the Compiler to execute. In the first command, we will be printing a line using the Console Write Line Function, and in the second line, we are instructing the thread to wait for 3 seconds before getting to the third command, which is also for printing a line. This means that the third command will be executed after 3 seconds of the first command.
As we can see in the above output, the program ran successfully, and all the 3 commands were carried out as there was a delay of 3 seconds between the first and third command, which was observed in the real-time execution of the program.
Example 02: Using the Delay Method to Delay a Task for a Few Seconds in Ubuntu 20.04
In this example, we will use a simple Delay() function in a C# program to delay a task for a few seconds. We’ll call the Delay() method from the “Systems Threading Tasks” namespace in this example. When the delay is launched, it will take the provided time to go to the next task, which will be visible throughout the program’s execution on the command line terminal of Ubuntu 20.04.
We will start a task and provide a delay in these tasks using the Delay() method in the above C# code, and we will monitor these delays using the “Date Time Now” function. We will create 3 delays in this program by using a for loop and timing them by printing the Date Time function in the string format at each delay. Then the ending time of the program will also be printed to see the time difference between the start and end of the program.
In the above output screenshot, we can see that the whole program took 9 seconds to execute, and each of the 3 delays was 3 seconds apart from each other.
Example 03: Using the Delay and Sleep Function Together to Delay a Task for a Few Seconds in Ubuntu 20.04
In this example, we will be implementing a delay in a task for several seconds by using both the Delay() and Sleep() methods in a C# program. In this example, we will use the “Systems Threading” and “Systems Threading Tasks” namespaces to use the Sleep() and Delay functions. The delay would be observed at the program’s runtime as it will take the given time and run the sleep function until the Delay() function specifies to do so.
This implementation will start by telling the initiation time of the program. A variable will also be initialized in which the Delay() function would be used to specify the delay time in seconds by using the “TimeSpan.FromSeconds” function. After that, we will use the while loop to initialize the Sleep() method and then use the “seconds” string variable to output the program’s finishing time as well as the overall time it took.
As we can see in the output screen, we have the program’s starting and ending time, which is 3 seconds, as mentioned in the last line of the terminal.
Now we will perform another method for waiting several seconds for a task other than the Sleep and Delay function.
Using the Stopwatch Class’s Elapsed Milliseconds Method to Create a Waiting Time for Some Seconds in Ubuntu 20.04
In this method, we will be using the “System Diagnostics” namespace to access the Stopwatch class. This class has all the functions related to timekeeping and monitoring the duration of programs or part of a program. We will be getting the Elapsed Milliseconds method to create a delay of several seconds in the program from the Stopwatch class.
In the above C# program, we will initialize an object of the Stopwatch class and start the program’s timer. After this, we will be starting a while loop with the Stopwatch Class’s Elapsed Milliseconds function as the parameter. And then print the time taken as we specified before. This will freeze the program for 3000 milliseconds, equivalent to 3 Seconds.
After compiling and executing the Stopwatch Class’s function code, we will get the above output. The time difference between the first and second lines is 3 seconds, and it was monitored during the run time execution of the program.
Conclusion
In this article, we discussed several methods to wait for several seconds during a C# program. The different namespaces and methods in the C# programming language used in this concept were discussed in this article. Then these different methods to delay a program were implemented in the Ubuntu 20.04 environment to get a better hold of this concept.