In this article, we will learn about multithreading in c#, so we will see that where and why can we use multithreading in our application.
Before understanding this concept called multithreading, we first need to see that we actually means by multithreading, so multithreading is a way of doing multiple tasks in one time or we should say that we can do more then a single task concurrently.
So, like we have an operating system running multiple applications at one time, like we can run a media player application alongside using the chrome browser alongside opening a few documents in word, and for running all those applications, the operating system makes use of something called Process.
A process is the part of operating system, which is responsible for executing the programs or the applications.
So, as you can see in the image above that inside the Task manager, multiple processes are running concurrently and that indicates that the operating system is running and executing multiple tasks in parallel, some of the processes running are the background processes and those are actually the windows services which are running in the background.
So the operating system makes the use of something called a Process to execute the application, and under the process there is something which we use to run the code of that application which we call as Thread.
A Thread is a light weight process Or it is the unit of process which is responsible for the execute the code of the application.
So every application has some code logic, and it is the Thread which takes care of running that code.
Every application or program has at least one Thread by default which is called the Main Thread and we call that application as single threaded application.
So, now we will see a single threaded application alongside understanding the concept of multithreading.
So lets create a console application in visual studio, we will name it as ThreadExample.
So, for using Threads in our c# application, we use the
system.threading namespace, An example below shows the use of a default or a single threaded application.
As shown above, that every application makes use of a single Thread called as the Main Thread, which is also the default thread.
Now, we will see the drawbacks of a single threaded application, we will see the working of a single threaded application which in this case will take care of all the tasks within the application and will perform all the given tasks sequentially using the Main thread, so let us see this in our example.
So, as seen in the above code, we are making use of a single thread to do multitasking in our application, so what it does is that it is executing all the 3 methods in a sequential order, and after the completion of one method, it takes the control away from that method and gives it to the other method, this is what the output looks like.
So, this is something which is not an appropriate approach to handle multitasking, so that is where we are introducing this concept of multithreading.
So, the problem with the above approach is that we might have processes taking long time for execution, and that would cause the other processes to wait for the completion of a single long time taking process, the other major problem with this approach is that multitasking is not taking place here in an efficient way and this ends up blocking the entire application.
So, now let us see the execution of the code above using the concept of multithreading.
So, you can see in the above image, we are having 3 methods, we have considered the Method2 to be responsible for performing a time taking database operation, and hence we have a delay of 10 seconds inside that method, so that the process of multithreading can be understand in a much better way, apart from that we have 2 more methods Method1 and Method2.
We are calling those methods from the main function using 3 different threads for each thread respectively.
So, as it can be seen that inside our main method, we are calling those methods using 3 different threads, which are responsible for handling their own method.
so we have threads t1,t2,t3 and each one of them are responsible for managing their own method, this is how the output looks like after this piece of code.
So, we can see in the above image, Method2 was a time taking process, and hence Method3 did not wait until the completion of Method2, but since as we know that all those tasks are handled by their own threads, so that is why Method3 did not have to wait until the completion of Method2, so here when the goes to Method2, after the 3rd iteration, the application takes the control away and gives it to Method3, which completes all of its iteration, after which it tries to execute the remaining iterations of Method2 along with also ending the Thread2 for Method2.