>>>Friend Class AppMgr >>> <STAThread()> _ >>> Shared Sub Main() >>> ''The 2 event handlers >>> ''add an unhandled exceptions handler >>> Dim currentDomain As AppDomain = AppDomain.CurrentDomain >>> 'for regular unhandled stuff >>> AddHandler currentDomain.UnhandledException, AddressOf MYExceptionHandler >>> 'for threads behind forms >>> AddHandler Application.ThreadException, AddressOf MYThreadHandler >>> >>> Dim frm1 As Form >>> frm1 = New DocProcess_Main() >>> Application.Run(frm1) >>> End Sub >>>#Region "Exeption Handler" >>> Shared Sub MYExceptionHandler(ByVal sender As Object, ByVal e As UnhandledExceptionEventArgs) >>> Dim EX As Exception >>> EX = e.ExceptionObject >>> 'Console.WriteLine(EX.StackTrace) >>> DisplayError(EX) >>> End Sub >>> Shared Sub MYThreadHandler(ByVal sender As Object, ByVal e As Threading.ThreadExceptionEventArgs) >>> 'Console.WriteLine(e.Exception.StackTrace) >>> DisplayError(e.Exception) >>> End Sub >>> Shared Sub DisplayError(ByVal ex As Exception) >>> Dim errorMessage As String = ("Unhandled Exception:" + vbLf + vbLf + ex.Message + vbLf + vbLf + ex.GetType.ToString + vbLf + vbLf + "Stack Trace:" + vbLf) + ex.StackTrace >>> MessageBox.Show(errorMessage, "DMS Application Error", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.[Stop]) >>> MessageBox.Show(ex.ToString, "DMS Application Error", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.[Stop]) >>> End Sub >>>#End Region >>>End Class >>> >>>Public Class DocProcess_Main >>> Inherits System.Windows.Forms.Form >>> ... >>> ... >>>End Class>>>The error I currently get :
Imports System.Windows.Forms Namespace XXX Partial Friend Class MyApplication Private Sub MyApplication_Startup(sender As Object, e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup Dim currentDomain As AppDomain = AppDomain.CurrentDomain 'for regular unhandled stuff AddHandler currentDomain.UnhandledException, AddressOf MYExceptionHandler 'for threads behind forms AddHandler System.Windows.Forms.Application.ThreadException, AddressOf MYThreadHandler End Sub Shared Sub MYExceptionHandler(ByVal sender As Object, ByVal e As UnhandledExceptionEventArgs) Dim EX As Exception EX = e.ExceptionObject 'Console.WriteLine(EX.StackTrace) DisplayError(EX) End Sub Shared Sub MYThreadHandler(ByVal sender As Object, ByVal e As Threading.ThreadExceptionEventArgs) 'Console.WriteLine(e.Exception.StackTrace) DisplayError(e.Exception) End Sub Shared Sub DisplayError(ByVal ex As Exception) Dim errorMessage As String = ("Unhandled Exception:" + vbLf + vbLf + ex.Message + vbLf + vbLf + ex.GetType.ToString + vbLf + vbLf + "Stack Trace:" + vbLf) + ex.StackTrace MessageBox.Show(errorMessage, "DMS Application Error", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.[Stop]) MessageBox.Show(ex.ToString, "DMS Application Error", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.[Stop]) End Sub End Class End NamespaceThis way you can set the Startup Form in the Application properties and not implement the Main()