|
-
Mar 28th, 2009, 10:17 AM
#1
[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!
-
Mar 28th, 2009, 10:23 AM
#2
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:
#If DEBUG Then 'Code here will be compiled into a Debug build but not a Release build. #Else 'Code here will be compiled into a Release build but not a Debug build. #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:
#If DEBUG Then 'The method has a parameter when debugging... Private Sub DoSomething(ByVal data As Object) 'Do something while debugging here. #Else '...but doesn't in a Release build. Private Sub DoSomething() #End If 'Always do something here. End Sub
Last edited by jmcilhinney; Mar 28th, 2009 at 10:28 AM.
-
Mar 28th, 2009, 10:30 AM
#3
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.
-
Mar 28th, 2009, 10:38 AM
#4
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.
-
Mar 28th, 2009, 10:46 AM
#5
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|