visual-studio 在 VisualStudio 中在运行时查看变量值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3288681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 12:12:56  来源:igfitidea点击:

Viewing variable values at runtime in VisualStudio

c#visual-studiodebugging

提问by Josh

Is there any tool I can use to view the values of variables live as the code executes in VS?

当代码在 VS 中执行时,我可以使用任何工具来实时查看变量的值吗?

Right now I can see them only when I keep a breakpoint.But,the problem is that the code works perfectly fine when I keep a breakpoint.it messes up only when it runs fast.

现在我只有在保持断点时才能看到它们。但是,问题是当我保持断点时代码工作得很好。它只有在快速运行时才会混乱。

Any help would be appreciated.

任何帮助,将不胜感激。

Thanks

谢谢

回答by djdd87

You can only view variables when you have a breakpoint, however you could just manually write the variable values to the Visual Studios Output window:

您只能在有断点时查看变量,但是您可以手动将变量值写入 Visual Studios 输出窗口:

System.Diagnostics.Debug.WriteLine(variable);

回答by Toan Nguyen

I my opinion , rather than setting break points, you can use Debug.Write(yourVariable)under the Debug mode, so you can watch the value in the output windows.

我认为,与其设置断点,不如Debug.Write(yourVariable)在调试模式下使用,这样你就可以在输出窗口中观察值。

Cheers.

干杯。

回答by Adam Houldsworth

Sounds like you need a conditional breakpoint. Aside from printing the values (console, debug output, trace) as the code runs there is nothing that will show you live data slow enough for you to see it. Put a conditional statement in detecting when the values are no longer valid and stick a programmatic break point on that.

听起来您需要一个条件断点。除了在代码运行时打印值(控制台、调试输出、跟踪)之外,没有任何东西可以向您显示足够慢的实时数据以供您查看。使用条件语句检测值何时不再有效,并在其上设置程序断点。

回答by Kangkan

Use Debug.Writeto print out the variables. You can also use profiler.

Debug.Write打印出的变量。您还可以使用分析器。

回答by Willem van Rumpt

All the aforementioned methods (dumping to console, using Debug.Write, custom logging, etc. etc.) to dump the contents of variables will do the trick .

所有上述方法(转储到控制台、使用 Debug.Write、自定义日志记录等)都可以转储变量的内容。

From your problem description however (i.e. "...works fine with breakpoints, fails when left alone to run..."), it sounds like you have a threaded scenario with synchronization issues. If that's the case, inspecting the synchronization methods used might yield better results.

然而,从您的问题描述(即“...在断点上工作正常,在单独运行时失败...”),听起来您有一个同步问题的线程场景。如果是这种情况,检查使用的同步方法可能会产生更好的结果。

回答by noti

In visual studio 2010 (maybe in earlier versions, I didn't check) the conditional breakpoint can be set to print a variable value and continue running. It is done by right clicking the breakpoint and choosing 'When hit'. Then it opens a dialog in which you can specify what and how to print. It worked fine for me in a native C project.

在visual studio 2010中(可能在早期版本中,我没有检查)可以设置条件断点以打印变量值并继续运行。这是通过右键单击断点并选择“命中时”来完成的。然后它会打开一个对话框,您可以在其中指定打印内容和方式。它在本机 C 项目中对我来说效果很好。