Is there any way to break on the next line of code executed in Visual Studio?

13,141

Solution 1

Have you tried the Debug > Break All ("pause") button? (Ctrl+Break)

Debug > Break All

It'll usually break somewhere pretty low on the stack, like at Show() for your main form in a WinForms app, but if you then Step Into to get past that, it'll often work pretty well for this sort of thing.

Solution 2

Are you looking for the Step Into (F11) or Step Over (F10) ?

-- Edit

Do you also know about the Call Stack window? It can help you determine your location, and what is happening.

Solution 3

Conditional Breakpoints may be your answer. You can set them were you think your code is breaking and they will only halt when the condition is satisfied.

Share:
13,141
user3953201
Author by

user3953201

A C# .NET Developer with a passion for coding best practices and Test Driven Development

Updated on June 02, 2022

Comments

  • user3953201
    user3953201 about 2 years

    I'm trying to track down a bug that occurs when I click a particular element on an aspx page...

    In the past I've had to track down the class that handles that particular event and put a break point on the line that I think should be hit. Often it takes me several tries before I finally find the correct class....especially if the class is a user control buried somewhere...

    So it's left me wondering if there is any way to get Visual Studio to break at the next line of code executed after I click an element (say a button) on an aspx page. I know there is a way to break on any exception that is thrown so I'm thinking maybe there is something similar that could help me.

    If this sort of feature isn't a possibility, perhaps someone could suggest a better way for me to quickly find the class I want to debug...

  • user3953201
    user3953201 almost 15 years
    sorry, I'm aware of F11, F10, and the call stack window. I'm looking for something that allows me to break once I hit the code base after triggering an action on an aspx page.
  • user3953201
    user3953201 almost 15 years
    true, but I'm wanting to find a way to not have to set a whole bunch of break points in order to find the code I'm looking for
  • user3953201
    user3953201 almost 15 years
    that's good but in my case the bug I'm looking for isn't caused by an exception
  • user3953201
    user3953201 almost 15 years
    I'm not sure the Break All button is quite what I'm looking for as I would think the code would be executed within milliseconds of my click on the screen and as such I wouldn't be able to pause it fast enough.
  • Noon Silk
    Noon Silk almost 15 years
    mezoid: perhaps you want to write System.Diagnostics.Debugger.Break(); in a specific part of your code? That will force the debugger to break there, regardless of you explicitly setting a break point?
  • user3953201
    user3953201 almost 15 years
    except that I don't know where the class is that I need to put the break in... :(
  • Noon Silk
    Noon Silk almost 15 years
    Yes ... well, I'm afraid there is not much that can help you here, but the application of logic :) (well, maybe this: stackoverflow.com/questions/907856/…), if Mehrdad accepts it as a project :P)
  • user3953201
    user3953201 almost 15 years
    ok, well then if there is nothing that does what I'm looking for then I'll have to award you the answer just for telling me it can't be done. :) thanks for your help!
  • andriy
    andriy almost 15 years
    You don't have to award anyone an answer. You should only do it if your question is answered. You don't seem to me to have made much effort to try any of the answers given. Which is your choice, I suppose.
  • user3953201
    user3953201 over 14 years
    Ok, I've tried it again...and it didn't work. Perhaps its because I'm debugging an aspx page. When I hit pause and then click something on the window all it does is try to load the page...but nothing happens...I have to hit run and then pause it again before it will break and by then it is too late. I'll have to try your suggestion one something like a WinForms or Console app and see how it goes for that... if that works I'll mark yours as the answer...but for what I'm after it doesn't seem to work...
  • user3953201
    user3953201 over 14 years
    Ok I've tried it with a simple WinForms app and I figured out what I was doing wrong. When I hit pause it stopped the application and paused on one of the WinForm's default lines of code. I then pressed F11 which then allowed me to access the WinForm. Pressing one of the buttons on the form took me directly to that line of code which is exactly what I wanted. I'll have to try it at Work with the aspx page and see if works there too. I'll try to mark yours as the answer...sorry it's taken me so long to figure out...but I do appreciate having learned something I've been wanting to know for years
  • Chris Purves
    Chris Purves almost 13 years
    This is a good answer, and I don't think something that's "obvious". Simple - Break All, F11 (Step, usually on Application.Run line), click the button on your form, code breaks at that location. This will not work if you have certain things going on.. like WndProc overrides, etc. things that fire on the message pump. For other cases, this works just fine and is an easy way to say, "Break on the next action I do" (like clicking a button!).
  • Fls'Zen
    Fls'Zen over 11 years
    If you have background workers in your application and need to set the next line executed on the main thread, then this procedure works as well. Just make sure to set the current thread to the main thread before you step into.
  • Chris Purves
    Chris Purves over 10 years
    Update: I mentioned that this doesn't work well if you're doing things like overriding WndProc, trapping keyboard events, etc. - the solution is to add the [DebuggerStepThrough] attribute to your method. This will bypass that method and allow you to step into the "next" method after you've done the break-all and hit F11, but will bypass any other method that you don't want to debug.
  • Travis Heeter
    Travis Heeter over 8 years
    I am also interested in figuring out how to break the code on the next line executed. I tried Break All, but then it says I need to disable "Just My Code" I think I've done that before, and it was a nightmare. I had no idea what was going on after that, and it took a while to get it back to normal.
  • Jason Stevenson
    Jason Stevenson over 8 years
    @MatthewM. The [DebuggerStepThrough] attribute sounds brilliant! THANK YOU!