class Program
{
private int x = 0;
static void Main(string[] args)
{
Program p = new Program();
int a, b;
ThreadPool.GetMaxThreads(out a, out b);
Console.WriteLine("{0} - {1}", a, b);
for (int y = 0; y < 20; y++)
{
WaitCallback cb = new WaitCallback(DoSomething);
ThreadPool.QueueUserWorkItem(cb, y);
}
}
public static void DoSomething(object state)
{
Console.WriteLine(state);
}
}
Most of the time it prints out 20 items. However, sometimes it only prints a few. Why is that? I come from a Java background and am wondering if I am making some bad assumptions about how .NET ThreadPools work.
The ThreadPool is OK, it doesn’t drop anything.
Most likely your program closes before the output is complete.
The ThreadPool uses background threads and they are simply aborted when the process exits.
So add something that waits, like a
Console.ReadLine()at the end of Main and see what happens.