Results 1 to 9 of 9

Thread: Way to make application think that it is in debug mode at runtime?

  1. #1

    Thread Starter
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,388

    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 ...

    Thanks,
    Kris

  2. #2
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,686

    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 :-)

  3. #3

    Thread Starter
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,388

    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?

    Kris

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,047

    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.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,388

    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.

    Kris

  6. #6
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: Way to make application think that it is in debug mode at runtime?

    Quote Originally Posted by i00 View Post
    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.

    Test Unhandled.zip

    P.S.: I saved this as VS2008 project.

  7. #7

    Thread Starter
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,388

    Re: Way to make application think that it is in debug mode at runtime?

    Quote Originally Posted by TnTinMN View Post
    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.

    Test Unhandled.zip

    P.S.: I saved this as VS2008 project.
    ... well I was away for the weekend and didn't have access to my project .... I am not using the framework ... so in my sub main have:

    vb Code:
    1. Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException)
    2.         AddHandler Application.ThreadException, AddressOf LogThreadException
    3.         AddHandler currentDomain.UnhandledException, AddressOf LogUnhandled
    When an interface (form) is in the main thread things work fine ... but when they are not errors are not captured...

    ALSO it appears to work in VS2008 ... but not 2013 (BTW going back is not an option)???

    happens in RUNTIME only remember...

    Test project attached ... also I didn't use any kind of naming conventions ...

    Kris
    Attached Files Attached Files

  8. #8
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: Way to make application think that it is in debug mode at runtime?

    Quote Originally Posted by i00 View Post
    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.

  9. #9

    Thread Starter
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,388

    Re: Way to make application think that it is in debug mode at runtime?

    Quote Originally Posted by TnTinMN View Post
    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?

    Kris

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width