Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C# Data Types
  • C# Decision Making
  • C# Methods
  • C# Delegates
  • C# Constructors
  • C# Arrays
  • C# ArrayList
  • C# String
  • C# Tuple
  • C# Indexers
  • C# Interface
  • C# Multithreading
  • C# Exception
Open In App
Next Article:
How to Create Threads in C#?
Next article icon

C# | How to check current state of a thread

Last Updated : 01 Feb, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report

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:

  • https://docs.microsoft.com/en-us/dotnet/api/system.threading.thread.threadstate?view=netframework-4.7.2


Next Article
How to Create Threads in C#?
author
ankita_saini
Improve
Article Tags :
  • C#
  • CSharp Multithreading
  • CSharp Thread Class

Similar Reads

  • How to Terminate a Thread in C#?
    In C#, threads are used to achieve tasks concurrently, a fundamental feature in multithreading and parallel programming. However, there are scenarios where we need to terminate a thread. There are different ways to terminate a thread and in this article, we will discuss those ways with their code im
    6 min read
  • C# Lifecycle and States of a Thread
    In C#, threads enable the concurrent execution of tasks, it improves application performance and responsiveness. Understanding the lifecycle and states of a thread is essential for managing and synchronizing threads efficiently. A thread in C# can exist in one of the following states at any given ti
    4 min read
  • How to Create Threads in C#?
    Multithreading enables concurrent task execution in C#. It improves performance and responsiveness. We can create threads using the System.Threading namespace. With the help of Threads, we can achieve multitasking and can define their behavior using different methods provided by the Thread Class. Ex
    6 min read
  • How to Schedule a Thread for Execution in C#?
    In C#, we can schedule a thread for execution using the Thread class. The Thread.Start() method is responsible for scheduling a thread, allowing it to run when system resources are available. This method can be overloaded to pass parameters to the thread. Also, the Thread.Sleep() method can be used
    3 min read
  • C# | Check if a thread belongs to managed thread pool or not
    A Thread class is responsible for creating and managing a thread in multi-thread programming. It provides a property known as IsThreadPoolThread to check if the thread belongs to the managed thread pool or not. Syntax: public bool IsThreadPoolThread { get; } Return Value: This property returns true
    2 min read
  • How to Check Whether a Thread is Alive or Not in C#?
    A Thread class is responsible for creating and managing a thread in multi-thread programming. It provides a property known as IsAlive to check if the thread is alive or not. In other words, the value of this property indicates the current execution of the thread. Example 1: This example demonstrates
    3 min read
  • How to check whether a thread is a background thread or not in C#
    As we know that thread is created and managed by the Thread class. So, the Thread class provides a property known as IsBackground property to check whether the given thread is running in the background or in the foreground. If the value of IsBackground is set to be true, then it means the thread is
    3 min read
  • Suspending the current thread for the specified amount of time in C#
    In C#, a Sleep() method temporarily suspends the current execution of the thread for specified milliseconds, so that other threads can get the chance to start the execution, or may get the CPU for execution. There are two methods in the overload list of Thread.Sleep Method as follows: Sleep(Int32) S
    3 min read
  • Naming a thread and fetching name of current thread in C#
    A thread is a light-weight process within a process. In C#, a user is allowed to assign a name to the thread and also find the name of the current working thread by using the Thread.Name property of the Thread class. Syntax : public string Name { get; set; } Here, the string contains the name of the
    2 min read
  • C# | Check if an array is synchronized (thread safe) or not
    Array.IsSynchronized Property is used to get a value which indicates whether access to the Array is synchronized (thread-safe) or not. Syntax: public bool IsSynchronized { get; } Property Value: This property always returns false for all arrays. Below programs illustrate the use of above-discussed p
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences