Advance your scripting skills to the next level with TechRepublic’s free Visual Basic newsletter, delivered each Friday. Automatically sign up today!
Developers most commonly use the DoEvents
function to permit a VB6 program to remain responsive to user input (i.e., the
keyboard and mouse) while executing long, repetitive calculations.
It’s processor-intensive to make calls to DoEvents, and calling this function every time a long loop
iterates can really slow things down. Without DoEvents,
however, the program will be “frozen” for the duration of the loop,
which is not a good idea!
The best solution I have come up with is the API function GetInputState().
This function returns a non-zero value if there is a mouse or keyboard event
waiting in the Windows queue for the current thread (in other words, the
current program). Because a call to this function is a lot faster than a call
to DoEvents, you can check its return value and then
call DoEvents only when an input event is waiting.
The following is a code sample that illustrates my point:
For i = 1 To 50000000
If GetInputState <> 0 Then DoEvents
' Iterated calculation code goes here.
You’ll see a noticeable speed improvement with this