C# Multithreading - c# - c# tutorial - c# net
What is Multithreading in C# ?
- 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.
Multithreading
System.Threading Namespace
- The System.Threading namespace contains various classes and interfaces to provide the facility of multithreaded programming.
- It also provides classes to synchronize the thread resource.
- Here there are list of commonly used classes are given below:
Process and Thread:
- A process represents an application whereas a thread represents a module of the application.
- Process is heavyweight component whereas thread is lightweight.
- A thread can be termed as lightweight subprocess because it is executed inside a process.
- Whenever you create a process, a separate memory area is occupied. But threads share a common memory area.
C# Thread Life Cycle
- In C#, each thread has a 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.
C# Thread class
- C# Thread class provides properties and methods to create and control threads. It is found in System.Threading namespace.
C# Thread Properties
- A list of important properties of Thread class are given below:
Property | Description |
---|---|
CurrentThread | returns the instance of currently running thread. |
IsAlive | checks whether the current thread is alive or not. It is used to find the execution status of the thread. |
IsBackground | is used to get or set value whether current thread is in background or not. |
ManagedThreadId | is used to get unique id for the current managed thread. |
Name | is used to get or set the name of the current thread. |
Priority | is used to get or set the priority of the current thread. |
ThreadState | is used to return a value representing the thread state. |
C# Thread Methods:
- A list of important methods of Thread class are given below:
Method | Description |
---|---|
Abort() | is used to terminate the thread. It raises ThreadAbortException. |
Interrupt() | is used to interrupt a thread which is in WaitSleepJoin state. |
Join() | is used to block all the calling threads until this thread terminates. |
ResetAbort() | is used to cancel the Abort request for the current thread. |
Resume() | is used to resume the suspended thread. It is obselete. |
Sleep(Int32) | is used to suspend the current thread for the specified milliseconds. |
Start() | changes the current state of the thread to Runnable. |
Suspend() | suspends the current thread if it is not suspended. It is obselete. |
Yield() | is used to yield the execution of current thread to another thread. |
C# Thread Life Cycle: C# Main Thread Example:
- The first thread which is created inside a process is called Main thread. It starts first and ends at last.
- Let's see an example of Main thread in C#.
C# examples - Output :
C# Threading Example: static method:
- We can call static and non-static methods on the execution of the thread.
- To call the static and non-static methods, you need to pass method name in the constructor of ThreadStart class.
- For static method, we don't need to create the instance of the class. You can refer it by the name of class.
C# examples - Output :
- The output of the above program can be anything because there is context switching between the threads.
C# Threading Example: non-static method
- For non-static method, you need to create instance of the class so that you can refer it in the constructor of ThreadStart class.
C# examples - Output :
- Like above program output, the output of this program can be anything because there is context switching between the threads.
C# Threading Example: performing different tasks on each thread
- Let's see an example where we are executing different methods on each thread.
C# examples - Output :
C# Threading Example: Sleep() method
- The Sleep() method suspends the current thread for the specified milliseconds. So, other threads get the chance to start execution.
C# examples - Output :
C# Threading Example: Abort() method:
- The Abort() method is used to terminate the thread. It raises ThreadAbortException if Abort operation is not done.
Read Also
dot net online training , dot net training institute in chennai tamil nadu , .net course contentC# examples - Output :
- Output is unpredictable because thread may be in running state.
C# Threading Example: Join() method
- It causes all the calling threads to wait until the current thread (joined thread) is terminated or completes its task.
C# examples - Output :
C# Threading Example: Naming Thread
- Let's see an example where we are setting and getting names of the threads.
C# examples - Output :
C# Threading Example: ThreadPriority
- Let's see an example where we are changing the priority of the thread. The high priority thread can be executed first. But it is not guaranteed because thread is highly system dependent. It increases the chance of the high priority thread to execute before low priority thread.
C# examples - Output :
- The output is unpredictable because threads are highly system dependent. It may follow any algorithm preemptive or non-preemptive.