一、在program.cs处完善成如下,但是这样后只能抛出主线程(UI)的错误,所以请看第二步
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
//全局异常捕捉
Application.ThreadException += Application_ThreadException; //UI线程异常
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; //多线程异常
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new FrmMain());
}
//UI线程异常
static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
WinformException.FrmBugReport.ShowBug(e.Exception);
}
//多线程异常
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
WinformException.FrmBugReport.ShowBug((Exception)e.ExceptionObject);
}
二、将其他线程异常抛掷到主线程
//
Thread t = new Thread((ThreadStart)delegate
{
try
{
throw new Exception("非窗体线程异常");
}
catch (Exception ex)
{
this.BeginInvoke((Action)delegate
{
throw ex;
});
}
});
t.Start();