views:

5067

answers:

5

I've always wondered if there was a way to place a watch on variable and only have visual studio break when that value changes...it would make it so much easier to find tricky state issues.

Does anyone know if this can be done?

EDIT: Breakpoint conditions still need a breakpoint set, I'd rather set a watch and VS set the breakpoints at state changes.

+1  A: 

right click on the breakpoint works fine for me (though mostly using it for conditional breakpoints on specific variable values, even breaking on expressions involving thread name works which is very useful if you're trying to spot threading issues)

Oskar
+1  A: 

I remember the way you described using vb6. In visual studio, the only way i have found so far is by specifying a breakpoint condition..

Gulzar
+5  A: 

In the VS2005 menu:

Debug / New Breakpoint / New Data Breakpoint

enter:

&myVariable
AShelly
is this available for managed code? I see this option disabled for C# project. Remember reading somewhere this is a tough feature to implement in debugging managed apps especially with garbage collector involved.
Gulzar
It's only available for unmanaged code, unfortunately: http://msdn.microsoft.com/en-us/library/350dyxd0.aspx
Josh Kodroff
A: 

You can use a memory watchpoint in unmanaged code. Not sure if these are available in managed code though.

1800 INFORMATION
+3  A: 

You can also choose to break explicitly in code:

// assuming C#
if (condition)
{
  System.Diagnostics.Debugger.Break();
}

From MSDN:

Debugger.Break: If no debugger is attached, users are asked if they want to attach a debugger. If yes, the debugger is started. If a debugger is attached, the debugger is signaled with a user breakpoint event, and the debugger suspends execution of the process just as if a debugger breakpoint had been hit.

This is only a fallback, though. Setting a conditional breakpoint in VS, as described in other comments, is a better choice.

Michael Petrotta