幻鲨的天空

记下发生过的点点滴滴

« 碧眼方瞳是神仙暴雪:咱来算算开发《暗黑破坏神》要多少钱 »

C#控制台应用程序自动关闭

控制台应用程序,由于一些未捕获的异常,会导致程序崩溃,Windows会自动弹出“XXXX程序已停止工作”的对话框。为了避免这种情况发生

在应用程序入口处订阅UnhandledExceptionEventHandler事件。

代码如下:

AppDomain.CurrentDomain.UnhandledException +=new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); //;OnDomainUnhandledException;

 

捕获异常,并处理:

static void  CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
    ExceptionToMessageHelper.WriteLog(
string.Format("应用程序出现异常,请重新启动!异常情况如下:\r\n{0}", e.ExceptionObject.ToString()));
    SendMonitorMessage(
-40003011"应用程序出现异常,请重新启动!"); //发送消息到监控系统
}

 

  • 相关文章:

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

日历

最新留言

最近发表

Powered By Z-Blog 1.6 Final Build 60816

Copyright www.erabillow.com. Some Rights Reserved.