Visual Studio: How to stop breakpoint hit from stealing focus?

Visual StudioDebuggingFocusBreakpoints

Visual Studio Problem Overview


When a breakpoint is hit in Visual Studio, it steals the focus from whatever other application the programmer is viewing/typing into at that moment. This can be very irritating since VS grabs any keyboard input the programmer was typing into the other application at that moment and takes that input as its own.

What are the tricks you folks use to prevent this focus steal?

(I face this on Visual C++ 2008 and 2010. I am guessing it is a problem for Visual Studio in general and for all recent versions.)

Visual Studio Solutions


Solution 1 - Visual Studio

This is finally fixed in VS2019. Go to Tools->Options->Debugging->General, down at the bottom is "Bring Visual Studio to the foreground when breaking in the debugger."
Just de-select it and you will no longer be interrupted while multitasking.

enter image description here

Solution 2 - Visual Studio

This is a registry setting. See ForegroundLockTimeout at http://technet.microsoft.com/en-us/library/cc957208.aspx. Zero allows applications to steal focus. TweakUI sets this value to 200000 when "Prevent applications from stealing focus" is checked.

For more control, download the Tweak UI utility of Powertoys for Windows XP. In the "General" tab, select "Focus" and check "Prevent applications from stealing focus".

> The strange thing is, there are > provisions built into the operating > system to protect us from badly > written, focus stealing applications. > The ForegroundLockTimeout registry > setting is expressly designed to > prevent applications from stealing > focus from the user. The OS silently > converts that inappropriate focus > stealing behavior into friendlier, > less invasive taskbar button flashing, > which is the subject of the > ForegroundFlashCount registry setting.

Solution 3 - Visual Studio

Right click the breakpoint and select When hit ... this will allow you to run a function when the breakpoint is hit. You can use this to print status messages to the output window. You application will keep focus.

Solution 4 - Visual Studio

By accident I discovered a workaround, which I've been using for a few years now and while I haven't tested it in 2008 and 2010, it certainly works in 2013, '15 & '17 and at least Windows 7 & 10.

It relies on the fact that Visual Studio will not steal focus if another Visual Studio instance is paused in execution. Obviously the only thing as special as VS is another VS. :-/

Open a second instance of VS with a simple project. Pause the execution of the project anyway you like (e.g. put a breakpoint on the first line of execution and debug), you can then simply minimise that VS and none of the VS instances you're actually using will steal focus.

This is is obviously a heavy weight solution, but if you have ample RAM (the CPU usage of the idle VS doesn't even register for me), it works well. I haven't tried it with inter-version instances (e.g. pausing in '13 to make '17 behave), but if that works you'll probably want to opt to use the older version instance as your "dummy" VS to cut down on resource use.

Solution 5 - Visual Studio

One workaround is to use OutputDebugString() function to output current state into the debugger output window. You just place Visual Studio in background, position the debugged program window so that the "Output" window is visible - and no focus transition ever happens.

You will perhaps want to use macros for conditional compilation so that tracing code is not included into the release builds.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAshwin NanjappaView Question on Stackoverflow
Solution 1 - Visual StudioDaniel WilliamsView Answer on Stackoverflow
Solution 2 - Visual StudioAMissicoView Answer on Stackoverflow
Solution 3 - Visual StudioBrian RasmussenView Answer on Stackoverflow
Solution 4 - Visual StudioJoolsView Answer on Stackoverflow
Solution 5 - Visual StudiosharptoothView Answer on Stackoverflow