让我们编写简单的控制台应用程序(调试模式):
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);
}
你认为你会在结果中看到什么?
[Q1] 为什么 p.Threads.Count 与 Process.GetCurrentProcess().Threads.Count 不同?