Lets write simple console application:
static void Main(string[] args)
{
IList<Thread> threads = new List<Thread>();
Console.WriteLine((Process.GetCurrentProcess().Threads).OfType<ProcessThread>().Where(thread => thread.ThreadState == ThreadState.Wait).Count());
for(int i=0;i<30;i++)
{
Thread t = new Thread(Test);
Console.WriteLine("Before start: {0}", (Process.GetCurrentProcess().Threads).OfType<ProcessThread>().Where(thread => thread.ThreadState == ThreadState.Wait).Count());
t.Start();
Console.WriteLine("After start: {0}", (Process.GetCurrentProcess().Threads).OfType<ProcessThread>().Where(thread => thread.ThreadState == ThreadState.Wait).Count());
}
Console.WriteLine((Process.GetCurrentProcess().Threads).OfType<ProcessThread>().Where(thread => thread.ThreadState == ThreadState.Wait).Count());
Console.ReadKey();
}
static void Test()
{
Thread.Sleep(0);
}
As you see we created 30 waiting threads in our process. Where they are? MSDN:
Use ProcessThread to get all the threads associated with the current process.
Your threads exit nearly immediately. Sleep(0) isn’t a sleep forever, it’s just a yield. If there are no other runnable threads, the call to Sleep(0) just returns without yielding the timeslice. Your threads exit immediately after that call to Sleep.
Do a longer Sleep() to test this.