// Get the state of the current thread ThreadState state = Thread.CurrentThread.ThreadState; Console.WriteLine("Current thread state: " + state);
// Create a new thread and start it Thread thread = new Thread(() => { Console.WriteLine("Thread is running..."); }); thread.Start(); // Check if the thread is running while (thread.ThreadState == ThreadState.Running) { // Do something else while waiting for the thread to finish } Console.WriteLine("Thread finished.");
// Get the current process Process process = Process.GetCurrentProcess(); // Get the state of each thread in the process foreach (ProcessThread thread in process.Threads) { Console.WriteLine("Thread ID: " + thread.Id + ", State: " + thread.ThreadState); }The AThreadState class is included in the System.Diagnostics namespace of the .NET Framework, which is part of the core .NET class library.