问题
Is there an easy way to set conditional breakpoints in Visual Studio?
If I want to hit a breakpoint only when the value of a variable becomes something, how can I do it?
回答1:
Set a breakpoint as usual. Right click it. Click Condition.
回答2:
When you are using Express edition you can try this:
#if DEBUG
if( fooVariable == true )
System.Diagnostics.Debugger.Break();
#endif
if statement makes sure that in release build breakepoint will not be present.
回答3:
Visual Studio provides lots of options for conditional breakpoints:
To set any of these you
- Set a breakpoint.
- Right-Click over the breakpoint, and in the popup menu you select an option that suites you.
These options are as follows:
- You can set a condition, based on a code expression that you supply (select Condition from the popup menu). For instance, you can specify that
foo == 8or some other expression. - You can make breakpoints trigger after they have been hit a certain number of times. (select Hit Count from the popup menu). This is a fun option to play with as you actually aren't limited to breaking on a certain hit count, but you have options for a few other scenarios as well. I'll leave it to you to explore the possibilities.
- You can Set filters on the Process ID, thread ID, and machine name (select Filter from the popup menu)
回答4:
Just another way of doing it, (or if you are using express) add the condition in code:
if(yourCondition)
{
System.Diagnostics.Debugger.Break();
}
回答5:
- Set breakpoint on the line
- Right clik on RED ball
- Chose conditioal breakpoint
- Setup condition
回答6:
Writing the actual condition can be the tricky part, so I tend to
- Set a regular breakpoint.
- Run the code until the breakpoint is hit for the first time.
- Use the Immediate Window (Debug > Windows > Immediate) to test your expression.
- Right-click the breakpoint, click Condition and paste in your expression.
Advantages of using the Immediate window:
- It has IntelliSense.
- You can be sure that the variables in the expression are in scope when the expression is evaluated.
- You can be sure your expression returns true or false.
This example breaks when the code is referring to a table with the name "Setting":
table.GetTableName().Contains("Setting")
回答7:
Create a breakpoint as you normally would, right click the red dot and select "condition".
回答8:
- Set a breakpoint as usual
- Right click on the breakpoint and select Condition
- You'll see a dialog that says "Breakpoint Condition"
- Put a condition in the field e.g. "i==5"
The breakpoint will only get hit when i is 5.
回答9:
- Set a breakpoint as usual.
- Right-click on the breakpoint marker
- Click "Condition..."
- Write a condition, you may use variable names
- Select either "Is True" or "Has Changed"
回答10:
On Visual Studio 6.0
Alt+F9!!!
回答11:
Set the breakpoint as you do normally, right click the break point and select condion option and sets your condition.
回答12:
Create a conditional function breakpoint:
In the Breakpoints window, click New to create a new breakpoint.
On the Function tab, type Reverse for Function. Type 1 for Line, type 1 for Character, and then set Language to Basic.
Click Condition and make sure that the Condition checkbox is selected. Type
instr.length > 0for Condition, make sure that the is true option is selected, and then click OK.In the New Breakpoint dialog box, click OK.
On the Debug menu, click Start.
来源:https://stackoverflow.com/questions/6670415/how-to-set-conditional-breakpoints-in-visual-studio