Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Exception Handler - How to
Message
From
07/04/2011 14:17:57
 
 
General information
Forum:
ASP.NET
Category:
Forms
Miscellaneous
Thread ID:
01506276
Message ID:
01506416
Views:
52
Thank You!

This worked almost out of the box. Here is what I ended up wiith:
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 Namespace
Nice! 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!

This also - finally - allowed me to realy see whats wrong with my 64-bit stuff - see the reply in that thread.


>>>>I am trying to add an exception handler to my vb.net WinApp - and as usual run into to some problems :-(
>>>>
>>>>From my best friend Google I pieced some code together which should accomplish this :
>>>>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 :
>>>>
>>>>Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "WindowsApplication1.DocProcess_Main.resources" was correctly embedded or linked into assembly "DMS DataEntry" at compile time, or that all the satellite assemblies required are loadable and fully signed.
>>>>
>>>>My problem: I don't even know if I implemented the code correctly?
>>>>Any suggestions are great appreciated!
>>>
>>>The error doesn't look like it is related to the introduction of the exception handling code per se. Were you instantiating DocProcess_Main in the same place and way previously ?
>>
>>All I did was adding the class "AppMgr" and in there doing my event handling and starting the form.
>>
>>I added that class in the code file from the form - maybe thats the problem - does it need to be in a diferrent file. If yes, how do i tell the project that that new file is the entry/starting point?
>
>I think the cleanest way to do this is to wire up the event handlers in the Application.Events.vb file.
>Open the Properties window of the main application and, in the Application tab click 'View Application Events. This will create a skeleton ApplicationEvents.vb file.
>Select 'Application Events' in the left dropdown, and 'Startup' in the right dropdown. This will create stub code for the StartupEvent. Wire up the events here. The end result should look something like:
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 Namespace
This way you can set the Startup Form in the Application properties and not implement the Main()
>If you want to have the file visible in the Solution Explorer you will need to set 'Show All Files' in the project menu.
Previous
Reply
Map
View

Click here to load this message in the networking platform