控制台ThreadPool不执行

Jer*_*xon 0 c# multithreading

我正在努力理解为什么没有输出使用以下内容:

class Program
{
    static int m_Active = 500;
    static void Main(string[] args)
    {
        ThreadPool.SetMaxThreads(5, 5);
        Enumerable.Range(1, m_Active).ToList<int>()
            .ForEach(i => ThreadPool.QueueUserWorkItem((o) => { DoWork(i); }));
    }

    private static void DoWork(int i)
    {
        new Action(() => { Console.WriteLine(i); }).Invoke();
        if (Interlocked.Decrement(ref m_Active).Equals(0))
            new Action(() => { Console.WriteLine("Done"); }).Invoke();
    }
}
Run Code Online (Sandbox Code Playgroud)

Dar*_*rov 7

因为您的程序在有任何时间执行线程之前终止.添加简单

Console.ReadLine();
Run Code Online (Sandbox Code Playgroud)

Main方法的最后应该做的很好.