Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
I need to Execute some block of code only if Code is running in Debug Mode.
I dont know how to achieve this, Can you illustrate it ??

I googled Many pages but found none useful.


Prathamesh
Posted
Updated 15-Feb-23 13:30pm

Please look at the solution by Prerak. Effectively and practically, it probably solves your problem.

However, this is not what you literally asked for. You asked "I need to execute some block of code only if code is running in Debug Mode". If you use the solution pointed out by Prerak, you will get something different: you will be able to execute or not execute some fragment of code, but only because you can have two different builds of you code: one when your fragment is executed, and one when it is not. Once your application is compiled, it will behave either in one or another way.

In other words, you cannot run your application in one or another "mode"; instead, you only can create two different applications which behave differently for two different configuration. One important node: the Debug is only a configuration and has nothing to do with debugging itself. What does have something to do with debugging is the compilation options. You can have as many different configurations as you want; a configuration is just an umbrella for some configuration set under a certain name; you manually configure options for each configuration, and these options may or may not be related to debugging. That said: be careful with the code which executes only in certain configuration; it can be a source of inadequate testing results.

In other words, you cannot determine if a code is running in Debug Mode because such mode does not exist.

Surprise? :-)

—SA
 
Share this answer
 
Comments
Mehdi Gholam 9-Nov-11 23:56pm    
Yes, but you can tell if you are running in a debugger.
Sergey Alexandrovich Kryukov 10-Nov-11 11:06am    
Detect a debugger? Yes, probably, but I don't know how. Can you explain us?
--SA
Prerak Patel 10-Nov-11 3:11am    
I agree. The code in Debug directive won't be compiled in Release mode. But I think that how OP wanted it to act.
Sergey Alexandrovich Kryukov 10-Nov-11 11:08am    
Thank you, Prerak.
Yes, OP might go your way, but I warned about it. It could be just extra diagnostics or something like that.
--SA
Use directives like
C#
#if DEBUG
    Console.WriteLine("Debug version");
#endif
 
Share this answer
 
Comments
prathameshpitale 9-Nov-11 22:54pm    
I kept my mode in #if Debug Directives,
But while code is not in debug mode still it gets executes
Prerak Patel 10-Nov-11 3:12am    
You need to build your application in specific mode - Debug or Release. SA has explained it well in detail.
Sergey Alexandrovich Kryukov 9-Nov-11 23:32pm    
Prerak, my comment may come at surprise: even though your answer is useful in practice, formally it is completely incorrect. Read the question again. There is no such thing: Debug Mode or Release Mode; one can just create different versions of software with different behavior.

Please see my solution.
--SA
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 9-Nov-11 23:33pm    
Sorry, this is not really related to the question. My answer to the question can look a bit surprising. The thing is: there is no Debug or Release modes. Please see my solution.
--SA
Noting the dates of the above replies, it may be a bit late for the OP ;-)
But in the hope that it may help others, here is what I found in 2023: https://stackoverflow.com/questions/36418307/visual-c-detect-if-the-program-is-loaded-into-the-visual-debugger[^]

In case that link gets broken, here's an example:
if (IsDebuggerPresent()) printf("Eureka!");

It works in VS2019 under Windows 10, and googling "IsDebuggerPresent" gets you straight to the Microsoft documentation.
--Jon.
 
Share this answer
 
Comments
Member 15914308 15-Feb-23 19:49pm    
Ooops, sorry: just spotted that you were asking about C#, but my answer relates to C++. Probably still relevant though. Jon.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900