c sharp

C# Multithreading

A thread is said to be a lightweight process, task, or execution path of any code or program. Threads are used to perform a specific task or job and have their control. The concept of multithreading arrives when we divide a big thread into small threads to make them execute simultaneously. This way, the workload can be managed and complexity can be avoided. Within the C# programming, we have been provided with the “system.Threading.Thread” class to perform multithreading by utilizing its useful functions. This article is for every C# user who has no experience in multithreading and wants to learn its basics. Let’s get started now.

Example # 01: The Main Thread

You can verify for the current working directory to search your newly generated file. Open your new C# file in any of the available editors from your Ubuntu 20.04 system. We have opened it within the “text” editor, as it is very convenient to use for beginners as well as for experts. As the topic suggests, we will be discussing the concept of multithreading. When we create an object of the “Thread” class, we create a child thread of the currently executing thread. If you want to use the object to specify the name of a parent thread, you need to use the “CurrentThread” property of the Thread class.

So, within our first example, we will be looking at the property “CurrentThread” of the Thread class of C#. We are starting our first example code with the initialization of the “system” library and “System.Threading” class. We have created a namespace named “Multithread” and created a class “Threading” in it. The main() function of this class has been started with the initialization of the “Thread” class object while using the “CurrentThread” property in it. Now, the object is pointing towards the parent thread.

We have been using the parent thread object “t” to call the “Name” property of the Thread class to get the name of a current thread i.e. the main() thread. We have given it the name “Main” as per the below code. The Writeline() function from the Console class is used to display the name of a parent thread on our terminal screen by calling the “Name” property by the “t” object of the Thread class.

The very first step after saving our code is to compile the C# code. For this, your system must have the C# “mcs” compiler built into it. After compiling this file with “mcs”, the code “exe” file will be automatically generated in the current working directory of Ubuntu 20.04. We have to use this “exe” file to execute the code with the “mono” runtime instruction for C#. After running it, we have found that the “Main” thread is running.

Example # 02: Create a Child Thread

Let’s take a glance at the creation of a new thread using some “non-static” data function in the code. For this, we have used the same structure of a C# code i.e. the use of the “System” library, and “System.Threading” class. We have created two classes i.e. myThread, and New. Execution of this code will be started from the New class as it contains the main() function thread. The Console.WriteLine() statements will show us that the main() function is running.

After that, the object “t1” of a “myThread” class has been created. This object creation doesn’t mean that we have created a thread object. This object is utilized to call the myThread class functions. The Thread class object “t” has been created to create a new thread. While the ThreadStart property of this Thread class has been used to start the function “threading” from the “myThread” class as a new thread.

The object “t1” is calling its function in the parameters of the “ThreadStart” function as a start to a new thread. You need to use the “t” object to call the “Start” function of the Thread class to start the execution of a new thread. So, the threading() function of the myThread class will be executed. It’s for loop will be executed 5 times, displaying that the thread function is running. The code is now completed. Let’s see how it works.

After compiling and running our C# “cs” and “exe” files respectively, we have got the shown-below output. The child thread from the “myThread” class got executed 5 times, as per the “for” loop.

Example # 03: More Than One Thread

The above example was about the creation of a single thread. Within this example, we will discuss the use of static functions to create more than 1 child thread in the program. So, we have created two classes with the same names i.e. myThread and New. The New class contains the only main() method that is being used to create two objects t1 and t2 for the Thread class to create new child threads.

These objects have been using the threading1() and threading2() functions of the myThread class to create them as new child threads. The “start()” function of the Thread class has been used to start the threading1() function and threading2() function using the t1 and t2 thread objects. As both the thread functions are static, the second thread cannot be started until the first one completed its execution. Both threads executed their statements 3 times and the code will be terminated.

After running this compiled code, the output shows the execution of both threads separately.

Example # 04: Abort Thread

The Thread class has the Abort() function to terminate the threads. It uses the ThreadAbortExecption to perform the termination. Let’s take a look at this by creating two classes, myThread and New. The main() function started with the object “t” creation for the myThread class with its name “thread”.

The Constructor of the myThread class started to run and initialized a new child thread with the Thread class “t1” object using the “StartThread” function in its parameters. The name has been assigned to the Name property and the thread got started. The “StartThread” thread functions start with a “try-catch” statement. The “try” statement will continue running until the modulus of a value in its loop by 7 is equal to 0. Where the exception happens, the catch() statement will throw Abort exception.

After running this code, we have got the thread executed until the value 49, and terminated at code 300. The thread has been aborted due to the “if” statement and the Console.WriteLine() statement before the catch() statement has been avoided.

Conclusion

This guide is a must-have for every C# user who wants to learn the basics of multithreading. We have thoroughly explained the use of the Thread class in C#, to create threads and name them with the Current thread and “Name” property. We have discussed the use of “Thread” class object creation to create a single thread or multiple threads at a time and start them with the “Start” function of the Thread class. We have also seen how to use the Abort() function in the C# code to terminate a thread with the help of a try-catch statement in the code.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.