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

前端之家收集整理的这篇文章主要介绍了捕获子线程异常 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,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

  1. Public Sub FormMain_Load(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles MyBase.Load
  2.  
  3. AddHandler AppDomain.CurrentDomain.UnhandledException,AddressOf MyHandler
  4. AddHandler Application.ThreadException,AddressOf MyThreadHandler
  5.  
  6. End Sub
  7.  
  8.  
  9. ''Backup in case of crash application
  10. Private Sub MyHandler(ByVal sender As Object,ByVal args As UnhandledExceptionEventArgs)
  11. Dim e As Exception = DirectCast(args.ExceptionObject,Exception)
  12. If e.Message IsNot Nothing Then
  13. '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")
  14. 'Delete the old filename
  15. 'Reopen the *.bak filename
  16. 'Rename it as *.mdb
  17. 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
  18. 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"))
  19. ' Save the app config settings now that user wants to open an existing model
  20. AppConfigSettings.UpdateAppSettings("DatabaseName",System.Configuration.ConfigurationManager.AppSettings.Get("DatabaseName"))
  21. AppConfigSettings.UpdateAppSettings("LocaleIdentifier",System.Threading.Thread.CurrentThread.CurrentUICulture.LCID)
  22. System.Configuration.ConfigurationManager.RefreshSection("appSettings")
  23. OdbcLib.EditSystemDSN("Microsoft Access Driver (*.mdb)","ALiSSv3.0","(*.mdb)",System.Configuration.ConfigurationManager.AppSettings.Get("DatabaseName"))
  24. 'Update the XML Settings internal config file
  25. MyAppSettings.UpdateXmlFile(System.Configuration.ConfigurationManager.AppSettings.Get("DatabaseName"))
  26. Else
  27. 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")
  28. End If
  29. End If
  30. End Sub
  31. Private Sub MyThreadHandler(ByVal sender As Object,ByVal args As ThreadExceptionEventArgs)
  32. 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")
  33. 'Delete the old filename
  34. 'Reopen the *.bak filename
  35. 'Rename it as *.mdb
  36. 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
  37. 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"))
  38. 'Update the XML Settings internal config file
  39. MyAppSettings.UpdateXmlFile(System.Configuration.ConfigurationManager.AppSettings.Get("DatabaseName"))
  40. MessageBox.Show("Fatal Error.Application is shutting down.....Please restart ALiSS session","Fatal Error",MessageBoxIcon.Error)
  41. System.Diagnostics.Process.GetCurrentProcess.Kill()
  42. Else
  43. 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")
  44. End If
  45. End If
  46. End Sub

猜你在找的VB相关文章