捕获子线程异常 vb.net c#
c# 如果程序里面使用了多线程技术的话!就需要对子线程的异常做出特殊的处理! 帮朋友做了一个工具,在本地运行,测试,一切都是那么的完美,没有任何问题.但一到客户机的时候,一运行到多线程的地方,就自动关闭软件了!在他看来就像按了关闭程序的按钮一样! 那我们应该解决这个问题.,如何捕捉这个子线程的异常呢!其实也很简单,只要几行代码即可!(主要是自己做下笔记,以后可以查)
同时再添加两个方法处理异常信息! static void Application_ThreadException(object sender,ThreadExceptionEventArgs e) static void UnhandledExceptionFunction(Object sender,UnhandledExceptionEventArgs args) 其它主要处理子线程错误的就只有这两行 这样就可以捕捉到子线程的异常了!要不是以前自己遇过一次,都不知怎么回事呢!呵! 文章出处:飞诺网(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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |