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:
class Program
{
#if DEBUG
[ThreadStatic]
static int breakVariable = 0;
#endif
static void Main(string[] args)
{
TestMethod2();
TestMethod1();
TestMethod2();
TestMethod2();
TestMethod1();
TestMethod2();
}
static void TestMethod1()
{
Console.WriteLine("test1");
}
static void TestMethod2()
{
Console.WriteLine("test2");
}
}
Let's now assume that you set a breakpoint1 on Console.WriteLine("test1");
and breakpoint2 on Console.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 set Hit Count...
property of the breapoint1 to break when the hit count is equal to 2
. Then in the When Hit...
property check Print a message
and in the textbox type: {breakVariable = 1}
:

Then set the property Condition...
of the breakpoint2 to breakVariable == 1
and check Is true
:

If you would like the breakpoint2 to become inactive after being hit you may again use When Hit...
property setting its Print a message
value 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:
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports EnvDTE90a
Imports EnvDTE100
Imports System.Diagnostics
Public Module Module1
Public Sub SetMyBreakpoint()
Dim bps As EnvDTE.Breakpoints
bps = DTE.Debugger.Breakpoints.Add(File:="C:\MyProject\ConsoleApplication1\Program.cs", _
Line:=25)
Dim bp As EnvDTE80.Breakpoint2
For Each bp In bps
bp.Tag = "mytag"
' Add this line only if you want the breakpoint to be removed on hit
' Although I don't know why - it does not always worked :(
bp.Macro = "Macros.MyMacros.Module1.RemoveMyBreakpoint"
Next
End Sub
Public Sub RemoveMyBreakpoint()
Dim bp As EnvDTE.Breakpoint
For Each bp In DTE.Debugger.Breakpoints
If (bp.Tag = "mytag") Then
bp.Delete()
End If
Next
End Sub
End Module
Now for the breakpoint1 you still want to set the Hit Count...
property as previously but now in the When Hit...
property instead of checking Print a message
check Run a macro
and select the SetMyBreakpoint
procedure. It's very important that you provide the full path of the code file in the Breakpoints.Add
method 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 Condition
property of the breakpoint - have a look at this question to find some details.