Definition of DoEvents in Visual Basic for Applications

Last reviewed: February 20, 1998
Article ID: Q118468
The information in this article applies to:
  • Microsoft Visual Basic Programming System, Applications Edition, version 1.0
  • Microsoft Excel for Windows, versions 5.0, 5.0c
  • Microsoft Excel for the Macintosh, versions 5.0, 5.0a
  • Microsoft Excel for Windows 95, versions 7.0, 7.0a
  • Microsoft Excel 97 for Windows
  • Microsoft Excel 98 Macintosh Edition

SUMMARY

The DoEvents function surrenders execution of the macro so that the operating system can process other events. The DoEvents function passes control from the application to the operating system. Some instances in which DoEvents may be useful include the following:

  • Hardware I/O
  • Delay Loops
  • Operating System Calls
  • DDE Deadlocking

This article also discusses potential problems associated with the DoEvents function.

MORE INFORMATION

Hardware I/O

If your code waits for an input from any I/O device, the DoEvents function speeds up the application by multitasking. As a result, the computer does not seems to pause or stop responding (hang) while the code is executing.

Example:

   Open "com1" For Input As #1
   Input #1, x
   Do Until x = Chr(13)
   DoEvents
   '...
   '...
   Input #1, x
   Loop

Delay Loops

In a delay loop, the DoEvents function can allow the CPU operating system to continue with any pending jobs.

Example:

   X = Timer()
   Do While X + 10 > Timer()
       DoEvents
   Loop

Operating System Calls

When Visual Basic calls the operating system, the operating system may return the control even before processing the command completely. Doing so may prevent any macro code that depends on an object generated by the call from running. In the example below, the Shell function starts the Microsoft Word application. If Word is not yet open, any effort to establish a DDE link to it will halt the code. By using DoEvents, your procedure makes sure that an operation, such as Shell, is completely executed before the next macro statement is processed.

Example:

   z% = Shell("WinWord Source.Doc",1)
   DoEvents
   ...
   ...

DDE Deadlocking

Consider a situation in which a Visual Basic macro calls an application that is waiting for a second application to get some data. If the macro does not give control to the second application, the result is a deadlock. In DDE conversations between multiple applications, using DoEvents removes the possibility of this type of deadlocking.

Problems Associated with DoEvents

  • Using too many nested DoEvents statements may deplete the stack space and therefore generate an "Out of Stack Space" error message. This error is referring to the application stack space allocated to the Microsoft Excel application.
  • Make sure the procedure that has given up control with DoEvents is not executed again from a different part of your code before the first DoEvents call returns; this can cause unpredictable results.
  • Once DoEvents relinquishes control to the operating system, it is not possible to determine when Microsoft Excel will resume the control. After the operating system obtains control of the processor, it will process all pending events that are currently in the message queue (such as mouse clicks and keystrokes). This may be unsuitable for some real- time data acquisition applications.

REFERENCES

For more information about DoEvents, click the Search button in Help and type:

   doevents


Additional query words: Sendkeys keystroke Wait XL98 XL97 XL7 XL5
Keywords : kbcode kbprg kbusage
Version : 1.00
Platform : WINDOWS


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: February 20, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.