views:

221

answers:

7

How do you put an "IF DEBUG" condition in a c# program so that, at run time, it will ignore a set of code if you are running in Debug mode and yet, execute a block of code if the program is not running in debug mode? A situation where this can be used is if a time stamp is taken at the start of a block and another time stamp is taken at the end. THey will hardly differ at run time. Yet, if you are stepping through the code in debug mode, they will differ a lot, and error conditions in an "if block" might be kicked off leading to the untimely (pun) execution of some code.

+15  A: 

You just put your code in a block like this:

#IF DEBUG

//code goes here

#endif

This is not a runtime thing, this is a preprocessor directive, which means the code in that block won't even be compiled and will not be included.

If you want to check at runtime if you're debugging, you can check Debugger.IsAttached

BFree
+1 for the "runtime" bit
Earlz
On `Debugger.IsAttached`, I believe it will be true if you are doing remote debugging on a production site. Yes, I know you usually don't want to do this, but in some edge cases where you just can't figure out what's wrong it may help. Just make sure your "debug" code won't cause problems in a production environment if you use remote debugging.
Nelson
+1 @Nelson: Remote debugging (attaching a debugger) is the only way to debug a windows service.
SnOrfus
@SnOrfus - actually, that's not completely true. :) I can't find the code now (which I copied from a website), but I have one service set up in a way that if I choose "Debug" build, it runs inside Visual Studio with normal debugging ability. If I choose "Release", it builds the normal installable service which can only be debugged with remote debugging. If you ever want a service to run with debugging symbols, I suppose you could create a new build configuration (Debug-normal service).
Nelson
@Nelson: Very interesting, If you can find the code/article I'd like to see it (I'll start looking myself). I was going off the MSDN article here: http://msdn.microsoft.com/en-us/library/7a50syb3(VS.80).aspx.
SnOrfus
@SnOrfus: Yeah, and normally you can't debug it that way, but some people come up with ingenious ideas. I think it was basically running as a console application and manually running OnStart(). I should have it by tomorrow.
Nelson
+4  A: 

Use the preprocessor instruction #if:

#if debug
    // run in debug mode
#else
    // run if not in debug mode
#endif
SnOrfus
doesn't work see OP ... so that, at run time ...
KevinDTimm
A: 
#if DEBUG
        // something
#elif
        // some other thing
#endif
simendsjo
doesn't work see OP ... so that, at run time ...
KevinDTimm
@KevinDTimm: Ah, missed that point. Don't mind me, I just give the wrong answers
simendsjo
+2  A: 

You can also use the

[Conditional("Debug")]

attribute on methods.

Arne
doesn't work see OP ... so that, at run time ...
KevinDTimm
A: 
#if (DEBUG)
...
#else
...
#endif

see here: http://bloggingabout.net/blogs/rick/archive/2005/03/10/2396.aspx

Tim Goodman
doesn't work see OP ... so that, at run time ...
KevinDTimm
@KevinDTimm: Although the questioner says "at run time", I'm not seeing why his specific scenario couldn't be dealt with by the preprocessor directive. It seems he wants to skip over the `if (ItTookALongTime)` stuff when running in debug mode, since in debug mode we expect it may take a long time ... so why not just make sure the `if (ItTookALongTime)` stuff is excluded when we compile for debugging?
Tim Goodman
because it's not what he asked for :) he wanted a way to turn on a flag for debugging purposes, this could just as easily be a way (in the future) to turn on logging (for example).
KevinDTimm
+1  A: 

A couple more details:

  • You can wrap an entire method or just a code block or just a line of code.
  • You need to add using System.Diagnostics;
DOK
the only answer that addresses the question
KevinDTimm
A: 

Since every other answer (but one) does not address your question, let me try:

If you wish to have a runtime switch, you'll need a way to get the information from your environment or the command line. Additionally, once you get that info, you'll need to apply it to your program.

1) For this you'll need a global variable.

bool bDebug;

2) get debugging information

bDebug = strcmp (getEnv ("DebuggingMode"), "1");  // you'll need to mess with this a little - do 'man getenv' and 'man strcmp' for an understanding of what I'm doing
  • or -

utilize the getopt() function to find out if debugging is turned off or on

3) every place in your code you want debugging turned on, check if bDebug is set

   if (bDebug) {
   // do something
   }
KevinDTimm