views:

116

answers:

3

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.)

A: 

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.

sharptooth
+3  A: 

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.

Brian Rasmussen
This is very useful -- I use this technique a lot (though it can really slow down the app)
the_mandrill
+13  A: 

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".

AMissico
+1 wow! great answer!
Ofek Shilon
The value of ForegroundLockTimeout in my registry on Windows 7 is 0x30d40 (200000) already and yet I see VStudio stealing focus when a breakpoint is hit.
Ashwin