I have a VB.NET winforms solution, and would like to add the standard application exception handlers - Application.ThreadException
and AppDomain.CurrentDomain.UnhandledException
.
I have the following code 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
How can I do it in VB.NET when I have no acces to the Sub Main()
method?
Is the case when "Enable Application Framework" of my solution properties is enabled (Sub Main
is hidden)...
You can intercept the My.Application.Startup event to add code that needs to run before any forms are loaded.
Note that the code for the Startup event handler is stored in the ApplicationEvents.vb file, which is hidden by default.
EDIT: To answer your comment, there's a confusion between My.Application and System.Windows.Forms.Application. If you prefix .Application with System.Windows.Forms, it will work - I've just tested this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With