Multithreading in C# is a process in which multiple threads work simultaneously. It is a process to achieve multitasking. It saves time because multiple tasks are being executed at a time.
To create multithreaded application in C#, we need to use System.Threding namespace.
Example:
Let’s understand this concept with a very basic example. Everyone has used Microsoft Word. It takes input from the user and displays it on the screen in one thread while it continues to check spelling and grammatical mistakes in another thread and at the same time another thread saves the document automatically at regular intervals.
Thread Life Cycle
The life cycle of a thread is started when instance of System.Threading.Thread class is created. When the task execution of the thread is completed, its life cycle is ended.
There are following states in the life cycle of a Thread in C#.
- Unstarted
- Runnable (Ready to run)
- Running
- Not Runnable
- Dead (Terminated)
Unstarted State
When the instance of Thread class is created, it is in unstarted state by default.
Runnable State
When start() method on the thread is called, it is in runnable or ready to run state.
Running State
Only one thread within a process can be executed at a time. At the time of execution, thread is in running state.
Not Runnable State
The thread is in not runnable state, if sleep() or wait() method is called on the thread, or input/output operation is blocked.
Dead State
After completing the task, thread enters into dead or terminated state.
Most Common Instance Member of the System.Threading.Thread class
The following are the most common instance members of the System.Threading.Thread class:
Name
A property of string type used to get/set the friendly name of the thread instance.
Priority
A property of type System.Threading.ThreadPriority to schedule the priority of threads.
IsAlive
A Boolean property indicating whether the thread is alive or terminated.
ThreadState
A property of type System.Threading.ThreadState, used to get the value containing the state of the thread.
Start()
Starts the execution of the thread.
Abort()
Allows the current thread to stop the execution of the thread permanently.
Suspend()
Pauses the execution of the thread temporarily.
Resume()
Resumes the execution of a suspended thread.
Join()
Make the current thread wait for another thread to finish.
What is the problem with the above program execution?
In our example, we are just writing some simple code to print the values from 1 to 5. What will you do if one method is taking more than the expected time? Suppose Method2 is going to interact with a database or it is going to invoke any web service which will take more than 10 seconds to provide the response. In that case, the Method2 execution will be delayed for 10 seconds as it is waiting there to get a response back either from the database or from the Web Service. Until Method2 is not completed its execution, Method3 is not going to be executed because of the sequential execution of the program i.e. one by one.