我正在尝试使用以下C#语句来崩溃LINQPad4:
new Thread(() => new Thread(() => { throw new Exception(); }).Start()).Start();
Run Code Online (Sandbox Code Playgroud)
将显示未处理的异常对话框,但该过程不会消失.我想IsTerminating = true就像在所有UnhandledThreadExceptions中一样......它是如何阻止进程死亡的?
它有一个全局异常处理程序,用于所有非UI线程异常,类似于Main方法:
AppDomain.CurrentDomain.UnhandledException +=
new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
Run Code Online (Sandbox Code Playgroud)
当然还有其他一些小事要做,加上Application.Run的try/catch.
在这里查看完整的文章和详细信息:C#教程 - 处理未处理的异常
编辑: hb.尝试调试这个:;-)
using System;
using System.Threading;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
new Thread(() => new Thread(() => { throw new ApplicationException("Ciao"); }).Start()).Start();
try
{
Application.Run(new Form1());
}
catch (Exception exc)
{
System.Diagnostics.Debug.WriteLine(exc.Message);
}
}
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
// here catching Unhandled Exceptions
System.Diagnostics.Debug.WriteLine(e.ExceptionObject.ToString());
}
}
}
Run Code Online (Sandbox Code Playgroud)