Monday, March 9, 2009

DoEvents vs Sleep

Sometimes when you want to delay things you might call a DoEvents.  However I've ocassionally come across situations where this can really slow down an application.  Sleep is actually called by DoEvents, so it might be better to call Sleep instead.  Call DoEvents if you have a long running routine or loop and you don't want to tie up the operating system.  Thats my take on it. 

This forum has more on the topic.
http://www.vbforums.com/showthread.php?t=344709

And it has an interesting API call where you check the status to see if there are items waiting in the Queue

  1. Option Explicit
  2.  
  3. Private Const QS_HOTKEY = &H80
  4. Private Const QS_KEY = &H1
  5. Private Const QS_MOUSEBUTTON = &H4
  6. Private Const QS_MOUSEMOVE = &H2
  7. Private Const QS_PAINT = &H20
  8. Private Const QS_POSTMESSAGE = &H8
  9. Private Const QS_SENDMESSAGE = &H40
  10. Private Const QS_TIMER = &H10
  11. Private Const QS_ALLINPUT = (QS_SENDMESSAGE Or QS_PAINT Or QS_TIMER Or QS_POSTMESSAGE Or QS_MOUSEBUTTON Or QS_MOUSEMOVE Or QS_HOTKEY Or QS_KEY)
  12.  
  13. Private Declare Function GetQueueStatus Lib "user32" (ByVal fuFlags As Long) As Long
  14.  
  15. Private Sub Form_Click()
  16.     Dim lngDoEventCnt As Long
  17.     Dim lngLoopCount As Long
  18.    
  19.     Do
  20.         If GetQueueStatus(QS_ALLINPUT) <> 0 Then
  21.             lngDoEventCnt = lngDoEventCnt + 1
  22.             DoEvents
  23.         End If
  24.        
  25.         lngLoopCount = lngLoopCount + 1
  26.     Loop Until lngLoopCount = 2000000
  27.    
  28.     Debug.Print lngDoEventCnt
  29. End Sub


Another post from the forum is below:

HOWTO: Determine the Differences Between DoEvents and Sleep
Q158175


SUMMARY
This article explains the differences between the Visual Basic DoEvents function and
the Sleep() Windows API function.

MORE INFORMATION
DoEvents is a Visual Basic function that yields execution so the operating system can
process other events. This function cleans out the message loop and executes any other
pending business in the Visual Basic runtime. Upon completing the pending business
execution, the function calls the Sleep function with zero (0) as the argument so that
the remaining time slice can be used to check the queue.

The Sleep 32-bit API function is a subset of the DoEvents function. The Visual Basic
program calling the function and the Visual Basic runtime executable and interactions
with Windows are immediately put to sleep by this function. The programs remain inactive
for the time in milliseconds specified in the Sleep argument.

The Sleep function allows you to specify the amount of time your applications are
inactive. The DoEvents function returns control to the Visual Basic program after
the operating system has finished processing the events in its queue and all keys in
the SendKeys queue have been sent.

The information in this article applies to:
Microsoft Visual Basic Learning Edition for Windows 5.0
Microsoft Visual Basic Learning Edition for Windows 6.0
Microsoft Visual Basic Professional Edition for Windows 5.0
Microsoft Visual Basic Professional Edition for Windows 6.0
Microsoft Visual Basic Enterprise Edition for Windows 5.0
Microsoft Visual Basic Enterprise Edition for Windows 6.0
Microsoft Visual Basic Standard Edition, 32-bit, for Windows 4.0
Microsoft Visual Basic Professional Edition, 32-bit, for Windows 4.0
Microsoft Visual Basic Enterprise Edition, 32-bit, for Windows 4.0
Last Reviewed: 5/13/2003 (3.0)
Keywords: kbhowto KB158175


No comments: