Multithreading in C#
Multithreading in C#
A thread is defined as the execution path of a program. Each thread defines a unique flow of control. If your application involves complicated and time-consuming operations, then it is often helpful to set different execution paths or threads, with each thread performing a particular job.
Threads are lightweight processes. One common example of use of thread is implementation of concurrent programming by modern operating systems. Use of threads saves wastage of CPU cycle and increase efficiency of an application.
A simple program is one where a single thread runs as a single process which is the running instance of the application. However, this way the application can perform one job at a time. To make it execute more than one task at a time, it could be divided into smaller threads.
Thread Life Cycle
The life cycle of a thread starts when an object of the System.Threading.Thread class is created and ends when the thread is terminated or completes execution.
Following are the various states in the life cycle of a thread −
· The Unstarted State − It is the situation when the instance of the thread is created but the Start method is not called.
· The Ready State − It is the situation when the thread is ready to run and waiting CPU cycle.
· The Not Runnable State − A thread is not executable, when
-
- Sleep method has been called
- Wait method has been called
- Blocked by I/O operations
· The Dead State − It is the situation when the thread completes execution or is aborted.
The Main Thread
In C#, the System.Threading.Thread class is used for working with threads. It allows creating and accessing individual threads in a multithreaded application. The first thread to be executed in a process is called the main thread.
When a C# program starts execution, the main thread is automatically created. The threads created using the Thread class are called the child threads of the main thread. You can access a thread using the CurrentThread property of the Thread class.
The following program demonstrates main thread execution −
using System;
using System.Threading;
namespace MultithreadingApplication {
class MainThreadProgram {
static void Main(string[] args) {
Thread th = Thread.CurrentThread;
th.Name = "MainThread";
Console.WriteLine("This is {0}", th.Name);
Console.ReadKey();
}
}
}
When the above code is compiled and executed, it produces the following result −
This is MainThread
Properties and Methods of the Thread Class
The following table shows some most commonly used properties of the Thread class −
Sr.No. |
Property & Description |
1 |
CurrentContext Gets the current context in which the thread is executing. |
2 |
CurrentCulture Gets or sets the culture for the current thread. |
3 |
CurrentPrinciple Gets or sets the thread's current principal (for role-based security). |
4 |
CurrentThread Gets the currently running thread. |
5 |
CurrentUICulture Gets or sets the current culture used by the Resource Manager to look up culture-specific resources at run-time. |
6 |
ExecutionContext Gets an ExecutionContext object that contains information about the various contexts of the current thread. |
7 |
IsAlive Gets a value indicating the execution status of the current thread. |
8 |
IsBackground Gets or sets a value indicating whether or not a thread is a background thread. |
|
