Setting a Breakpoint with Code
Versions: 2008,2010
Published: 10/30/2010
Code: vstipDebug0036
There will be times you want code to break when you are debugging without having to set a Breakpoint. You can do this in your code quite easily. Here is how you do it:
Compiler Directive
In C# and VB you need to set a compiler option that will only hit the breakpoint when debugging. If you don't then, in your Release code, it will continue to hit the code-based Breakpoint which is generally considered a bad thing. To do this you use the #If DEBUG compiler option. I will demonstrate this in each of the code samples.
C#
In C# you set a Breakpoint by using the System.Diagnostics.Debugger.Break() method:
VB
In VB you set a Breakpoint by using the Stop command:
And there you have it! Now you can use code to set Breakpoints. Just as an aside, Breakpoints set in this way will NOT show up in the Breakpoints Window.