ser*_*hio 5 .net vb.net exception-handling
我有一个 VB.NET winforms 解决方案,并想添加标准的应用程序异常处理程序 -Application.ThreadException和AppDomain.CurrentDomain.UnhandledException.
我有以下来自 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
Run Code Online (Sandbox Code Playgroud)
当我无法访问该Sub Main()方法时,如何在 VB.NET 中执行此操作?
是否启用了我的解决方案属性的“启用应用程序框架”(Sub Main隐藏)...
您可以拦截My.Application.Startup 事件以添加在加载任何表单之前需要运行的代码。
请注意,启动事件处理程序的代码存储在 ApplicationEvents.vb 文件中,默认情况下该文件是隐藏的。
编辑:为了回答您的评论,My.Application 和 System.Windows.Forms.Application 之间存在混淆。如果您在 .Application 前添加 System.Windows.Forms 前缀,它将起作用 - 我刚刚对此进行了测试。