Вопрос

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