Application.DoEvents() 只适用于 WinForms 吗?

发布于 2024-10-17 11:30:55 字数 213 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

柏拉图鍀咏恒 2024-10-24 11:30:55

是的,它确实是针对 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).

战皆罪 2024-10-24 11:30:55

没有WinForms,就没有标准的事件队列。 (嗯,WPF 中有一个事件队列,但这只是另一个框架)。

Without WinForms, there is no standard event queue. (Well, there is an event queue in WPF, but this is just another framework).

孤蝉 2024-10-24 11:30:55

如果您想要实现的目标是等待应用程序外部发生某些事情(例如,将文件拖放到某个目录中),则可能的解决方法是 Timer 类System.Timers 命名空间。

示例(基于 MSDN):

Private Sub SetTimer()
    Dim aTimer As New System.Timers.Timer
    AddHandler aTimer.Elapsed, AddressOf OnTimedEvent
    aTimer.Interval = 5000
    aTimer.Enabled = True

    Console.WriteLine("Press q to exit")
    While Console.Read <> Asc("q")
    End While
End Sub

Private Sub OnTimedEvent(ByVal source As Object, ByVal e As ElapsedEventArgs)
    'Do the job here
    Console.WriteLine("HELLO WORLD!")
    'Don't forget to disable the timer if you don't need it anymore
    'Source.Enabled = False
End Sub

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):

Private Sub SetTimer()
    Dim aTimer As New System.Timers.Timer
    AddHandler aTimer.Elapsed, AddressOf OnTimedEvent
    aTimer.Interval = 5000
    aTimer.Enabled = True

    Console.WriteLine("Press q to exit")
    While Console.Read <> Asc("q")
    End While
End Sub

Private Sub OnTimedEvent(ByVal source As Object, ByVal e As ElapsedEventArgs)
    'Do the job here
    Console.WriteLine("HELLO WORLD!")
    'Don't forget to disable the timer if you don't need it anymore
    'Source.Enabled = False
End Sub

More info at MSDN: http://msdn.microsoft.com/en-us/library/system.timers.timer%28v=vs.71%29.aspx

梦境 2024-10-24 11:30:55

是的,它仅适用于 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文