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 ...
Get VB.NET Language in a Nutshell, Second Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.