Application.DoEvents() 只适用于 WinForms 吗?
Application.DoEvents()
仅适用于表单吗?
我认为此命令用于确保处理之前的所有命令,但现在在阅读 文档,我不再确定了。
Is Application.DoEvents()
just for forms?
I thought that this command was used to ensure that all of the commands before are processed but now after reading the documentation, I'm not sure anymore.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的,它确实是针对 Windows 窗体的。然而,在我看来,应该尽可能避免这种情况。
它通常被开发人员用作黑客攻击,他们不想将长时间运行的操作放在不同的线程上......但这意味着他们引入了很难追踪的重入问题,因为以及在某些时候仍然阻塞 UI 线程(如果其中包括文件操作之类的内容,您无法真正预测该操作是否会足够快地完成而不会产生用户可见的效果)。
Yes, it's really aimed at Windows Forms. However, in my view it should be avoided where possible.
It's usually used as a hack by developers who don't want to be bothered with putting long-running operations on a different thread... but that means they're introducing re-entrancy issues which can be very hard to track down, as well as still blocking the UI thread for some of the time (and if that includes something like a file operation, you can't really predict whether the operation will complete quickly enough to not have a user visible effect).
没有WinForms,就没有标准的事件队列。 (嗯,WPF 中有一个事件队列,但这只是另一个框架)。
Without WinForms, there is no standard event queue. (Well, there is an event queue in WPF, but this is just another framework).
如果您想要实现的目标是等待应用程序外部发生某些事情(例如,将文件拖放到某个目录中),则可能的解决方法是 的 Timer 类System.Timers 命名空间。
示例(基于 MSDN):
MSDN 上的更多信息:http://msdn.microsoft.com/en-us/library/system.timers.timer%28v=vs.71%29.aspx
If what you are trying to achieve is waiting for something to happen outside your application (eg. a file to be dropped in a certain directory), a possible workaround would be the Timer class of the System.Timers namespace.
An example (based on MSDN):
More info at MSDN: http://msdn.microsoft.com/en-us/library/system.timers.timer%28v=vs.71%29.aspx
是的,它仅适用于 Windows 窗体。这在控制台或 ASP.NET 应用程序中没有意义,因为没有消息循环。可以使用调度程序在 WPF 中执行此操作,如下所示
Yes, it's only for Windows Forms. It wouldn't make sense in a console or ASP.NET application, because there is no message loop. It is possible to do it in WPF, using the dispatcher, as shown here. Anyway, I wouldn't recommend using
DoEvents
except perhaps in a quick and dirty application, for the reasons explained by Jon.