Namespace My ' The following events are available for MyApplication: ' ' Startup: Raised when the application starts, before the startup form is created. ' Shutdown: Raised after all application forms are closed. This event is not raised if the application terminates abnormally. ' UnhandledException: Raised if the application encounters an unhandled exception. ' StartupNextInstance: Raised when launching a single-instance application and the application is already active. ' NetworkAvailabilityChanged: Raised when the network connection is connected or disconnected. Partial Friend Class MyApplication Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup ' for threads behind forms AddHandler System.Windows.Forms.Application.ThreadException, AddressOf MYThreadHandler End Sub Private Sub MyApplication_Exception(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs) Handles Me.UnhandledException ' for regular unhandled stuff HandleError(e.Exception) End Sub Private Sub MyApplication_NextInstance(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupNextInstanceEventArgs) Handles Me.StartupNextInstance MsgBox("You can start this program only once.", MsgBoxStyle.Information Or MsgBoxStyle.OkOnly, "DMS Problem") e.BringToForeground = True End Sub Shared Sub MYThreadHandler(ByVal sender As Object, ByVal e As Threading.ThreadExceptionEventArgs) 'Console.WriteLine(e.Exception.StackTrace) HandleError(e.Exception) End Sub Shared Sub HandleError(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(ex.ToString, "DMS Application Error", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.[Stop]) End Sub End Class End NamespaceNice! I had noticed that app event button before, but could not realy get it work. You defently pointed me in the right direction - thanks again!
>>>>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()