How to stop debugger after a specific number of breakpoint hit ?

By | August 6, 2013

You can stop the debugger at breakpoint after a specific number of breakpoint hit. Hit Count is used to keep track on how many times the debugger has paused at some particular breakpoint. This is extremely useful when you want to stop the debugger after a certain number within a iterative process; and of course where you have nothing to track using conditional break points.

For an example, you have following code block and break point is placed at line number 21, where it is printing some values in  the console. Now you want to check the value for the 3rd item within the collection before it print.image

For the small collection given in the example, you can iterate through the every items and check the values for the specific item. But is it possible to check when the collection size is big ? Say you have 1000 items in the collection and you want to check the  value of 300th elements . At this point of time “Breakpoint Hit Count” features if Visual Studio becomes very handy and useful. 

image

By default, it’s set to always.  So, whenever the breakpoints hits, hit count will increase automatically but the debugger will stop every time in the breakpoint.  You can also specific the count of hit over here to stop the debugger. For this example we can selection option 2 and set the values of count to 3 as shown in figure below.

image

image

Om click of “OK”, the break point icon will change to a conditional break point icon (additional  + Symbol ) to indicate some additional condition has been added with the breakpoint.

image

Now, if you try to debug the application, the debugger will only stop for the items when break point hit count is equal to 3.

Note: This is not a new features of visual studio, it has been there from early version of Visual Studio. This may be known to many of you, but  few of you may found it interesting.

Interested to learn something more on debugging and breakpoints ? Read this post  http://www.codeproject.com/Articles/79508/Mastering-Debugging-in-Visual-Studio-2010-A-Beginn

One thought on “How to stop debugger after a specific number of breakpoint hit ?

  1. Pingback: 10 Effective Debugging Tips for .NET Developer

Comments are closed.