How does SetUnhandledExceptionFilter work in .NET WinForms applications?

后端 未结 3 708
甜味超标
甜味超标 2020-12-08 23:48

I am working on a project to enhance our production debugging capabilities. Our goal is to reliably produce a minidump on any unhandled exception, whether the exception is

3条回答
  •  时光取名叫无心
    2020-12-09 00:18

    Windows Forms has a built-in exception handler that does the following by default:

    • Catches an unhandled managed exception when:
      • no debugger attached, and
      • exception occurs during window message processing, and
      • jitDebugging = false in App.Config.
    • Shows dialog to user and prevents app termination.

    You can disable the first behaviour by setting jitDebugging = true in App.Config. This means that your last chance to stop the app terminating is to catch the unhandled exception by registering for the event Application.ThreadException, e.g. in C#:

    Application.ThreadException += new Threading.ThreadExceptionHandler(CatchFormsExceptions);
    

    If you decide not to catch the unhandled exception here, then you will need to check and/or change the registry setting DbgJitDebugLaunchSetting under HKLM\Software.NetFramework. This has one of three values of which I'm aware:

    • 0: shows user dialog asking "debug or terminate".
    • 1: lets exception through for CLR to deal with.
    • 2: launches debugger specified in DbgManagedDebugger registry key.

    In Visual Studio, go to Tools>Options>Debugging>JIT to set this key to 0 or 2. But a value of 1 is usually what you want on an end-user's machine. Note that this registry key is acted on before the CLR unhandled exception event that you discuss.

    Then you can set the native exception filter that you discussed.

提交回复
热议问题