.Net的线程池线程异常 [英] Exceptions on .Net ThreadPool Threads

查看:194
本文介绍了.Net的线程池线程异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

复制:如何捕捉从异常的ThreadPool.QueueUserWorkItem


我排队在.NET线程池的多个代表为大量独立的远程调用本身调用多个数据库和其他线下资源。通过排队的线程池这些电话,我可以同时运行他们,并最大限度地减少整体延迟。

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
}

但整批需要处理事务,IE浏览器,父线程的输出需要只被允许继续进行,如果所有的子线程成功。我有codeD时,它未能抛出一个自定义异常的子线程,但我已经发现,这将导致父线程崩溃,因为这些异常不能在父线程中招......

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...

我已阅读有关UnHandledExceptionEvent由CLR抛出的时候出现这种情况,但我需要在这些子线程正在排队,并催生了方法来处理这个异常,成功的基础上,以控制直接下游加工德子threeads的......我怎么去呢?

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?

推荐答案

我这样做了很多在我目前的项目。我采取的方法是使我自己的调度,是以委托,将其添加到自己的队列中,并运行他们处理在年底同步等。

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

绝招我在这种情况下使用是不打电话委托直接在线程池,而是叫我的调度方法,它包装的委托,更优雅地处理异常。这样一来,实际的,内部方法调用是在合适的处理一直(对我来说)的方式。

The "trick" I've used in this case is to not call the delegate directly on the thread pool, but rather call a method in my scheduler that wraps the delegate and handles the exception more gracefully. This way, the actual, internal method call is always handled in an appropriate (for me) manner.

当异常情况发生时,我的调度通知,停止安排今后的任务(这是一个不错的奖金),但也需要告诉来电者,有一个异常的照顾。

When an exception happens, my scheduler is notified and stops scheduling future tasks (which is a nice bonus), but also takes care of telling the caller that there was a single exception.

这篇关于.Net的线程池线程异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆