Way to make application think that it is in debug mode at runtime?
Hi ... just wondering if there is a way to make my application "think" that it is in debug @ runtime...
As with the newest versions of the framework global unhandled exception handling doesn't work (unless in debug) for unhandled exceptions on other threads ...
Re: Way to make application think that it is in debug mode at runtime?
Hello,
If you are referring to MyApplication.UnhandledException then this only works when compiled for Debug mode. If Debugger.IsAttached returns True when there is an exception and you have written your own UnhandledException that event is triggered. Then again you may be doing something different :-)
Re: Way to make application think that it is in debug mode at runtime?
Ok ... it is compiled in debug mode ... but when using it in VS IsAttached=true ... when not it is false... my custom error handler ALWAYS works when running in VS, but not outside it for unhanded exceptions that occur in secondary threads...
Regardless if i am compiled in Debug mode or not isAttached returns True when running in VS and false otherwise ... when True the unhanded exceptions work as they should.
Any ideas, I thought it may be possible to make it think it was being debugged in VS, so that it would work?
Re: Way to make application think that it is in debug mode at runtime?
It's an interesting problem, and one I haven't encountered, but is it really something you need to solve? After all, just relying on some generalized unhandled exception handler seems like a pretty rough solution. Wouldn't it always be better to trap the exceptions closer to where they occur, or keep them from occuring in the first place, if that is possible.
Re: Way to make application think that it is in debug mode at runtime?
Yes i ALWAYS try to handle them where they occur... and very rarely encounter the global one picking them up ... but I think it is still nice to have a global one as a fallback ... so that i can send me the details about an un-handled error that i have not made previsions for.
Re: Way to make application think that it is in debug mode at runtime?
Originally Posted by i00
Yes i ALWAYS try to handle them where they occur... and very rarely encounter the global one picking them up ... but I think it is still nice to have a global one as a fallback ... so that i can send me the details about an un-handled error that i have not made previsions for.
Kris
Since you have not stated which event you claim does not work properly, it is hard to address that. However, handling the AppDomain.UnhandledException Event will allow you to log issues from non UI threads. For UI thread errors, you can use either the System.Windows.Forms.Application.ThreadException (non Application Framework - Sub Main startup) or the Application Framework's (Form startup) MyApplication.UnhandledException.
Note that when the VS debugger is attached, AppDomain.UnhandledException intercepts the UI thread unhandled errors.
Here is a test project for you to play with. Button1 will start a thread that raises an exception and Button2 will raise an exception on the UI thread. The error handling code is in the "ApplicationEvents.vb" file.
Re: Way to make application think that it is in debug mode at runtime?
Originally Posted by TnTinMN
Since you have not stated which event you claim does not work properly, it is hard to address that. However, handling the AppDomain.UnhandledException Event will allow you to log issues from non UI threads. For UI thread errors, you can use either the System.Windows.Forms.Application.ThreadException (non Application Framework - Sub Main startup) or the Application Framework's (Form startup) MyApplication.UnhandledException.
Note that when the VS debugger is attached, AppDomain.UnhandledException intercepts the UI thread unhandled errors.
Here is a test project for you to play with. Button1 will start a thread that raises an exception and Button2 will raise an exception on the UI thread. The error handling code is in the "ApplicationEvents.vb" file.
Re: Way to make application think that it is in debug mode at runtime?
Originally Posted by i00
Test project attached ... also I didn't use any kind of naming conventions ...
I believe that the reason your test case did not work is that you are starting a secondary UI thread and the error is raised on that UI thread. If you modify you code like this:
Code:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim NewFormThread = Sub()
AddHandler Application.ThreadException, AddressOf Autoexec.LogThreadException
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException)
Using frm As New Form1
frm.Button1.Text = "This errors but doesn't work ... RUNTIME ONLY"
frm.mess = "new Message"""
frm.Button2.Visible = False
frm.ShowDialog()
End Using
End Sub
Dim t As New System.Threading.Thread(NewFormThread)
t.Start()
End Sub
it should now capture the secondary UI thread error.
Last edited by TnTinMN; Dec 14th, 2014 at 07:41 PM.
Re: Way to make application think that it is in debug mode at runtime?
Originally Posted by TnTinMN
I believe that the reason your test case did not work is that you are starting a secondary UI thread and the error is raised on that UI thread. If you modify you code like this:
Code:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim NewFormThread = Sub()
AddHandler Application.ThreadException, AddressOf Autoexec.LogThreadException
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException)
Using frm As New Form1
frm.Button1.Text = "This errors but doesn't work ... RUNTIME ONLY"
frm.mess = "new Message"""
frm.Button2.Visible = False
frm.ShowDialog()
End Using
End Sub
Dim t As New System.Threading.Thread(NewFormThread)
t.Start()
End Sub
it should now capture the secondary UI thread error.
Why does this function differently when the debugger is attached?