Results 1 to 5 of 5

Thread: [RESOLVED] Determine if Running Under Debugger

  1. #1

    Thread Starter
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Resolved [RESOLVED] Determine if Running Under Debugger

    I am trying to determine how my application is running. There are four (I think) possibilities:

    F5 in the IDE
    CTRL F5 in the IDE
    Publish and Installed in Debug Configuration
    Publish and Installed in Release Configuration

    I have searched the forum for the answer and can't find the answer, though I swear I have seen it.

    Thanks in advance!
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Determine if Running Under Debugger

    It really depends what you're trying to achieve. Generally the best option is not to determine whether you're in debug mode at run time but rather to use conditional compilation, so code that's not needed in one particular mode is omitted when compiled:
    vb.net Code:
    1. #If DEBUG Then
    2.         'Code here will be compiled into a Debug build but not a Release build.
    3. #Else
    4.         'Code here will be compiled into a Release build but not a Debug build.
    5. #End If
    The lines beginning with # are not compiled into your assembly but are used by the compiler to determine whether the code between two # lines should be compiled or omitted. Conditional compilation can be used anywhere as long as the lines it leaves behind can be successfully compiled. For instance, this is legal code:
    vb.net Code:
    1. #If DEBUG Then
    2.     'The method has a parameter when debugging...
    3.     Private Sub DoSomething(ByVal data As Object)
    4.         'Do something while debugging here.
    5. #Else
    6.     '...but doesn't in a Release build.
    7.     Private Sub DoSomething()
    8. #End If
    9.         'Always do something here.
    10.     End Sub
    Last edited by jmcilhinney; Mar 28th, 2009 at 10:28 AM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Determine if Running Under Debugger

    Thanks. I knew about
    #If CONFIG = "Release" Then

    It was the first two (F5, CTRL F5) that was giving me a problem. I continued to look and found

    If Debugger.IsAttached Then

    which seem to take care of them. I was looking for one solution, but it looks like a combination of the two.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Determine if Running Under Debugger

    With regards to your original four options, there is no difference between 2 and 4 because 2 doesn't actually run anything IN the IDE. It simply runs the output from the bin\Release folder, which is exactly the same as what you would deploy. As for option 3, it's very, very rare that you should ever encounter that scenario. The only genuine reason I can think of is that there's an issue with a deployment that you cannot reproduce when debugging yourself in the IDE.

    Also, if you already know something about the solution to your problem then it's a good idea to say so, so people don't waste their time and yours telling you thinks you already know. If you say "I already know X but is there anything better" then people won't bother posting to tell you about X.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Determine if Running Under Debugger

    Sorry about the omission. If I had searched 10 minutes more I would have found the answer.

    What I ended up with
    Code:
    #If CONFIG = "Release" Then
            _Debug = False
    #Else
            _Debug = True
    #End If
            If Not Debugger.IsAttached AndAlso Not _Debug Then
                'no debugger
            Else
                'debugger
            End If
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

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