views:

25

answers:

3

Normally in Visual Studio, a watch cannot be evaluated unless the debugger is stopped at a breakpoint. Is there a trick or add-on to make Visual Studio evaluate a watch while the application is still running? For example, evaluate the watch every time execution passes a point in the code while it's still running and without changing the code to insert statements like Debug.WriteLine.

Not sure this is possible, but I thought I'd ask.

A: 

I would do that using compiler directives.

#if DEBUG
     Debug.WriteLine
#end if
Eric
The idea is to not change code. I don't wish to recompile with new debug statements every time I find a new method I want to debug. Moreover, I'd have to rollback those edits since I can't check them into source control in that form.
spoulson
+1  A: 

Yes, this is possible. Set a breakpoint at the location where you'd want to see the value. Right-click the breakpoint and choose "When Hit...". Tick "Print a message" and write an expression like { value }. The message is displayed in the Output window while your program runs.

Hans Passant
Even in this mode the application is not running when the value is printed out. The application is paused, the value evaluated, and then the application continues running again.
JaredPar
Hmya, threads are paused all the time. Not sure if it makes a difference if the scheduler does it or the debugger. Not one you'd easily notice anyway, other than that it runs slower.
Hans Passant
I was hoping "When hit" would allow for watches to be evaluated, but this is as close as it gets.
spoulson
A: 

No this is not possible to do. The evaluation feature in Visual Studio is a stack frame based mechanism. That is that every evaluation is done in the context of a given stack frame (as viewed through the stack window). When the program is running the set of stack frames is currently changing and hence it's not possible to do a stable evaluation.

Additionally there are other limitations in the CLR which prevent this from managed code. It's not possible for instance to execute a function unless the debugee process is in a very specific state.

JaredPar