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