How to: create a breakpoint using conditions? [C # Express]

I saw this on my Visual C # 2008 RSS feed forever:

http://lincolnfair.net/oldLincolnFair/mad.jpg

I'm sure this is only a feature of VS 2010, but I was wondering if this is the case to reproduce this in VS 2008?

+6
c # visual-studio-2008 visual-studio visual-studio-2010 breakpoints
source share
3 answers

Similar to @Relster I have a code snippet with the following

#if DEBUG if( node.Name == "Book" ) System.Diagnostics.Debugger.Break(); #endif 

Where node.Name == "Book" changes depending on the condition for which I want to check. the #if DEBUG wrapper ensures that checks never force it to free code.

It is also much faster than using conditional breakpoints in Visual Studio. When you use the built-in conditional bp, the visual studio must enter the application, pause all threads, evaluate the expression and determine if it is true every time it hits the breakpoint. In a narrow loop, this may be the difference between near-full performance and launch bypass.

+19
source share

You can do this in VS 2008. I’m sure there are many ways to do this, but one way is to right-click on the red dot on the edge of the existing breakpoint and select condition... and then just give it a condition, which evaluates to bool , and it will only break if true. A conditional statement must have access to everything in scope in the line where the breakpoint is set.

There are other parameters in this context menu that allow you to filter what will cause a break (for example, only certain flows), break down based on the number of hits of a breakpoint, run macros when you click a breakpoint, etc.

+4
source share

Another way to do this is to create your own conditions and use the call:

 System.Diagnostics.Debugger.Break(); 

Although it may not be as complicated as setting VS2010 breakpoints, you can get the same effect with minimal code overhead. Remember to take this stuff when you create the release code.

Note. In VS2008 and VS2005, you can set a conditional breakpoint by setting a regular breakpoint (F9 or double-clicking in the gutter), and then right-clicking on this breakpoint to set a "condition ...". The ability to set conditional breakpoints is NOT available in the VS2008 Express Edition.

+1
source share

All Articles