Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I break a loop at a certain point when debugging?

Env: Visual Studio 2008 - C#

I have a for which runs for 1000+ times over a string array.

I want to have my app break when one of the strings matches a certain term so I can walk through at that point in my code.

Now I know I can add a piece of code that looks for this and a break point when it hits, but is there not a way to do this in the debugger?

like image 721
Coding Monkey Avatar asked Jun 18 '09 23:06

Coding Monkey


People also ask

How do you break a loop in debugging?

As a user I would suggest the following: make a hotkey to exit the loop in debugging mode, for example ctrl-shift-F11.

How do I skip a line in debugging?

The "Set Next Statement" (CTRL+SHIFT+F10) shortcut will work at the end of a function... but you need to use the mouse though as well. On VS 2019 there are those green arrows in front of lines, hold CTRL key and you can set next statement directly there, skipping anything in between.


3 Answers

Go to your code

  1. create a breakpoint
  2. right click on the red dot on the left
  3. select condition
  4. put something like i == 1000

or

at the middle of your loop

write

if (i == 1000){
  int a = 1;
}

and break over int a = 1;

The second method looks more like garbage, but I find it easier and faster to do

like image 135
Eric Avatar answered Sep 29 '22 05:09

Eric


Yes, you can in the debugger. It's called a "conditional breakpoint." Basically, right click on the red breakpoint and go to the "condition" option.

A quick google turned this and this up:

P.S. The last one is VS 2005, but it's the same in 2008.

like image 28
joshua.ewer Avatar answered Sep 29 '22 03:09

joshua.ewer


In visual studio you can set a conditional breakpoint - set a breakpoint at the point where you want to break as normal and then right click on the brown circle in the left margin and select "conditional breakpoint..." or whatever. You then type in an expression that evaluates to true when you want to break (e.g. i == 1000, or MyString = "hello world")

like image 24
Justin Avatar answered Sep 29 '22 04:09

Justin