I’m using Visual Studio 2008 on C# code.
I would like to only break on a breakpoint if another breakpoint has been hit (and broken upon.) Is there a way of doing that?
I would imagine as a subproblem it would be nice to get access to the information that the debugger has.
The rationale for this is I’m interested in only breaking on a certain breakpoint given a certain callstack (and at a certain point in the execution of one of those functions in the callstack). Perhaps I should be using the callstack instead? Another reason is it would be interesting to have programmatic access to the stuff that the debugger knows about.
Thanks.
Using local variable
The easiest way to create such a conditional breakpoint would be to create a new thread-static variable (or just static if it should be global). Suppose that our code looks as follows:
Let’s now assume that you set a breakpoint1 on
Console.WriteLine("test1");and breakpoint2 onConsole.WriteLine("test2");. You would like to break at the breakpoint2 only when the breakpoint1 was hit 2 times. In this case you would need to setHit Count...property of the breapoint1 tobreak when the hit count is equal to 2. Then in theWhen Hit...property checkPrint a messageand in the textbox type:{breakVariable = 1}:Then set the property
Condition...of the breakpoint2 tobreakVariable == 1and checkIs true:If you would like the breakpoint2 to become inactive after being hit you may again use
When Hit...property setting itsPrint a messagevalue to{breakVariable=0}.Using macro
This approach is much harder especially if you don’t like VBA (like me:) ) but you may be interested as it does not require any changes in the application code. Let’s define two macros:
Now for the breakpoint1 you still want to set the
Hit Count...property as previously but now in theWhen Hit...property instead of checkingPrint a messagecheckRun a macroand select theSetMyBreakpointprocedure. It’s very important that you provide the full path of the code file in theBreakpoints.Addmethod and the correct line (you may check the API to find other ways to set a breakpoint, like on the function instead a code file). One small caveat here – I observed that the automatic removal of the second breakpoint does not always worked – but maybe it was my Visual Studio.Using call stack
You may again use the
Conditionproperty of the breakpoint – have a look at this question to find some details.