O'Reilly logo

VB.NET Language in a Nutshell, Second Edition by Steven Roman PhD, Paul Lomax, Ron Petrusha

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Name

Application.DoEvents Method

Class

System.Windows.Forms.Application

Syntax

Application.DoEvents(  )

Description

Allows the operating system to process events and messages waiting in the message queue.

For example, you can allow a user to click a Cancel button while a processor- intensive operation is executing. In this case, without DoEvents, the click event is not processed until after the operation had completed. With DoEvents, Windows allocates time for the Cancel button’s Click event to fire and the event handler to execute.

Example

The following example uses a form with two command buttons to illustrate DoEvents. Suppose the user clicks CommandButton1. Then the Do loop in the click event executes indefinitely. However, if the user clicks CommandButton2, its click event is processed when the DoEvents statement in CommandButton1_Click is executed. This sets the Boolean flag to False, which terminates the Do loop.

Option Explicit
Private lngCtr As Long
Private blnFlag As Boolean

Private Sub Button1_Click(ByVal sender As System.Object, _
                          ByVal e As System.EventArgs) _
                          Handles Button1.Click

   blnFlag = True

   Do While blnFlag
      lngCtr = lngCtr + 1
      DoEvents(  )
   Loop
   MsgBox("Loop interrupted after " & lngCtr & _
          " iterations.")
End Sub

Private Sub CommandButton2_Click(  )

   blnFlag = False

End Sub

Programming Tips and Gotchas

  • While DoEvents can be indispensable for increasing the responsiveness of your application, it should at the same time be used judiciously, since it entails an enormous performance ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required