How to understand Process.Threads.Count results? What is this variable?

Allows you to write a simple console application (debug mode):

static void Main(string[] args) { Process p = Process.GetCurrentProcess(); IList<Thread> threads = new List<Thread>(); Console.WriteLine(p.Threads.Count); for(int i=0;i<30;i++) { Thread t = new Thread(Test); Console.WriteLine("Before start: {0}", p.Threads.Count); t.Start(); Console.WriteLine("After start: {0}", p.Threads.Count); } Console.WriteLine(Process.GetCurrentProcess().Threads.Count); Console.ReadKey(); } static void Test() { for(int i=0;i<100;i++)Thread.Sleep(1); } 

What do you think you'll see in the results?

[Q1] Why is p.Threads.Count different from Process.GetCurrentProcess (). Threads.Count?

+4
source share
1 answer

You need to call Process.Refresh() before you get the Threads property every time so as not to see the caching results.

Do this and you will see the expected results.

+4
source

All Articles