我刚刚玩过AppDomain的UnhandledException行为,(这是未处理的异常注册到的最后阶段)
是的,处理完事件处理程序后,您的应用程序将终止,并显示讨厌的“ …程序停止工作对话框”。
:)您 仍然 可以避免这种情况。
查看:
class Program{ void Run() { AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); Console.WriteLine("Press enter to exit."); do { (new Thread(delegate() { throw new ArgumentException("ha-ha"); })).Start(); } while (Console.ReadLine().Trim().ToLowerInvariant() == "x"); Console.WriteLine("last good-bye"); } int r = 0; void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { Interlocked.Increment(ref r); Console.WriteLine("handled. {0}", r); Console.WriteLine("Terminating " + e.IsTerminating.ToString()); Thread.CurrentThread.IsBackground = true; Thread.CurrentThread.Name = "Dead thread"; while (true) Thread.Sleep(TimeSpan.FromHours(1)); //Process.GetCurrentProcess().Kill(); } static void Main(string[] args) { Console.WriteLine("..."); (new Program()).Run(); }}
PS请
在更高级别处理未处理的Application.ThreadException(WinForms)或DispatcherUnhandledException(WPF)。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)