什么是Application.ThreadException和AppDomain.CurrentDomain.UnhandledException之间的区别?区别、ThreadException、Ap

2023-09-02 01:29:07 作者:海是深蓝色的坟墓

好吧,这是一个容易的:

Alright, this is an easy one:

有什么的区别 Application.ThreadException AppDomain.CurrentDomain。 UnhandledException

What's the difference between Application.ThreadException and AppDomain.CurrentDomain.UnhandledException?

我是否需要同时处理?

谢谢!

推荐答案

Application.ThreadException是特定于Windows窗体。的WinForms运行事件处理程序来响应发送给它的Windows消息。例如Click事件,我敢肯定,你认识他们。如果这样的事件处理程序抛出一个异常,然后有一个备用站,​​捕获该异常的WinForms消息循环中。

Application.ThreadException is specific to Windows Forms. Winforms runs event handlers in response to messages sent to it by Windows. The Click event for example, I'm sure you know them. If such an event handler throws an exception then there's a back-stop inside the Winforms message loop that catches that exception.

这逆止触发Application.ThreadException事件。如果不重写,用户将得到一个ThreadExceptionDialog.这让他忽略了的异常,并保持运行程序。不是一个好主意BTW。

That backstop fires the Application.ThreadException event. If you don't override it, the user will get a ThreadExceptionDialog. Which allows him to ignore the exception and keep running your program. Not a great idea btw.

您可以通过调用Application.SetUnhandledExceptionMode()在Program.cs中的Main()方法。如果没有这种逆止器的地方,平常的事情发生了,当一个线程未处理的异常死亡:AppDomain.UnhandledException火灾和程序将终止。

You can disable this behavior by calling Application.SetUnhandledExceptionMode() in the Main() method in Program.cs. Without that backstop in place, the usual thing happens when a thread dies from an unhandled exception: AppDomain.UnhandledException fires and the program terminates.

FWIW:ThreadException是一个非常可怜的名字的选择。它无关的线程。

Fwiw: "ThreadException" was a very poor name choice. It has nothing to do with threads.