Exceptions to .Net ThreadPool Threads

Duplicate: How to catch exceptions from ThreadPool.QueueUserWorkItem?




I host several delegates in .Net ThreadPool for a large number of independent remote calls, which are themselves called several databases and other autonomous resources. By queuing these calls in ThreadPool, I can run them at the same time and minimize the overall delay.

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 whole batch should be processed transactionally, IE, the output of the parent thread should be allowed only if all child threads were successful. I coded the child thread to throw a custom exception when it failed, but I found that this causes the parent thread to crash because these exceptions cannot be "caught" in the parent thread ...

I read about the UnHandledExceptionEvent CLR error when this happens, but I need to "handle" this exception in a method in which these child threads are queued and thrown to control the immediate subsequent processing based on success from child triads ... How do I get this to do?

+4
exception threadpool unhandled-exception
Mar 20 '09 at 22:43
source share
3 answers

I am doing this a lot in my current project. The approach I took was to create my own scheduler that accepts the delegate, adds it to my own queue and runs them to handle synchronization at the end, etc.

The β€œtrick” I used in this case is not to call the delegate directly in the thread pool, but rather to call the method in my scheduler, which wraps the delegate and handles the exception more gracefully. Thus, the call to the internal method is always handled accordingly (for me).

When an exception occurs, my scheduler is notified and stops planning future tasks (which is a nice bonus), but also takes care to inform the caller that there is one exception.

+2
Mar 21 '09 at 1:04
source share

If the stream fails, you can note that at least one failure occurred inside the local variable of the CompleteAndQueuePayLoads function and adds an exception / failure variable, which will be discussed later.

+3
Mar 20 '09 at 22:58
source share

You can learn parallel library extensions for 3.5. You can use Parallel.Invoke in your code block to do exactly what you are trying to do.

+1
Oct 29 '09 at 5:31
source share



All Articles