I have to support a desktop application that had been implemented by another developer. The application has some 'magic' code, and TPL is not working after it is called.
I think that someone blocked ThreadPool from working but I have no idea how to find this.
The question is: What going on in VeryLargeBlackBoxMethod()
method? How do I find out what the the wrong code is?
[STAThread]
public static void Main()
{
var testTimer = new System.Threading.Timer((s) => { Console.WriteLine("This code never run"); });
testTimer.Change(TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(1));
PrintAvailableThreads();
// ThreadPool can`t provide a new Task after this method executing
VeryLargeBlackBoxMethod();
PrintAvailableThreads();
Task.Factory.StartNew(() => { Console.WriteLine("This code never run too"); });
ThreadPool.QueueUserWorkItem((p) => { Console.WriteLine("And this code newer run"); });
var thread = new Thread(() => { Console.WriteLine("But this code work"); });
thread.Start();
Console.ReadLine();
}
private static void PrintAvailableThreads()
{
var work = 0;
var completionThreads = 0;
ThreadPool.GetAvailableThreads(out work, out completionThreads);
Console.WriteLine("worker threads {0}, completionThreads {1}", work, completionThreads);
}
// Console output:
// worker threads 32767, completionThreads 1000
// worker threads 32762, completionThreads 1000
// But this code work