Duplicate: How to catch exceptions from a ThreadPool.QueueUserWorkItem?
I am queueing up multiple delegates on the .Net ThreadPool for a large number of independant remoting calls that themselves call multiple databases and other offline resources. By Queueing these calls on the ThreadPool I can run them concurrently and minimize the overall latency.
private void CompleteAndQueuePayLoads(IEnumerable<UsagePayload> payLoads)
{
List<WaitHandle> waitHndls = new List<WaitHandle>();
foreach (UsagePayload uPyLd in payLoads)
{
ManualResetEvent txEvnt = new ManualResetEvent(false);
UsagePayload uPyLd1 = uPyLd ;
ThreadPool.QueueUserWorkItem(
delegate
{
if (!uPyLd1 .IsComplete)
// IEEDAL.GetPayloadReadings is long running DB call
try { IEEDAL.GetPayloadReadings(uPyLd1 ); }
catch (IEEAccessException iX)
{
log.Write(log.Level.Error,
"IEEWSDAL.CompleteAndQueuePayLoads " +
" Delegate Failed " +
iX.Message, iX);
txEvnt.Set();
throw; // this causes parent thread to crash!
// was going to try Thread.Abort next ...
// Thread.CurrentThread.Abort();
}
UsageCache.PersistPayload(uPyLd1 );
SavePayLoadToProcessQueueFolder(uPyLd1 );
txEvnt.Set();
});
waitHndls.Add(txEvnt);
}
util.WaitAll(waitHndls.ToArray()); //To waitone on > 64 waithandles
}
But the entire batch needs to be handled transactionally, I.E., the output of the Parent Thread needs to only be allowed to proceed if all child threads were successful. I had coded the child thread to throw a custom exception when it failed, but I have discovered that this causes the parent thread to crash, as these exceptions cannot be "caught" in the parent thread...
I have read about the UnHandledExceptionEvent throw by the CLR when this happens, but I need to "handle" this exception in the method where these child threads are being Queued up and spawned, to control the immediate downstream processing based on the success of teh child threeads... How do I go about this?