spoulson
spoulson

Reputation: 21591

Can VS be made to eval a debug watch even while the application is still running?

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.

Upvotes: 2

Views: 137

Answers (3)

JaredPar
JaredPar

Reputation: 754555

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.

Upvotes: 0

Hans Passant
Hans Passant

Reputation: 941277

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.

Upvotes: 1

Eric
Eric

Reputation: 1472

I would do that using compiler directives.

#if DEBUG
     Debug.WriteLine
#end if

Upvotes: 0

Related Questions