Application.DoEvents,什么时候需要,什么时候不需要?

发布于 2024-07-27 05:04:48 字数 60 浏览 4 评论 0原文

使用 Application.DoEvents 的必要性是什么?何时应该使用它?

What is the necessity of using Application.DoEvents and when we should use it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

无边思念无边月 2024-08-03 05:04:48

Application.DoEvents 通常用于确保在 UI 线程上执行某些长时间运行的操作时定期处理事件。

更好的解决方案就是不这样做。 在单独的线程上执行长时间运行的操作,并在需要时编组到 UI 线程(使用 Control.BeginInvoke/Invoke 或使用 BackgroundWorker)更新用户界面。

Application.DoEvents 引入了重入的可能性,这可能会导致非常难以理解的错误。

Application.DoEvents is usually used to make sure that events get handled periodicaly when you're performing some long-running operation on the UI thread.

A better solution is just not to do that. Perform long-running operations on separate threads, marshalling to the UI thread (either using Control.BeginInvoke/Invoke or with BackgroundWorker) when you need to update the UI.

Application.DoEvents introduces the possibility of re-entrancy, which can lead to very hard-to-understand bugs.

夏末的微笑 2024-08-03 05:04:48

Windows 维护一个队列来保存各种事件,如单击、调整大小、关闭等。当控件响应事件时,所有其他事件都将保留在队列中。 因此,如果您的应用程序花费过长的时间来处理按钮单击,则应用程序的其余部分似乎会冻结。 因此,您的应用程序在执行一些繁重的处理以响应事件时可能会显得无响应。 虽然理想情况下您应该以异步方式进行繁重的处理以确保 UI 不会冻结,但快速而简单的解决方案是定期调用 Application.DoEvents() 以允许将挂起的事件发送到您的应用程序。

对于良好的 Windows 应用程序,最终用户不喜欢任何形式的应用程序在执行较大/重量级操作时冻结。 用户总是希望应用程序能够平稳且响应迅速地运行,而不是冻结 UI。 但经过谷歌搜索后,我发现 Application.DoEvents() 并不是在应用程序中更频繁地使用的好习惯,因此最好使用 BackGround Worker Thread 来执行长时间运行的任务而不冻结窗口。

如果你实际观察一下,你会得到更好的想法。 只需复制以下代码并检查应用程序是否放置 Application.DoEvents()。

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        For i As Integer = 0 To 1000
            System.Threading.Thread.Sleep(100)
            ListBox1.Items.Add(i.ToString())
            Application.DoEvents()
        Next
    End Sub

Windows maintains a queue to hold various events like click, resize, close, etc. While a control is responding to an event, all other events are held back in the queue. So if your application is taking unduly long to process a button-click, rest of the application would appear to freeze. Consequently it is possible that your application appears unresponsive while it is doing some heavy processing in response to an event. While you should ideally do heavy processing in an asynchronous manner to ensure that the UI doesn’t freeze, a quick and easy solution is to just call Application.DoEvents() periodically to allow pending events to be sent to your application.

For good windows application, end user doesn’t like when any form of application are freezing out while performing larger/heavyweight operation. User always wants application run smoothly and in responsive manner rather than freezing UI. But after googling i found that Application.DoEvents() is not a good practice to use in application more frequently so instead this events it’s better to use BackGround Worker Thread for performing long running task without freezing windows.

You can get better idea if you practically look it. Just copy following code and check application with and without putting Application.DoEvents().

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        For i As Integer = 0 To 1000
            System.Threading.Thread.Sleep(100)
            ListBox1.Items.Add(i.ToString())
            Application.DoEvents()
        Next
    End Sub
夏日浅笑〃 2024-08-03 05:04:48

恕我直言,你应该少用它,因为你可能会出现非常意想不到的行为。
刚刚生成的代码就ok了。 像您正在再次执行当前所在的事件处理程序之类的事情,因为用户按了两次键等等。
如果要刷新控件以显示当前进程,您应该在该控件上显式调用 .Update,而不是调用 Application.DoEvents。

Imho you should more less never use it, as you might end up with very unexpected behavior.
Just generated code is ok. Things like you are executing again the event handler you are currently in,because the user pressed a key twice etc etc.
If you want to refresh a control to display the current process you should explicitly call .Update on that control in instead of calling Application.DoEvents.

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