Application.DoEvents, when it's necessary and when it's not?
Asked Answered
N

3

13

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

Nalor answered 12/7, 2009 at 6:10 Comment(0)
I
18

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.

Icbm answered 12/7, 2009 at 6:25 Comment(3)
Jon, you said that to make sure events get handled periodically...which events? in a sequential programming, suppose we have a button on a form. when user clicks, for example the opacity of the form changes... now, what kind of events are there here except for button's click?Nalor
Repaints, resizes, mouse movements etc. There are an awful lot of events, even if your application code doesn't explicitly handle most of them.Icbm
@JonSkeet I would love your feedback regarding a possible valid use of DoEvents. #45024326Arrowy
B
2

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
Beauchamp answered 15/10, 2012 at 17:10 Comment(0)
T
1

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.

Trisoctahedron answered 12/7, 2009 at 6:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.