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

捕获子线程异常 vb.net c#,第1张

概述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

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 MyThreadHandlerEnd Sub''Backup in case of crash applicationPrivate 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 *.mdbIf 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") ThenGlobals.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 modelAppConfigSettings.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 fileMyAppSettings.UpdateXmlfile(System.Configuration.ConfigurationManager.AppSettings.Get("Databasename"))ElseGlobals.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 IfEnd SubPrivate 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 *.mdbIf 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") ThenGlobals.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 fileMyAppSettings.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()ElseGlobals.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 IfEnd IfEnd Sub
总结

以上是内存溢出为你收集整理的捕获子线程异常 vb.net c#全部内容,希望文章能够帮你解决捕获子线程异常 vb.net c#所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/langs/1294120.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-10
下一篇2022-06-10

发表评论

登录后才能评论

评论列表(0条)

    保存