Back

Topic

[KB321]VBA function DoEvent

Tags: Scripting, VBA

14 years ago
By RM
Options
Print
Applies to:

PcVue all versions.


Summary:

This article describes how to use the VBA function DoEvents and warns about the possible problems that can occur when using it.


Details:

The DoEvents function pauses the execution of a procedure so that the OS can process other events. The procedure execution resumes when the OS has finished processing the events.

This function returns 0 in PcVue applications.

 

Code sample

Dim bFunctionExecuted as Boolean

Sub Init()
  bFunctionExecuted = False
End sub

Sub FunctionWaiting()
  While bFunctionExecuted = False
    DoEvents
  Wend
End Sub

Sub Function2()
  bFunctionExecuted = True
End sub

The function FunctionWaiting is executed.

As long as the variable bFunctionExecuted = False, the code in the While loop instruction is executed repeatedly. Meanwhile, the execution of other functions is possible because of the DoEvents instruction.

If function Function2 is executed, the While loop is interrupted and the rest of the code of the FunctionWaiting function is executed.

Caution

1. Warning from the VBA help – Any time you temporarily yield the processor within an event procedure, make sure the procedure is not executed again from a different part of your code before the first call returns; this could cause unpredictable results.

2. In PcVue – If this syntax is used during the opening of a mimic (event procedure Mimic_Open), PcVue will load the mimic but won’t activate (display) it as long as the execution of the Mimic_Open function is not finished. If a second command to open the mimic occurs while the DoEvents instruction is executed from the first mimic open command, PcVue will display it. A problem occurs when the mimic is closed while the DoEvents instruction is still executed from the first mimic open command: The Mimic_Open function execution is interrupted and the OS, unaware that the mimic is closed, tries to finish the initialization of the mimic window. Consequently PcVue crashes.


Created on: 23 Dec 2010 Last update: 13 May 2024