Here’s a followup post on the .NET ThreadPool bug that I described here:
Breaking Changes in the ThreadPool: The Movie
I have been in touch with the guys who are in charge of the ThreadPool and they have both confirmed that this is a bug and that they are planning on fixing it in .NET 2.0 SP2 – but they are not sure of the timeline for its release.
In the meantime, Vance Morrison, a .NET Runtime Performance Architect at Microsoft, has given me this work-around.
Take this “broken” code:
private static void UseThreadPool(int count)
{
for ( int i = 0; i < count; i++ )
{
ThreadPool.QueueUserWorkItem(
delegate { SlowMethod(); } );
}
}
And add a strategic Thread.Sleep and it’s fixed:
private static void UseThreadPool(int count)
{
for ( int i = 0; i < count; i++ )
{
ThreadPool.QueueUserWorkItem(
delegate { SlowMethod(); } );
Thread.Sleep(1);
}
}