Quote:
Here's how to get it done in VB.NET. First, create a class to intercept errors and call the ExceptionHandler.DisplayMessage() method.
Imports System
Imports System.ComponentModel
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Threading
Imports Microsoft.VisualBasic
'The Error Handler class
'We need a class because event handling methods can't be static
Friend Class CustomExceptionHandler
'Handle the exception event
Public Sub OnThreadException(ByVal sender As Object, ByVal t As ThreadExceptionEventArgs)
Dim result As DialogResult = DialogResult.Cancel
Try
OurExceptionHandler.DisplayMessage(t.Exception)
Catch
Try
MessageBox.Show("Fatal Error", "Fatal Error", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop)
Finally
Application.Exit()
End Try
End Try
End Sub
Then hook it in the Main this way:
Public Class Main
'The application's main entry point.
Public Shared Sub Main()
'Explicitly set apartment state to Single Thread Apartment (STA)
'System.Threading.Thread.CurrentThread.ApartmentState = System.Threading.ApartmentState.STA
Dim eh As New CustomExceptionHandler
AddHandler Application.ThreadException, AddressOf eh.OnThreadException
Application.Run(New ErrorHandler)
End Sub
End Class
I hope some of you find this simple, yet elegant, solution as schweet as I did.
this actually made for a good read :)