c#
如果程序里面使用了多线程技术的话!就需要对子线程的异常做出特殊的处理!
据我所知,如果没有做特殊处理的话,好像子线程的异常不会抛给主线程,有时会直接在客户端抛出异常(这当然不是我们想要的),更夸张的是,有时直接把程序给强制关闭了!
在用户的角度上,就像按了一个关闭按钮一样!我今天就遇到这样的一个问题!
帮朋友做了一个工具,在本地运行,测试,一切都是那么的完美,没有任何问题.但一到客户机的时候,一运行到多线程的地方,就自动关闭软件了!在他看来就像按了关闭程序的按钮一样!
那我们应该解决这个问题.,如何捕捉这个子线程的异常呢!其实也很简单,只要几行代码即可!(主要是自己做下笔记,以后可以查)
在程序的Program类的Main方法里面添加如下代码
#if !DEBUG
Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
#endif
static void Application_ThreadException(object sender,ThreadExceptionEventArgs e)
{
MessageBox.Show(e.Exception.Message,"系统错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
static void UnhandledExceptionFunction(Object sender,UnhandledExceptionEventArgs args)
{
MessageBox.Show(((Exception)args.ExceptionObject).Message,MessageBoxIcon.Error);
}
其它主要处理子线程错误的就只有这两行
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledExceptionFunction);
以及UnhandledExceptionFunction方法
这样就可以捕捉到子线程的异常了!要不是以前自己遇过一次,都不知怎么回事呢!呵!
文章出处:飞诺网(www.firnow.com):http://dev.firnow.com/course/4_webprogram/asp.net/netjs/20100714/445480.html
vb.net
Public Sub FormMain_Load(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles MyBase.Load AddHandler AppDomain.CurrentDomain.UnhandledException,AddressOf MyHandler AddHandler Application.ThreadException,AddressOf MyThreadHandler End Sub ''Backup in case of crash application Private Sub MyHandler(ByVal sender As Object,ByVal args As UnhandledExceptionEventArgs) Dim e As Exception = DirectCast(args.ExceptionObject,Exception) If e.Message IsNot Nothing Then 'Globals.startBackup(System.Configuration.ConfigurationManager.AppSettings.Get("DatabaseName"),System.IO.Path.GetDirectoryName(System.Configuration.ConfigurationManager.AppSettings.Get("DatabaseName")) & "/" & System.IO.Path.GetFileNameWithoutExtension(System.Configuration.ConfigurationManager.AppSettings.Get("DatabaseName")) & ".bak") 'Delete the old filename 'Reopen the *.bak filename 'Rename it as *.mdb If System.IO.File.Exists(System.IO.Path.GetDirectoryName(System.Configuration.ConfigurationManager.AppSettings.Get("DatabaseName")) & "/" & System.IO.Path.GetFileNameWithoutExtension(System.Configuration.ConfigurationManager.AppSettings.Get("DatabaseName")) & ".bak") Then Globals.startBackup(System.IO.Path.GetDirectoryName(System.Configuration.ConfigurationManager.AppSettings.Get("DatabaseName")) & "/" & System.IO.Path.GetFileNameWithoutExtension(System.Configuration.ConfigurationManager.AppSettings.Get("DatabaseName")) & ".bak",System.Configuration.ConfigurationManager.AppSettings.Get("DatabaseName")) ' Save the app config settings now that user wants to open an existing model AppConfigSettings.UpdateAppSettings("DatabaseName",System.Configuration.ConfigurationManager.AppSettings.Get("DatabaseName")) AppConfigSettings.UpdateAppSettings("LocaleIdentifier",System.Threading.Thread.CurrentThread.CurrentUICulture.LCID) System.Configuration.ConfigurationManager.RefreshSection("appSettings") OdbcLib.EditSystemDSN("Microsoft Access Driver (*.mdb)","ALiSSv3.0","(*.mdb)",System.Configuration.ConfigurationManager.AppSettings.Get("DatabaseName")) 'Update the XML Settings internal config file MyAppSettings.UpdateXmlFile(System.Configuration.ConfigurationManager.AppSettings.Get("DatabaseName")) Else Globals.copyDatabase(System.Configuration.ConfigurationManager.AppSettings.Get("DatabaseName"),System.IO.Path.GetDirectoryName(System.Configuration.ConfigurationManager.AppSettings.Get("DatabaseName")) & "/" & System.IO.Path.GetFileNameWithoutExtension(System.Configuration.ConfigurationManager.AppSettings.Get("DatabaseName")) & ".bak") End If End If End Sub Private Sub MyThreadHandler(ByVal sender As Object,ByVal args As ThreadExceptionEventArgs) Dim e As Exception = DirectCast(args.Exception,System.IO.Path.GetDirectoryName(System.Configuration.ConfigurationManager.AppSettings.Get("DatabaseName")) & "/" & System.IO.Path.GetFileNameWithoutExtension(System.Configuration.ConfigurationManager.AppSettings.Get("DatabaseName")) & ".bak") 'Delete the old filename 'Reopen the *.bak filename 'Rename it as *.mdb If System.IO.File.Exists(System.IO.Path.GetDirectoryName(System.Configuration.ConfigurationManager.AppSettings.Get("DatabaseName")) & "/" & System.IO.Path.GetFileNameWithoutExtension(System.Configuration.ConfigurationManager.AppSettings.Get("DatabaseName")) & ".bak") Then Globals.copyDatabase(System.IO.Path.GetDirectoryName(System.Configuration.ConfigurationManager.AppSettings.Get("DatabaseName")) & "/" & System.IO.Path.GetFileNameWithoutExtension(System.Configuration.ConfigurationManager.AppSettings.Get("DatabaseName")) & ".bak",System.Configuration.ConfigurationManager.AppSettings.Get("DatabaseName")) 'Update the XML Settings internal config file MyAppSettings.UpdateXmlFile(System.Configuration.ConfigurationManager.AppSettings.Get("DatabaseName")) MessageBox.Show("Fatal Error.Application is shutting down.....Please restart ALiSS session","Fatal Error",MessageBoxIcon.Error) System.Diagnostics.Process.GetCurrentProcess.Kill() Else Globals.copyDatabase(System.Configuration.ConfigurationManager.AppSettings.Get("DatabaseName"),System.IO.Path.GetDirectoryName(System.Configuration.ConfigurationManager.AppSettings.Get("DatabaseName")) & "/" & System.IO.Path.GetFileNameWithoutExtension(System.Configuration.ConfigurationManager.AppSettings.Get("DatabaseName")) & ".bak") End If End If End Sub