How to use ThreadException?

非 Y 不嫁゛ 提交于 2019-11-30 16:05:03
Hans Passant

Your code works fine, there are not a lot of possible failure modes I can think of. Except one, there's a problem in the interaction between the debugger and Windows SEH when you debug 32-bit code on a 64-bit operating system prior to Windows 8. This can cause exceptions to be swallowed without any diagnostic when they occur in the form's Load event or OnLoad() method override. Check the linked post for workarounds, the simplest one is Project + Properties, Build tab, Platform Target = AnyCPU, untick "Prefer 32-bit" if you see it.

In general, you are doing the appropriate thing by not letting the default exception handling of an Application.ThreadException display the dialog. But keep it simple, do it like this:

#if (!DEBUG)
      Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException);
#endif

Now you never have to worry about a ThreadException anymore, all exceptions trigger the AppDomain.UnhandledException event handler. And the #if around the code still lets you debug an unhandled exception, the debugger will automatically stop when the exception is raised.

Add this to the UnhandledException method to prevent the Windows crash notification from showing up:

        Environment.Exit(1);

You haven't show where you're throwing the ArgumentNullException. My guess is that it's either within the constructor of MediaPlayerForm (which means it's before the message loop has started) or it's in a different thread. Application.ThreadException only handles exceptions caught on Windows Forms threads running message loops.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!