Is Application.DoEvents () only for WinForms?

Is Application.DoEvents() only for forms?

I thought this command was used to ensure that all the commands were processed, but now after reading the documentation , I'm not sure.

+7
source share
4 answers

Yes, it really targets Windows Forms. However, in my opinion, it should be avoided when possible.

It is usually used as a hack for developers who don’t want to worry about putting long operations on another thread ... but this means that they introduce reconnection problems, which can be very difficult to track down, as well as still blocking the user interface thread for some time (and if this includes something like a file operation, you cannot predict whether the operation will complete fast enough so as not to have a visible user effect).

+14
source

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

+3
source

If what you are trying to achieve expects something to happen outside your application (for example, a file that needs to be deleted in a specific directory), a possible workaround would be the Timer class of the System.Timers namespace.

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 

Additional information on MSDN: http://msdn.microsoft.com/en-us/library/system.timers.timer%28v=vs.71%29.aspx

+3
source

Yes, this is for Windows Forms only. This does not make sense in a console or ASP.NET application because there is no message loop. This can be done in WPF using the dispatcher, as shown here . In any case, I would not recommend using DoEvents with the possible exception of a quick and dirty application, for reasons explained by John.

+2
source

All Articles