ThreadPool上发生的C#捕获异常 [英] C# Catching exception which is occurring on ThreadPool

查看:319
本文介绍了ThreadPool上发生的C#捕获异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在调查由Win32异常引起的应用程序崩溃,并且我将其范围缩小为必须在正在处理应用程序中EventLog.EntryWrittenEventHandler事件处理程序的线程池中发生.我是这样设置的:

I am investigating some crashes in my application caused by a Win32 exception, and I have narrowed it down that it must be occurring in the threadpool which is taking care of the EventLog.EntryWrittenEventHandler event handler in my application. I set this up like this:

// Create the event log monitor
eventLog.Log = "Application";
eventLog.EnableRaisingEvents = true;
eventLog.EntryWritten += new EntryWrittenEventHandler(EventLogMonitor);

EventLogMonitor是我的事件的处理程序.我想知道是否有人对我在哪里可以找到导致此异常的原因有任何想法.似乎正在侦听正在设置ThreadPoolWaitOrTimerCallback的事件,该事件上没有任何我的代码,并且如果发生此异常,我只是看不到如何处理此问题.任何帮助都非常感谢!

EventLogMonitor is the handler for my event. I am wondering does anybody have any ideas as to where I could find out whats causing this exception. It seems that to listen for events a ThreadPoolWaitOrTimerCallback is being set up, which wouldn't have any of my code on it, and if the exception is occurring on this I just cant see how to deal with this problem. Any help is really appreciated!!

这是WinDBG中!clrstack的输出:

Here is the output of !clrstack in WinDBG:

0:008> !clrstack
OS Thread Id: 0x106c (8)
ESP       EIP     
049df1c8 7756f871 [HelperMethodFrame: 049df1c8] 
049df26c 73ce6fa0 System.Diagnostics.EventLog.get_OldestEntryNumber()
049df27c 73bf24ed System.Diagnostics.EventLog.CompletionCallback(System.Object)
049df2c4 73bf0fe4 System.Diagnostics.EventLog.StaticCompletionCallback(System.Object, Boolean)
049df2f4 744fc3b8 System.Threading._ThreadPoolWaitOrTimerCallback.WaitOrTimerCallback_Context(System.Object, Boolean)
049df300 744fc373 System.Threading._ThreadPoolWaitOrTimerCallback.WaitOrTimerCallback_Context_f(System.Object)
049df304 7400027f System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
049df31c 744fc477 System.Threading._ThreadPoolWaitOrTimerCallback.PerformWaitOrTimerCallback(System.Object, Boolean)
049df4ac 74991b5c [GCFrame: 049df4ac] 

如果有帮助,我的应用程序将只检查写入事件日志的每个条目的事件ID,如果它与某个ID集合中的一个匹配,则将其记录下来.崩溃很少在安静的地方发生,并且异常是带有消息访问被拒绝"的System.ComponentModel.Win32异常.这听起来像是一个权限问题,但是为什么它在一段时间内仍然可以正常运行,然后突然与此崩溃.

In case it helps, my application is just checking the event ID of every entry written to the event log, and if it matches one of a certain set of ID's then I log it. The crashes happen quiet rarely, and the exception is a System.ComponentModel.Win32 exception with message 'Access is denied'. That sounds like it could be a permissions issue but why would it work ok for a certain period and then suddenly crash with this.

推荐答案

如果我对您的理解正确(如果传递通过堆栈跟踪使您得出的结论是线程池线程中发生了异常,这会有所帮助),将您的EventLogMonitor代码包装在try/catch块中.

If I understand you correctly (it would help if you pass the stacktrace that leads you to the conclusion that the exception is happening inside a threadpool thread), then just wrap your code of EventLogMonitor in a try/catch block.

示例:

void EventLogHandler(object sender, EventArgs args)
{
   try
   {
      // Your original code.
   }
   catch (Exception ex)
   {
      // Log or Write "ex" to the console. Set a breakpoint, whatever.

      throw;
   }
}

更新:更新之后,似乎确实不是从处理程序内部引发异常,而是在EventLog类内部甚至调用了该异常.

UPDATE: after your update it looks as if the exception is indeed not raised from inside your handler, but before it is even called inside the EventLog class.

您可以尝试使用 AppDomain.UnhandledException 事件,并在那里进行日志记录/处理.请注意,这将不允许您抑制或更改"或包装异常,而只是将其记录在某处以进行诊断.

You could try registering a handler with the AppDomain.UnhandledException event and do your logging/handling in there. Note that this will not allow you to suppress or "change" or wrap the exception, but merely to log it somewhere for diagnostic purposes.

如果只想一次(或偶尔)检查异常,则应尝试在WinDBG中使用SOS扩展的!PrintException命令.

If you just want to inspect the exception once (or on occasion), you should try using the SOS-extension's !PrintException command in WinDBG.

更新2 :经过进一步调查,我发现异常使所有异常冒出来很奇怪.您的堆栈跟踪建议您使用的是.NET 3.5(或更早版本,但不是4),然后查看Reflector中的EventLog类,您会发现EventWrittenHandler的整个处理过程,包括可能导致异常的前导代码,包装在一个大的"try/catch(Exception)/catch"块中.好笑.

UPDATE 2: after further investigation I find it rather strange that the exception bubbles up all. Your stacktrace suggests you're using .NET 3.5 (or earlier, but not 4.) and looking at the EventLog class in Reflector you can see that the whole handling of the EventWrittenHandler, including the preamble code that seems to cause the exception, is wrapped in one big "try/catch(Exception)/catch" block. Funny.

这篇关于ThreadPool上发生的C#捕获异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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