而在调试模式步进GUI不重绘步进、而在、不重、模式

2023-09-06 16:07:11 作者:旧梦难醒

在任何.NET 2.0 WinForms应用程序,如果运行code在调试模式下,你要么打一个断点或单步逐行code线,和你想看到的GUI该应用程序正在调试,然后.NET不会绘制应用程序界面。

例如,我有写一些消息发送到文本框在窗体上的应用程序。当我调试一步的code步骤,或者当一个断点被击中,我希望看到所有的消息都记录在文本框中什么,但如果我preSS使用Alt-Tab从VS2005的窗口切换到的WinForms应用程序窗口,我看到的是白色。形式没有重绘,直到你在VS2005调试模式preSS F5。

,这是什么原因,是有办法解决这个问题,不引入任何线程在code?

解决方案   

,这是什么原因

当你调试,你有效地阻止了UI线程 - 你通过它的执行流程手动步进。 UI线程无法绘制UI,而你从执行停止它。

  GDBFrontend 一款灵活可扩展的GUI调试工具

和有没有办法解决这个

您可以的尝试的调用 Application.DoEvents()手动,但我一般建议反对。

这将是更好的只是等待,直到你得到的方法结束,让UI重绘自身正常。如果你的方法是很长(在时间上),然后记住,当的不是的调试,用户界面​​仍然不能够进行自我更新,而该方法正在执行。这可能会导致你改变你的设计(这是很难从我们得到的时刻信息告诉刚)。

In any .NET 2.0 Winforms application, if you run the code in Debug Mode, and you either hit a breakpoint or otherwise Step Into the code line by line, and you want to see the GUI of the application being debugged, then .NET does not draw the application screen.

For example, I have an application which writes some messages to a TextBox on a Form. When I debug the code step by step, or when a breakpoint is hit, I want to see what all messages are logged in the TextBox, but if I press Alt-Tab to switch from VS2005 window to the WinForms application window, all I see is white color. The form is not redrawn, until you press F5 in the debug mode in VS2005.

What is the reason for this, and is there a way to overcome this, without introducing any threads in the code?

解决方案

What is the reason for this

While you're debugging, you're effectively blocking the UI thread - you're manually stepping through its execution flow. The UI thread can't draw the UI while you're stopping it from executing.

and is there a way to overcome this

You could try calling Application.DoEvents() manually, but I'd generally recommend against it.

It would be better to just wait until you got to the end of the method and let the UI redraw itself normally. If your method is very long (in terms of time) then bear in mind that when not debugging, the UI still wouldn't be able to update itself while that method is executing. This may cause you to change your design (it's hard to tell just from the information we've got at the moment).