本文参考网上搜索的信息,并作了适当修改可让捕捉到异常以后阻止程序退出。html
另给出了经过命令行自动重启的方法。spring
若是一个线程里运行下面的代码app
int a = 0; int c = 10 / a;
将会致使程序自动结束,并且没有任何提示信息 可是若是是在主线程里运行这个代码,是会弹出异常信息对话框的post
请问如何在线程里也出现这个异常信息对话框.或者
避免程序直接退出,忽略异常,继续往下执行呢?
在WINFORM主线程捕获所有异常就行,以下代码:
//处理未捕获的异常 Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); //处理UI线程异常 Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException); //处理非UI线程异常 AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
最常出现的错误在 :UnhandledException 里出现。详细代码以下:
/// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main(string[] args) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); //处理未捕获的异常 Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); //处理UI线程异常 Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException); //处理非UI线程异常 AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); Application.Run(new Form1(args)); glExitApp = true;//标志应用程序能够退出 } /// <summary> /// 是否退出应用程序 /// </summary> static bool glExitApp = false; static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { LogHelper.Save("CurrentDomain_UnhandledException", LogType.Error); LogHelper.Save("IsTerminating : " + e.IsTerminating.ToString(), LogType.Error); LogHelper.Save(e.ExceptionObject.ToString()); while (true) {//循环处理,不然应用程序将会退出 if (glExitApp) {//标志应用程序能够退出,不然程序退出后,进程仍然在运行 LogHelper.Save("ExitApp"); return; } System.Threading.Thread.Sleep(2*1000); }; } static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e) { LogHelper.Save("Application_ThreadException:" + e.Exception.Message, LogType.Error); LogHelper.Save(e.Exception); //throw new NotImplementedException(); }
若是程序须要重启只须要在捕获的事件处理时启动当前应用程序的代码便可。参考以下:this
CmdStartCTIProc(Application.ExecutablePath, "cmd params");//放到捕获事件的处理代码后,重启程序,须要时加上重启的参数 /// <summary> /// 在命令行窗口中执行 /// </summary> /// <param name="sExePath"></param> /// <param name="sArguments"></param> static void CmdStartCTIProc(string sExePath, string sArguments) { Process p = new Process(); p.StartInfo.FileName = "cmd.exe"; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardError = true; p.StartInfo.CreateNoWindow = false; p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; p.Start(); p.StandardInput.WriteLine(sExePath + " " + sArguments); p.StandardInput.WriteLine("exit"); p.Close(); System.Threading.Thread.Sleep(2000);//必须等待,不然重启的程序还未启动完成;根据状况调整等待时间 }
另一种重启进程的方式:spa
//重启程序,须要时加上重启的参数 System.Diagnostics.ProcessStartInfo cp = new System.Diagnostics.ProcessStartInfo(); cp.FileName = Application.ExecutablePath; cp.Arguments = "cmd params"; cp.UseShellExecute = true; System.Diagnostics.Process.Start(cp);
看了以为有用的朋友,若是您方便的话,能够顶一下。谢谢!命令行
程序崩溃后重启,能够试试下面的方式:线程
/// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.ThreadException+=new System.Threading.ThreadExceptionEventHandler(Application_ThreadException); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e) { Application.Restart(); }