使用Visual Studio调试器更改值时中断
时间:2020-03-06 15:00:05 来源:igfitidea点击:
有没有一种方法可以监视变量,并且只有在值更改时才使Visual Studio中断?
这将使发现棘手的状态问题变得容易得多。
能做到吗?
断点条件仍然需要设置断点,而我宁愿设置一个监视并让Visual Studio在状态更改时设置断点。
解决方案
右键单击断点对我来说效果很好(尽管大多数情况下,我将其用于特定变量值的条件断点。即使中断涉及线程名称的表达式也可以工作,如果我们试图发现线程问题,这将非常有用)。
我记得我们使用Visual Basic 6.0描述它的方式。在Visual Studio中,到目前为止,我发现的唯一方法是指定断点条件。
在Visual Studio 2005菜单中:
调试->新断点->新数据断点
进入:
&myVariable
我们可以在非托管代码中使用内存监视点。但是不确定这些在托管代码中是否可用。
我们还可以选择在代码中明确中断:
// Assuming C# if (condition) { System.Diagnostics.Debugger.Break(); }
从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.
不过,这只是一个备用。如其他注释中所述,在Visual Studio中设置条件断点是更好的选择。