Any uncaught exception on a background thread will immediately terminate your application. This is by design.
The recommended technique for handling exceptions on threads created with ThreadPool.QueueUserWorkItem() is this:
Surround your code with try/catch. Do not allow exceptions to escape the callback.
using System.Threading;
namespace Zuga.net
{
class Program
{
static void Main(string[] args)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc), null);
// ...
}
private static void ThreadProc(object state)
{
try
{
// Your code
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
}
}
What happens without try/catch: