开发者

Console ThreadPool not Executing

I'm struggling to understand why nothing is output using the following:

class P开发者_运维百科rogram
{
    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();
    }
}


Because your program terminates before it has any time to execute the threads. Adding a simple

Console.ReadLine();

at the end of the Main method should do just fine.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜