A Thread class is responsible for creating and managing a thread in multi-thread programming. It provides a property known as ThreadState to check the current state of the thread. The initial state of a thread is Unstarted state.
Syntax:
public ThreadState ThreadState{ get; }
Return Value: This property returns the value that indicates the state of the current thread.
Below programs illustrate the use of ThreadState property:
Example 1:
// C# program to illustrate the // use of ThreadState property using System; using System.Threading; public class GFG { // Main Method static public void Main() { Thread thr; // Get the reference of main Thread // Using CurrentThread property thr = Thread.CurrentThread; // Display the current state // of the main thread Console.WriteLine("The name of the current state of the Main " + "thread is: {0}", thr.ThreadState); } } |
Output:
The name of the current state of the main thread is: Running
Example 2:
// C# program to illustrate the // use of ThreadState property using System; using System.Threading; public class GFG { // Main method public static void Main() { // Creating and initializing threads Thread TR1 = new Thread(new ThreadStart(job)); Thread TR2 = new Thread(new ThreadStart(job)); Console.WriteLine("ThreadState of TR1 thread"+ " is: {0}", TR1.ThreadState); Console.WriteLine("ThreadState of TR2 thread"+ " is: {0}", TR2.ThreadState); // Running state TR1.Start(); Console.WriteLine("ThreadState of TR1 thread "+ "is: {0}", TR1.ThreadState); TR2.Start(); Console.WriteLine("ThreadState of TR2 thread"+ " is: {0}", TR2.ThreadState); } // Static method public static void job() { Thread.Sleep(2000); } } |
Output:
ThreadState of TR1 thread is: Unstarted ThreadState of TR2 thread is: Unstarted ThreadState of TR1 thread is: Running ThreadState of TR2 thread is: WaitSleepJoin
Reference:
Recommended Posts:
- Naming a thread and fetching name of current thread in C#
- How to check whether a thread is a background thread or not in C#
- C# | Check if a thread belongs to managed thread pool or not
- Suspending the current thread for the specified amount of time in C#
- C# | Getting the unique identifier for the current managed thread
- C# | Check if StringCollection is synchronized (thread safe)
- C# | Check if the BitArray is synchronized (thread safe)
- C# | Check if StringDictionary is synchronized (thread safe)
- C# | Check if ListDictionary is synchronized (thread safe)
- C# | Check if HybridDictionary is Synchronized (thread safe)
- C# | Check if Hashtable is synchronized (thread safe)
- C# | Check if ArrayList is Synchronized (thread safe)
- C# | Check if an array is synchronized (thread safe) or not
- How to check whether a thread is alive or not in C#
- C# | Creating a synchronized (thread-safe) wrapper for a SortedList object
- Thread.CurrentThread Property in C#
- C# | Creating a synchronized (thread-safe) wrapper for the Hashtable
- C# | Creating a synchronized (thread-safe) wrapper for the ArrayList
- C# | Thread Priority in Multithreading
- Lifecycle and States of a Thread in C#
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

