Question on how to remove a Visual Studio Breakpoi

2020-03-01 03:22发布

问题:

Let's say I have 10 breakpoints and I want to clear one but not the other 9.

If I toggle the breakpoint on the one that I want to remove, it is resurrected the next time I restart the app. The only way that I know to permanently get rid of it is to clear ALL the breakpoints, which I would rather not do since I would have to reset the other 9.

Is there a better way in ANY VS version?

回答1:

The breakpoint's state is only temporarily altered if you change it while you're debugging and it's a multi-bound breakpoint. This is actually a feature of Visual Studio. See the last post here.

If you're not debugging, and you remove it, then it won't come back. Alternately, as others have suggested, you can remove it permanently using the breakpoint management window.



回答2:

Hitting Ctrl+Alt+B will bring up a list of all breakpoints in your solution, from which you can manually toggle or delete them with a right-click.



回答3:

Open the breakpoints window (Debug -> Windows -> Breakpoints), select the breakpoint you want to delete and press the delete key (or click the cross icon).

If you are toggling the breakpoint using the keyboard (F9 using my keyboard mappings), it sometimes doesn't remove it properly. Pressing F9 again will remove it fully (this is due to the breakpoint being set on multiple threads and toggling it whilst debugging only disables the main breakpoint but not the ones for the other threads).



回答4:

If you want to delete a breakpoint with F9 or by clicking the red glyph, that breakpoint needs to be childless. Otherwise, the breakpoint will persist through its surviving child breakpoints. (Child breakpoints can form when you set breakpoints during debug.)
You could check this question, " Disable/remove child Breakpoints? ", for a macro to remove child breakpoints. I think you shouldn't call the macro during a Debug session though, as this might result in your breakpoints to not be hit.



回答5:

The following code can be used as a macro to remove the breakpoint on the currently selected line. (Note that Visual Studio automatically selects the line of a breakpoint when it is hit.)

Sub RemoveBreakPoint()
    Dim debugger As EnvDTE.Debugger = DTE.Debugger
    Dim children As EnvDTE.Breakpoints
    Dim sel As Integer = DTE.ActiveDocument.Selection.ActivePoint.Line
    For Each bp As EnvDTE.Breakpoint In debugger.Breakpoints
        If bp.File <> DTE.ActiveDocument.FullName Then
            Continue For
        End If
        For Each bpc As EnvDTE.Breakpoint In bp.Children
            If bpc.FileLine = sel Then
                bp.Delete()
                Exit For
            End If
        Next
    Next
End Sub

You can assign a keyboard shortcut to it for easy access. (Tools > Options > Environment > Keyboard.)



回答6:

Cross-post from https://stackoverflow.com/a/35935390/257470 , but it's even more relevant here.

There are some answers here, but in my opinion proposed actions are distractive to use during debugging (I don't want to lose my focus).

My flow with sticky breakpoints during breakpoints is as follows:

During debug, DISABLE the breakpoint instead of removing it.

Possible ways of disabling a breakpoint:

  • hover with cursor and click the two cycle icon;
  • or use context menu on it;
  • or keyboard short-cut CTRL+F9.

Later on, during development, I remove a disabled breakpoint when I see one.

PS. It's also a good practice to remove all breakpoints once in a while.