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

news/2024/7/5 4:12:17

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

http://www.niftyadmin.cn/n/4748240.html

相关文章

linux软件时间同步硬件时间,linux系统时间同步,硬件时钟和系统时间同步,时区的设置...

1、时间同步(手动):date -s "2015-07-15 22:13:30"hwclock --systohc (表示系统时间同步到硬件时钟)hwclock --hctosys (表示硬件时钟同步到系统时间)2、根据互联网时间同步:首先查看linux是否有ntp这个软件:rpm -qa | grep ntp如…

VirtualBox 4.0.12 安装Ubuntu 分配数据空间

今天在装VirtualBox的时候,提示有新的版本4.0.12可以使用了,按照我的一贯习惯,马上就下载了下来,进行了安装。装完之后就发生了一个悲剧,按照以前的办法安装完增强功能之后,接着挂载分配的数据空间&#xf…

linux7.0重启服务,linux centos安装nginx7.0 启动、重启、停止服务

一、安装准备首先由于nginx的一些模块依赖一些lib库,所以在安装nginx之前,必须先安装这些lib库,这些依赖库主要有g、gcc、openssl-devel、pcre-devel和zlib-devel 所以执行如下命令安装 #支持正则的pcre模块 比如安装 不然手动安装会报错yum …

用Ubuntu破解wep加密的wifi

1、 sudo apt-get install aircrack-ng 2、开启终端1, sudo airmon-ng start wlan0 sudo airodump -ng mon0 记住类型为web的设备的mac地址 3、开启终端2 sudo airodump-ng -c 频道 –bssid 目标主机mac -w wep mon0 这里的wep为默认的存包文件的名字,可…

happy 暑假

Happy 暑假 这个暑假过的真快,感觉自己就做了三件事,一是学生信息管理系统,二是机房收费系统,三是听英语。这三件事饱满的占据了我整个暑假学习… 我觉得这个暑假过的快的原因就是敲代码,以前看到一个简单的例子都头疼…

在linux开发板上显示图片,制作开发板的logo标签

如何制作开发板的logo标签:一,修改由kernel显示的开机logo:注意:QQ截图可以得到jpg格式图片,用windows的 开始->程序->附件->绘图->ACD SEE绘图工具打开,可以另存为png格式。1,下载…

Ubuntu下Fcitx输入法安装总结

Ubuntu 10.04自带的ibus拼音输入法不好用,思索着给他装个好用点的,上Ubuntu中文论坛,Fcitx输入法用的人比较多,决定就用Fcitx了。 按照Ubuntu 中文论坛的说明,打开终端,开始安装: 有安装ibus的…

老子的软件之道 - 道篇 1 软件的本源-抽象

摘要:软件哲学、软件之道、银弹、人狼、软件架构 参阅:序消灭人狼软件的十大命题编程规则 道篇 你发现构成软件体系最基本的粒子了吗? 这是通往软件之道的门户。(参见:软件架构形态) 1 软件的本源-抽象 圣人曰:道可道&#xff0…