I have some running code and I would like to add a conditional breakpoint, but I only know how to add conditions to existing breakpoints. For example if I add a breakpoint to a line of my C# code, using for example F9
, I can then right-click on the breakpoint's red dot in the left gutter which gives me this menu
from which I can choose Condition ...
to open up the conditional breakpoint settings
However I want to insert a conditional breakpoint in an often called function with a condition that is rarely true so that I can find out why an occasional error fires. I do not want to stop the code debugging, especially if it is an error that only manifests itself after some time. So the method above is inadequate. When I first insert the breakpoint, but before I have had a chance to add its condition, it will fire and the program will 'break'.
How do I add a breakpoint with an associated condition into code running under the debugger as a single atomic action, i.e. not add the breakpoint and then add a condition to it but add the breakpoint with its condition in one go?
I do not want to stop the code debugging […]. When I first insert the breakpoint, but before I have had a chance to add its condition, it will fire and the program will 'break'.
You can prevent this by making sure that the debugger isn't attached to the running process at that time:
Detach the debugger from your running process (via menu item Debug → Detach all).
Set a breakpoint.
Define a break condition on the breakpoint.
Re-attach the debugger to your running process (via menu item Debug → Attach to process…).
I work on OzCode, a commercial extension to Visual Studio that has two ways of adding a Conditional Breakpoint in one click -
As QuickAction (similar to Alt+Enter in Resharper). This will suggest relevant conditions for your Conditional Breakpoint, based on the type of the variable (ie, > 0
for numbers, == null
for reference types, etc.
If you are already on a breakpoint and see the invalid value that is causing the bug, and want to add a Conditional Breakpoint on it so that you can return to the same point again after changing your code, you can add one from the DataTip:
This will automatically created a Conditional Breakpoint on the relevant value, which you can edit before you hit enter to approve:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With