views:

145

answers:

4

Hi,

Is there a way to tell in the code if you are running in the debugger in VS2008? I need to set up a couple things for us developers differently than when the user is running the click one deployed app? I see that there are things you can do to tell if you are in Designer mode but that is not the same.

thanks! Bill

+1  A: 

I tend to separate out debug-only code using #debug:

#if DEBUG
    RunMeInDebugModeOnly();
#else
    RunMeInReleaseModeOnly();
#end if

The #else isn't necessary and is only required if you don't want something to run in Debug mode.

#if DEBUG
    RunMeInDebugModeOnly();
#end if

And finally if you only want code to execute in Release mode:

#if !DEBUG
    RunMeInReleaseModeOnly();
#end if

The code within the if #DEBUG clause doesn't get executed when you run in Visual Studio in Release mode, but you don't get the full functionality of the debugger then anyway ;-)

Andy Shellam
This doesn't answer the question however, because you can still debug in the Release configuration and run the .exe generated by the Debug configuration (and thus not using the debugger)
Webleeuw
It does answer the question because the poster doesn't want the code to run in ClickOnce, but does for developers. It's not that often that you debug in VS in a Release build (doesn't debug as well as a debug build.) If the developer does run the debug build directly, they're still a developer so it should still behave differently - in the context of the question. If the developer releases the Debug build through ClickOnce then that developer has done a Bad Thing(TM.)
Andy Shellam
Plus you can debug a build on a user's PC, in which case the debug code would be executed with your method - depending on what actually needs to be done differently for developers, this could be good or bad, so both our answers are valid for different situations. For example if the poster wanted the app to use a different database when running "out in the field", and remotely debugs the app, this would be a bad thing because the correct database then wouldn't be used.
Andy Shellam
Ok, there's certainly a truth in there :). I think it's a matter of how literally the question title must be taken. If I take it to the letter, then Bill Campbell just wants to know when a debugger is attached or not. Both answers might fit the situation, depending of what he needs.
Webleeuw
A: 

If you just want to create different code for Debug dlls use the DEBUG define #if DEBUG

If you want to know at runtime if your code is currently beeing debugged you can hook in using Visual Studio's DebuggerVisualiazers

bitbonk
A: 

You can get the name of the current EXE and if it has "vshost" in it, you are in the debugger.

Timores
Please be aware that you can start your exe and attach a debugger after that, so no vshost will be in the name of your current exe...
BeowulfOF
Agreed, but the question is about "setup", so at start-up, it is correct. Although I concede that running under vshost is something that is configurable, so it could be removed.But I only advise to do this for debugging purposes, not for real stuff. I used it in a console app. If run under the debugger, I add a "press enter to continue" message then Console.ReadLine, which annoys me when I run the app from a command prompt.
Timores
I did not know about the nice property mentioned by Webleeuw; I much prefer it over my "hack".
Timores
+7  A: 

Exact copy of my answer in the related question:

if (System.Diagnostics.Debugger.IsAttached) { 
    // You are debugging 
}
Webleeuw
Wow, I didn't know that, cool !
bitbonk
Doesn't this mean the code still gets compiled in, even in Release mode, and the system has to do an extra if{} when it encounters this? I know it's extremely negligible but I prefer not to have debug-only code in a release build in the first place.
Andy Shellam
It will be compiled in Release mode with the system doing an extra if, true, but it does answer the question as stated :)
Webleeuw
Perfect! Just what I was looking for! Thanks Much!!
Bill Campbell