让我们编写简单的控制台应用程序:
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);
}
如您所见,我们在进程中创建了 30 个等待线程。他们在哪里?MSDN:
使用 ProcessThread 获取与当前进程关联的所有线程。