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.
精彩评论