Monday, April 21, 2008

DoEvents in VB.NET

I am not sure why this is so difficult to find, but if you need to do the equivalent to a VB6 DoEvents in VB.NET or even in C#:

System.Windows.Forms.Application.DoEvents()

or if you are in a form's code, it is simply:
Application.DoEvents()

4 comments:

Jeff Gonzalez said...

Just FYI in Visual Basic 2008 express edition in a console application this:

System.Windows.Forms.Application.DoEvents()

gives the error - 'Windows' is not a member of 'System'

srego said...

I believe that the problem here is that the Console app is not a Form and DoEvents is part of Sytem.Windows.Forms. The console app isn't event driven, so that is probably why this method is not supported there. However, if you are wanting to have other events processed in other applications that are running, then you may be able to say something like:

System.Threading.Thread.Sleep(10)

and it might accomplish what you are trying to do since while the console app is sleeping, the other apps should get their slice of the processing time more easily.

Daniel Wilson said...

You should be able to add a reference to System.Windows.Forms even in a Console app, and then call System.Windows.Forms.Application.DoEvents()

Med Toledo said...

It Worked fine for me, just make sure you add a reference to System.Windows.Forms in your project properties and at the beggining of the code add:

Imports System.Windows.Forms

This was posted before just wanted to elaborate a little more.

Thanks for the info, I had spent hours looking for this.
Med Toledo