Application.ThreadException和AppDomain.CurrentDomain.UnhandledException事件 [英] Application.ThreadException and AppDomain.CurrentDomain.UnhandledException events

查看:57
本文介绍了Application.ThreadException和AppDomain.CurrentDomain.UnhandledException事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


您好

Hi

我的代码如下所示:

static void Main( string [] Args)

static void Main(string[] Args)

{

//创建将处理异常的方法实例。

// Creates an instance of the methods that will handle the exception.

CustomExceptionHandler eh = new CustomExceptionHandler ();

CustomExceptionHandler eh = new CustomExceptionHandler();

//将事件处理程序添加到事件中。

// Adds the event handler to to the event.

应用 .ThreadException + = < font color ="#0000ff"size = 2> new ThreadExceptionEventHandler (eh.OnThreadException);

Application.ThreadException += new ThreadExceptionEventHandler(eh.OnThreadException);

//设置未处理的异常模式以强制所有Windows窗体错误通过

// Set the unhandled exception mode to force all Windows Forms errors to go through

//我们的处理程序。

// our handler.

应用程序 .SetUnhandledExceptionMode( UnhandledExceptionMode .CatchException);

Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

//添加用于处理事件的非UI线程异常的事件处理程序。

// Add the event handler for handling non-UI thread exceptions to the event.

AppDomain .CurrentDomain .UnhandledException + =

AppDomain.CurrentDomain.UnhandledException +=

new UnhandledExceptionEventHandler (eh.CurrentDomain_UnhandledException);

new UnhandledExceptionEventHandler(eh.CurrentDomain_UnhandledException);

表格 frmMain = new FormMain ();

Form frmMain = new FormMain();

eh.frmMain = frmMain;

eh.frmMain = frmMain;

申请 .Run(frmMain);

Application.Run(frmMain);

}

内部 class CustomExceptionHandler

internal class CustomExceptionHandler

{

public 表格 frmMain;

public Form frmMain;

//处理异常事件。

// Handles the exception event.

public void OnThreadException( object sender, ThreadExceptionEventArgs t)

public void OnThreadException(object sender, ThreadExceptionEventArgs t)

{

}

public void CurrentDomain_UnhandledException( object sender, UnhandledExceptionEventArgs e)

public void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)

{

}

}

据我所知,Application.ThreadException应该捕获所有主应用程序线程的异常,但是在Form Main初始化之后,Application.ThreadException

As far as i understand, the Application.ThreadException is supposed to catch all the exceptions of the main application thread, however after the Form Main is initializes, none of the exceptions are caught by the Application.ThreadException

也没有捕获任何异常,如果Application.ThreadException没有捕获异常i本来可以预料到AppDomain事件会捕获它们,但是只有少数异常会进入appdomain事件,而其他许多事件只会停留在ide。

also, if the Application.ThreadException doesnt catch the exceptions i would have expected that the AppDomain event would catch them however only few of the exceptions get to the appdomain event whereas many other just stuck the ide.

我正在使用.net 2005

i am using .net 2005

我做错了什么?

推荐答案

< FONT face ="Verdana,Geneva,Arial,Sans-serif">嗨Ayala24。

ThreadException的文档实际上非常具有误导性。 很容易推断,此事件实际上只是针对后台线程中的异常而引发的。 情况并非如此,当应用程序被正确配置时,引发ThreadException,仅针对处理Windows消息时抛出的异常(在单线程WinForms应用程序中,这是针对主窗体之后发生的任何未处理异常)显示)。


这篇关于Application.ThreadException和AppDomain.CurrentDomain.UnhandledException事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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