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?

有帮助吗?

解决方案

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);
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top