捕获子线程异常 vb.net c#

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, "系统错误", MessageBoxButtons.OK, 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, 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.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"))
' 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"))
MessageBox.Show("Fatal Error.Application is shutting down.....Please restart ALiSS session", "Fatal Error", MessageBoxButtons.OK, 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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值