Is there 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.
Can this be done?
Breakpoint conditions still need a breakpoint set, and I'd rather set a watch and let Visual Studio set the breakpoints at state changes.
Change the variable into a property and add a breakpoint in the set method. Example:
Update in 2019:
This is now officially supported in Visual Studio 2019 Preview 2 for .Net Core 3.0 or higher. Of course, you may have to put some thoughts in potential risks of using a Preview version of IDE. I imagine in the near future this will be included in the official Visual Studio.
https://blogs.msdn.microsoft.com/visualstudio/2019/02/12/break-when-value-changes-data-breakpoints-for-net-core-in-visual-studio-2019/
If you are using WPF, there is an awesome tool : WPF Inspector.
It attaches itself to a WPF app and display the full tree of controls with the all properties, an it allows you (amongst other things) to break on any property change.
But sadly I didn't find any tool that would allow you to do the same with ANY property or variable.
You can use a memory watchpoint in unmanaged code. Not sure if these are available in managed code though.
As Peter Mortensen wrote:
Additional information:
Obviously, the system must know which address in memory to watch. So - set a normal breakpoint to the initialisation of
myVariable
(ormyClass.m_Variable
) - run the system and wait till it stops at that breakpoint. - Now the Menu entry is enabled, and you can watch the variable by entering&myVariable
, or the instance by entering&myClass.m_Variable
. Now the addresses are well defined.Sorry when I did things wrong by explaining an already given solution. But I could not add a comment, and there has been some comments regarding this.
In the Visual Studio 2005 menu:
Debug -> New Breakpoint -> New Data Breakpoint
Enter: