2011-03-14 40 views
3

我有一个VB.NET winforms解决方案,并且想要添加标准应用程序异常处理程序 - Application.ThreadExceptionAppDomain.CurrentDomain.UnhandledException当Sub Main不可访问时,在VB.NET中捕获ThreadingException

我有以下代码from MSDN

' Starts the application. ' 
<SecurityPermission(SecurityAction.Demand, Flags:=SecurityPermissionFlag.ControlAppDomain)> _ 
Public Shared Sub Main() 
    ' Add the event handler for handling UI thread exceptions to the event. ' 
    AddHandler Application.ThreadException, AddressOf ErrorHandlerForm.Form1_UIThreadException 

    ' Set the unhandled exception mode to force all Windows Forms errors to go through' 
    ' our handler. ' 
    Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException) 

    ' Add the event handler for handling non-UI thread exceptions to the event. ' 
    AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf CurrentDomain_UnhandledException 

    ' Runs the application. ' 
    Application.Run(New ErrorHandlerForm()) 
End Sub 

我怎样才能做到这一点在VB.NET的时候我没有存取权限的Sub Main()方法?

是当“启用应用程序框架”我的解决方案属性中启用的

+0

将前三行代码复制到您可以访问的第一个方法调用中。这里会出现一个小应急窗口,但你赢了一些,你输了一些。 – 2011-03-14 14:58:33

+0

@Mr。失望:看到与RoadWarrior波纹管相同的评论... – serhio 2011-03-14 15:33:59

+0

我明白了......看到我的答案。 – 2011-03-14 15:41:57

回答

1

可以拦截My.Application.Startup event添加需要加载任何形式之前运行代码的情况下(Sub Main是隐藏的)...。

请注意,启动事件处理程序的代码存储在默认情况下隐藏的ApplicationEvents.vb文件中。

编辑:为了回答您的评论,My.Application和System.Windows.Forms.Application之间存在混淆。如果你使用System.Windows.Forms的前缀.Application,它会工作 - 我刚刚测试过。

+0

'Application.ThreadException'在该上下文中不存在。 – serhio 2011-03-14 15:29:54

+0

meybe for unhandled有需要处理me.UnhandledException ...但不能确定它是否也为ThreadException ... – serhio 2011-03-14 15:54:09

+0

也许对于未处理有需要处理me.UnhandledException ...但不知道它是否为ThreadException ... – serhio 2011-03-14 15:55:18

1

我看到你的问题,我想 - 你好像在代码文件的头部缺少一个Imports声明。您可以添加所需Import或完全限定您所访问的类型:

Imports System 
Imports System.Windows.Forms 

Public Shared Sub MyApplicationInitialization() 
    AddHandler System.Windows.Forms.Application.ThreadException, AddressOf MyThreadExceptionHandler 

    Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException) 

    AddHandler System.AppDomain.CurrentDomain.UnhandledException, AddressOf MyUnhandledExceptionHandler 
End Sub 

正如你由此可以看出,在System.Windows.Forms命名空间内的System命名空间和ApplicationAppDomain生活。

请注意,您还需要在每个AddressOf之后定义自己的事件处理方法。这些可以如下布置:

Sub MyUnhandledExceptionHandler(ByVal sender As Object, ByVal e As UnhandledExceptionEventArgs) 
    'your logic here 
End Sub 

Sub MyThreadExceptionHandler(ByVal sender As Object, ByVal e As ThreadExceptionEventArgs) 
    'your logic here 
End Sub 
+0

如果使用显式名称空间,则不需要使用导入。这真的很简单,工作。从另一部分,甚至我不需要这样做,因为在ApplicationEvents.vb中有可能执行'Private Sub Application_UnhandledException(ByVal sender As Object,ByVal e As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs)处理我。 UnhandledException',但我不确定它是否也对threadExceptions也是这样,或者对于未处理的... – serhio 2011-03-14 15:50:56

+0

Ya,第一行解释了有两个选项,完全限定类型名称或使用导入。至于你的处理程序,它们的定义在功能方面是无关紧要的,只要它们可以正常访问。 – 2011-03-14 15:57:00

+0

我认为有一件事是使用'AddHandler System.AppDomain.CurrentDomain.UnhandledException',另一个是使用'处理Me.UnhandledException',因为'Me'和'System.AppDomain。CurrentDomain'是不同的东西,我相信,你也回答不会揭示,你应该在哪里使用... – serhio 2011-03-14 17:20:05