本文介绍了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 occuring 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 occuring on this I just cant see how to deal with this problem. Any help is really appreciated!!

下面是clrstack的WinDbg中输出:

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.

您可以尝试注册与处理程序事件,做你的日志/在那里处理。请注意,这不会让你抑制或改变或包装异常,而只是某个记录它用于诊断目的。

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.

如果你只是想检查除了一次(或上一次),你应该尝试使用SOS扩展的!PrintException 在WinDbg中的命令。

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),并期待在反射EventLog类,你可以看到,整个处理 EventWrittenHandler ,包括似乎导致异常的前置码,被包裹在一个大的try / catch语句(例外)/ 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.

这篇关于C#中捕获的异常是在线程池存在的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 19:45