Search Tools Links Login

DoEvents evolution; the API approach. (Method for 100% optimized loops)


Visual Basic 6, or VB Classic

Do you want to make your loops 100% faster? Here's how :
Many of us have used several times DoEvents, to supply a bit of air to our App, on Heavy-Duty times such as loops for updates or inserts on recordsets etc. As we most know, DoEvents processes Windows messages currently in the message queue. But what if we wanted to execute DoEvents only in times, when we want to allow user (Keyboard and Mouse) input? ( A special "thank you" to all of you who rated this article)

Original Author: John Galanopoulos

Code







New Page 1


DoEvents
evolution; the API approach


?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á


If there was such a function to
inspect the message queue for user input, we would have a main benefit:


?á?á?á?á?á?á
We would speed up our loops ÔÇÿcause we would process all the messages in
the queue (with DoEvents) only on user input. It's faster to check for
a message than to process all messages every time.


API provides us with such a
function:


It's called
GetInputState and you can locate it in user32 library.


?áHere is the declaration:


?á?á?á
Public Declare Function GetInputState Lib
"user32" () As Long


The GetInputState function
determines whether there are mouse-button or keyboard messages in the calling
thread's message queue.


If the queue contains one or more new mouse-button or
keyboard messages, the return value is nonzero else if there are no new
mouse-button or keyboard messages in the queue, the return value is zero.


So we can create an improved DoEvents with a Subroutine
like this :


Public Sub newDoEvents()


?á?á?á?á?á?á?á
If GetInputState() <> 0 then DoEvents


End Sub


?áYou
can use GetInputState() with many variations for example :?á?á?á?á?á?á?á?á?á?á?á


?á?á?á?á?á?á?á?á
uCancelMode = False


?á?á?á?á?á?á?á?á?á
?á?á?á?á?á?á?á
Do until rs.Eof


?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á
Rs.AddNew?á


?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á
(..your source here)


?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á
Rs.Update


?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á
Rs.MoveNext


?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á
If GetInputState() <> 0 then


?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á
DoEvents


?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á
If uCancelMode Then Exit Do


?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á
End If


?á?á?á?á?á?á
?á?á ?á?á?á?á?á?á?á?á?á?á


?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á
Loop


?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á



?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á
?á?á?á?á?áMsgbox ÔÇ£Finished.ÔÇØ





or
we could use it in a ScreenSaver e.t.c.


Let's
go a little further now and see what exactly is behind GetInputState().


It
is another API function located in User32 as well; GetQueueStatus()


The GetQueueStatus function indicates the type of messages
found in the calling thread's message queue. Here are the flags that
GetQueueStatus uses :


QS_ALLEVENTS?á?á?á?á?á?á?á?á?á?á?á?á An
input, WM_TIMER, WM_PAINT,WM_HOTKEY, or posted message is in the queue.


QS_ALLINPUT ?á?á?á?á?á?á?á?á?á
?á?á?á?áAny
message is in the queue.


QS_ALLPOSTMESSAGE?á?á?á
A posted message (other than those listed here) is in the queue.


QS_HOTKEY?á?á?á
?á?á?á?á?á?á?á?á?á
?á?á?á?áA
WM_HOTKEY message is in the queue.


QS_INPUT ?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á
An input message is in the queue.


QS_KEY ?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á
?á?á?á?áA WM_KEYUP, WM_KEYDOWN,WM_SYSKEYUP, or WM_SYSKEYDOWN?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á
?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á
message is in the queue.


QS_MOUSE ?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á
A WM_MOUSEMOVE message or mouse-buttonmessage (WM_LBUTTONUP, WM_RBUTTONDOWN, ?á?á?á?á?á?á?á?á?á ?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á
?á?á?á?áand so on).


QS_MOUSEBUTTON ?á?á?á?á?á
A mouse-button message (WM_LBUTTONUP, WM_RBUTTONDOWN, and so on).


QS_MOUSEMOVE?á?á?á?á?á?á?á?á?á
A WM_MOUSEMOVE message is in the queue.


QS_PAINT ?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á
A WM_PAINT message is in the queue.


QS_POSTMESSAGE ?á?á?á?á?á
A posted message (other than those listed here)
is in the queue.


QS_SENDMESSAGE ?á?á?á?á?á
A message sent by another thread or application
is in the queue.


QS_TIMER ?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á
A WM_TIMER message is in the queue.





?á?á?á?á?á?á?á?á?á
(I believe that GetInputState() is a GetQueueStatus(QS_HOTKEY Or QS_KEY Or
QS_MOUSEBUTTON))



With these constants you can create your own
GetInputState function that fits your needs. For example you can create a custom
function that issues DoEvents when it'll detects not only a Keyboard or Mouse

Key input, but also a WM_PAINT signal.


Why's that? ÔÇÿcause in your loop you might need
to update the screen so you must let your custom function process the specific
signal.


Look at this :





Public Const QS_HOTKEY = &H80


Public Const QS_KEY = &H1


Public Const QS_MOUSEBUTTON = &H4


Public Const QS_MOUSEMOVE = &H2


Public Const QS_PAINT = &H20


Public Const QS_POSTMESSAGE = &H8


Public Const QS_SENDMESSAGE = &H40



Public Const QS_TIMER = &H10


Public Const QS_ALLINPUT = (QS_SENDMESSAGE
Or QS_PAINT OrQS_TIMER Or
QS_POSTMESSAGE Or?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á
?á?á?á?á?á?á?á?á?á?á?á?á?á
?á?á?áQS_MOUSEBUTTON Or QS_MOUSEMOVE Or QS_HOTKEY Or
QS_KEY)


Public Const QS_MOUSE = (QS_MOUSEMOVE
Or QS_MOUSEBUTTON)


Public Const QS_INPUT = (QS_MOUSE Or
QS_KEY)


Public Const QS_ALLEVENTS = (QS_INPUT
Or QS_POSTMESSAGE OrQS_TIMER Or
QS_PAINT Or QS_HOTKEY)





Public Declare Function GetQueueStatus
Lib "user32" (ByVal qsFlags As Long) As Long








Public Function cGetInputState()


Dim
qsRet As Long


qsRet = GetQueueStatus(QS_HOTKEY Or
QS_KEY Or QS_MOUSEBUTTON Or QS_PAINT)


?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á
cGetInputState = qsRet


End Function





With this function you can trigger the DoEvents to
be executed only when the message queue contains Key input, Mouse button or a
WM_PAINT signal.





Call it like this.


. . if cGetInputState() <> 0
then DoEvents





?á?á?á?á?á?á?á?á?á This was
tested and proved to optimise a loopby
100% !!!!!!!!!





I wrote this article believing that the API is a
powerfull part on Windows programming and deserves your attention. I was stuck
several times and API prooved to be a problem solver. API is a large world but
with little effort, you can take advantage of it. You will create more
sophisticated and user aware programs.





I hope I helped.


Any comments or suggestions are always welcomed.


?á?á?á?á?á?á?á?á
John
Galanopoulos


?á?á?á?á?á?á?á?á?á?á
(Below there is a link to the .doc version of this article, for you to download.
If you want to implement this source in your projects, download the Class Module posted by John Baughman in this address
Also, you can check out Olav Jordan's article : Optimized loop (no more doevents)




About this post

Posted: 2002-06-01
By: ArchiveBot
Viewed: 101 times

Categories

Visual Basic 6

Attachments

DoEvents_e4315712202001.zip
Posted: 9/3/2020 3:45:00 PM
Size: 7,292 bytes


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.