Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Thread's state and "Sleep"

I'm trying to get a .NET thread state.
For this I check the ProcessThread.ThreadState property.
However when I use Thread.Sleep on that thread and check its state with Process Explorer - I see that it's in "Wait: Delay Exectuion", while my ThreadState is still "Running".
How can that be?

like image 374
Idov Avatar asked Mar 17 '26 04:03

Idov


1 Answers

The Process class caches properties on first access, so you will probably need to call the Refresh method to get an updated ThreadState. It seems that the ProcessThread objects (from the ProcessThreads property) are not attached to the parent Process, and the values it contains are not updated when Refresh is called. You will need to go through the Process object again.

Something like:

Process p = Process.GetProcessByName("MyProcess);

while(true)
{
  p.Refresh();
  Console.WriteLine(p.ProcessThreads[0].ThreadState);
  Thread.Sleep(1000);
}
like image 111
Mike Zboray Avatar answered Mar 19 '26 18:03

Mike Zboray



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!