捕获应用程序崩溃事件
我在VB.Net中做了一个应用程序。但一些用户面临崩溃时启动。这是一个问题导致此程序正常工作只需一个按钮关闭程序。
I made a application in VB.Net. But some users face crash upon startup. That is "A problem caused this program from working correctly" with just one button "Close the program". Since there are lot of things happening when the app loads, is it possible to know what caused the issue?
如果应用程序加载时有很多事情发生,应用程序框架在项目的属性中启用,单击应用程序项目属性页上的查看应用程序事件按钮。然后添加一个事件处理程序:
If the "Application Framework" is enabled in your project's properties, click the "View Application Events" button on the "Application" project properties page. Then add an event handler:
Partial Friend Class MyApplication
Private Sub MyApplication_UnhandledException(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs) Handles Me.UnhandledException
' ...
End Sub
End Class
如果你不使用应用程序框架,你应该在你的整个 Main
方法之间放一个try catch块。但是,这将只捕获在主线程上发生的异常。如果你的应用程序是多线程的,你可以通过创建一个这样的方法处理所有线程的异常:
If you are not using the application framework, you should put a try catch block around your entire Main
method. However, that will only catch exceptions that occur on the primary thread. If your application is multi-threaded, you can handle exceptions from all threads by creating a method like this:
Public Sub UnhandledExceptionHandler(ByVal sender As Object, ByVal e As UnhandledExceptionEventArgs)
' ...
End Sub
然后将其附加到您当前域的 UnhandledException
事件,如下所示:
And then attach it to your current domain's UnhandledException
event, like this:
AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf UnhandledExceptionHandler
然后将调用事件处理程序所有未处理的异常来自您的域中的任何位置,而不管当前线程。
That event handler will then get called for all unhandled exceptions from anywhere in your domain, regardless of the current thread.