18

Visual Studio opens source code on top of the stack when I "break all" while debugging; I want to keep the cursor on the document I'm currently working on, without any other document or window (e.g.: no symbols loaded) being opened.

8
  • A workaround I'm using for the "no symbols" window, although not entirerly what I need, is to place that window on the bottom with all the other inspection windows
    – ccalboni
    Commented Feb 1, 2013 at 12:00
  • Oh this has been annoying me since vs2002. Alas I don't know a workaround. Maybe wait for vs2039
    – wal
    Commented Feb 1, 2013 at 12:52
  • LOL @wal! I can't believe it's not yet fixed, maybe it's just me (and you) being annoyed by this...
    – ccalboni
    Commented Feb 1, 2013 at 16:26
  • 2
    Is not a simple workaround to click the "navigate backward" button (Ctrl+-)? Commented Feb 6, 2013 at 8:57
  • 2
    Potential duplicate of stackoverflow.com/questions/1715875/…
    – aL3891
    Commented Feb 6, 2013 at 9:04

3 Answers 3

11
+100

There is a way to stay on the current document, but that requires creating a Visual Studio add-in and a new UI command in the Debug toolbar. Credits for this answer should actually also go to openshac, who posted a similar SO question and also gave a workaround in his OP by using a macro.

The implementation is fairly simple (it took me a few minutes to have it working). First, in the add-in project, modify the Exec method in the Connect.cs file like this:

public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
{
    handled = false;
    if(executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
    {
        if(commandName == "BreakInCurrentDocument.Connect.BreakInCurrentDocument")
        {

            // here's where the magic happens
            // ******************************
            var activeWindow = _applicationObject.ActiveWindow;
            _applicationObject.Debugger.Break();
            if (_applicationObject.ActiveWindow != activeWindow)
            {
                _applicationObject.ActiveWindow.Close(vsSaveChanges.vsSaveChangesNo);
            }
            // ******************************

            handled = true;
            return;
        }
    }
}

After creating and registering the add-in, just:

  1. click TOOLS on the Visual Studio's menu
  2. Customize
  3. Commands
  4. Choose the "Toolbar" radio button
  5. Select "Debug"
  6. Add Command...
  7. From the "Addins" category, choose your custom add-in.

That's it.

3
  • 1
    This just works: thank you a thousand times. Will give bounty tomorrow, can't do this before 20 hours.
    – ccalboni
    Commented Feb 6, 2013 at 12:57
  • This is the right answer, but if you want to skip addin creation and registration steps then see Jarek's answer about it being part of latest build of VSCommands extension. Commented Feb 7, 2013 at 7:16
  • 1
    VSCommand looks great, I go with @AlexFilipovici solution for bounty and accepted answer since it just fit perfectly my question and needs, leaving total control with integration in my IDE. Anyway, thanks to everybody for support.
    – ccalboni
    Commented Feb 7, 2013 at 9:20
10

Latest build of VSCommands extension (free version) available from http://visualstudiogallery.msdn.microsoft.com/a83505c6-77b3-44a6-b53b-73d77cba84c8 has just what you want. It adds a Break In Current Document button to Debug Toolbar and Debug Menu:

http://vscommands.squaredinfinity.com/Media/VSCommands/BlogPost//blog/breakincurrentdocument.png

2
  • 1
    Would like to add that this extension was unfortunately abandoned in VS2015. Commented Jan 13, 2017 at 14:37
  • Visual Studio Toolkit, also available from the Extensions menu within VS, has this feature. After installing, I simply changed (Options > Keyboard) the key bindings for Debug.BreakAll and Debug.BreakAllInCurrentDocument to have the usual CTRL+ALT+BREAK combo bound to the new command marketplace.visualstudio.com/…
    – ccalboni
    Commented Jul 25, 2017 at 12:12
1

It's a feature.. when you do "break all" then it is assumed that your process has hung. The first thing you might be interested in such case is - WHERE. Hence, it's directing you right down to the 'current' place that is being executed. IIRC, this is defacto standard for all low-level debuggers. If you don't want the "no symbols loaded" just mark the 'show disasembly' and it will never pop up again:) (of course, instead, you will see the exact point of stop. And yes, this is also a feature that I myself used many times to debug unknown library code)

On the other hand, if you know where you want the code to stop, place the breakpoint there instead.

On yet another hand (as if we had three), if you want to actually stop the application - stop it, don't break, just stop.

I sense that your actual problem lies in the fact that you use one of the features in a wrong way, and therefore another feature bugs you. Please tell me, what do you use the "break all" for and how/why does it collide with your current text-editing. Why can't you stop or break-at-here instead? Or "detach"?

Anyways, I have to admit that as a feature, there should be some option for turning it off, just for the sake of configurability of the IDE.


EDIT

AAhh.. you're right. I've completely forgot about the glorious edit&continue. I'm not joking/teasing, E&C is a great feature that I wish all other platforms had. I've forgot about it, because... I extensively use lambdas, generics, foreachs and etc features that effectively block edit-and-continue.

Anyways, the point is, since that the edit-and-continue is the golden feature that you'd like to use - the application must be in 'break' mode. However, nevertheless how do 'break'/'pause' etc it, the IDE will assume it that the PAUSE was you goal, not editing, hence it will show you where did you pause the app.

There are a few options in MSVS like "show just my code" that may help you a little, but it will not solve the problem: edit-and-continue during debugging was designed for "small, local edits". Like, if(x>0)throw new uncaught() instead if(x<0)throw new uncaught(). Your app stopped on assertion or breakpoint and is about to crash, first-change exception handler fired off and here's your chance! You unwind the crash handler, correct the code, then run. Everything in the same one method which you had the stop occur in, as a way of just-in-time patches..

This is one of the main problems why can't you add methods, classes, modify generics, etc during E&C session: ie. editing your current lambda or current foreach might be OK, but the IDE would be not able to relocate the flows and execute the new code properly. This is a bit similar to why you sometimes see the "stale code" warning, but with those code constructs it is even harder to analyze, and therefore not implemented. And probably will never hit the top of MS's TO-DO list :/

The current boom in .Net/C# is not 'live development' but 'notaliveyet development' heavily supported by modularity and unit testing, where you put the effort to be able to test most of the features of the application off-line.. But that's a paradigm shift and for small projects or for local desktop development sometimes it is simply an overkill.

3
  • You're probably right saying is more something due to my way of using the IDE. Here's a typical scenario: program starts, main winform is shown; I open one dialog form. This form runs code placed in an external library. I'm still using the program, seeing what final user sees, then I notice something to get fixed. Back to IDE, I break all to use edit-and-continue, cursor is on current form .ShowDialog() call. Detaching means loosing edit-and-continue, while breakpoints are ok when you know where to stop. I only know which file I'm currently focus on.
    – ccalboni
    Commented Feb 6, 2013 at 9:26
  • The text got lenghy, so I've included it as edit. Unfortunatelly, I cannot help in any way other than agreeing with you:) Commented Feb 6, 2013 at 10:17
  • Thank you for the extensive explanation. I'm coming from classic ASP, VB6, then VB.NET and now C#: I simply can't program without edit-and-continue, it's so natural and useful to me (not that it have to be correct). Sometimes I found myself not writing a Linq statement just because otherwhise I lose E&C. Recently I started to write unit tests before writing affective code, just inheriting main class abstract methods: in this case E&C is a killer feature.
    – ccalboni
    Commented Feb 6, 2013 at 12:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.