7

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.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Jaime Oro
  • 9,899
  • 8
  • 31
  • 39
  • 4
    You really shouldn't ever need to use `Application.DoEvents`. What problem are you trying to solve? Perhaps someone can suggest a better way of doing so. – Cody Gray - on strike Feb 12 '11 at 11:12
  • It’s not just `DoEvents`. It’s the entire class. That’s why it’s `System.Windows.Forms.Application`... – Timwi Feb 12 '11 at 11:54

4 Answers4

15

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

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    +1000, Not just in your view. There is really no reason to use it at all. – Alastair Pitts Feb 12 '11 at 11:14
  • `DoEvents` can be useful for implementing a blocking operation like `MessageBox`. – Vlad Feb 13 '11 at 13:56
  • @Vlad: I believe it's better to just disable the rest of the UI input. Otherwise your DoEvents can still mean that the rest of the UI starts to respond when you don't want it to. – Jon Skeet Feb 13 '11 at 14:00
  • @Jon: I basically meant an arbitrary potentially slow function, which has to be blocking (e.g., has to produce an output value). The need for such functions (and not the design with StartCalculation/OnFinish event/callback) is somehow justified by the existence of `MessageBox.Show`. – Vlad Feb 13 '11 at 14:13
  • @Vlad: If you have an arbitrary potentially slow function, you simply shouldn't be running it on the UI thread. Run it in a background thread, and update the UI appropriately. – Jon Skeet Feb 13 '11 at 14:17
  • @Jon: I know, I meant exactly that by StartCalculation/OnFinish design. But MessageBox.Show doesn't run in the background thread, so there might be a possibility that someone would need some other functionality modelled on MessageBox.Show. And that in my opinion is where DoEvents can be useful. – Vlad Feb 13 '11 at 14:21
  • 1
    @Jon: looking back from the 1 year's perspective: now I believe that `DoEvents` is completely evil. :) – Vlad Aug 04 '12 at 12:59
  • @Vlad: Excellent - glad to hear it ) – Jon Skeet Aug 04 '12 at 14:56
3

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

show
  • 69
  • 1
  • 4
3

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

Vlad
  • 35,022
  • 6
  • 77
  • 199
  • @Cody: It means that a C# program is not necessarily a WinForms or WPF program. WPF is a framework built on the top of C# (and so is WinForms). The event queues will be available only if you use some of the frameworks, e.g., WinForms or WPF, but not per default. – Vlad Feb 12 '11 at 11:21
  • Sure. But isn't ASP.NET (for example) *also* a framework? `DoEvents` doesn't make much sense there. – Cody Gray - on strike Feb 12 '11 at 11:23
  • @Cody: I have no experience with ASP.NET, but I would assume it is. Otherwise event queues would be available in console applications as well. – Vlad Feb 12 '11 at 11:31
  • @Cody: it's not necessary that a framework _brings in_ the event queue. However, as the event queue is not something built-in into the .NET, the event queue must be introduced by some framework. (Note that the .NET _itself_ is called a framework, making the meaning of this term somewhat ambiguous.) – Vlad Feb 12 '11 at 12:35
2

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.

Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758