Catch UnhandledException when Application Framework is off

StewartBW 745 Reputation points
2024-06-18T05:17:20.2766667+00:00

Hello,

When I disable Application Framework:

AddHandler My.Application.UnhandledException, AddressOf MyUnhandledException

Becomes invalid:

UnhandledException is not an event of blah.My.MyApplication

Using VB.net WinForms .NET FW 4.0 app, anyone can give me a tip please?

Thanks in advance :)

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,642 questions
0 comments No comments
{count} votes

Accepted answer
  1. KOZ6.0 6,300 Reputation points
    2024-06-19T09:57:55.71+00:00

    Another way is to implement your own application framework. All you have to do is create a MyApplication class that inherits from the WindowsFormsApplicationBase class.

    Imports Microsoft.VisualBasic.ApplicationServices
    
    Module Module1
    
        Sub Main(args As String())
            Application.SetCompatibleTextRenderingDefault(False)
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException)
            Dim app As New MyApplication()
            AddHandler app.UnhandledException, AddressOf UnhandledExceptionEventHandler
            app.Run(args)
        End Sub
    
        Sub UnhandledExceptionEventHandler(sender As Object, e As UnhandledExceptionEventArgs)
            e.ExitApplication = False
        End Sub
    
        Class MyApplication
            Inherits WindowsFormsApplicationBase
    
            Public Sub New()
                MyBase.New()
                IsSingleInstance = False
                EnableVisualStyles = True
                SaveMySettingsOnExit = True
                ShutdownStyle = ShutdownMode.AfterMainFormCloses
            End Sub
    
            Private Shared _Form1 As Form1
    
            Public Shared Property Form1 As Form1
                Get
                    If _Form1 Is Nothing Then
                        _Form1 = New Form1()
                    End If
                    Return _Form1
                End Get
                Set(value As Form1)
                    _Form1 = value
                End Set
            End Property
    
            Protected Overrides Sub OnCreateMainForm()
                MainForm = Form1
            End Sub
    
        End Class
    
    End Module
    
    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. KOZ6.0 6,300 Reputation points
    2024-06-18T07:43:22.11+00:00