How to Doevents for VB.NET, VB 2005 and higher

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 5.00 out of 5)
 

The DoEvents() subroutine is still in VB.NET. It is located under the System.Windows.Forms.Application namespace.

            System.Windows.Forms.Application.DoEvents()

In Visual Basic 2005 and Newer you can access it quicker under the ‘MY’ namespace

            My.Application.DoEvents()

If you plan on using DoEvents() in a continuous loop, I would suggest using the GetInputState() api call.

The GetInputState() API will check whether there are any messages that needs to be processed and THEN it will call DoEvents() to process the messages. As a result, the code will be MANY times faster than if you used DoEvents() by itself.

Add this API call to the Declaration section of your application.

    Private Declare Function GetInputState Lib "user32" () As Integer

GetInputState can let you know if any messages are queued up for processing before you use DoEvents().
If there ARE messages that needs to be processed, THEN call DoEvents() and let the messages go ahead and process.

Basic example from my Snippets page:

Do
 '
 'Checks if any messages are queued up before DoEvents().
 'If there ARE messages that needs to be processed, THEN it will
 'call DoEvents() and let the messages go ahead and process.
 If GetInputState <> 0 Then Application.DoEvents()

Loop Until Me.Text = "Done"

One thought on “How to Doevents for VB.NET, VB 2005 and higher

  1. nick

    I got even better results with select case.
    select case GetInputState
    case 0
    case else
    Application.DoEvents
    end select

    Reply

Leave a Reply