开发者

Console ThreadPool not Executing

开发者 https://www.devze.com 2023-01-08 15:23 出处:网络
I\'m struggling to understand why nothing is output using the following: class P开发者_运维百科rogram

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

精彩评论

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