Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catching ThreadingException in VB.NET when Sub Main is not accessible

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)...

like image 956
serhio Avatar asked Nov 05 '22 01:11

serhio


1 Answers

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.

like image 92
HTTP 410 Avatar answered Nov 13 '22 01:11

HTTP 410