Epson
*

MsgWait for Visual Basic

Here is the MsgWait sample code for use with VB.  To use in your VB application, create a new code module and paste this sample code in the new module.

SPEL Code Example

Option Explicit
Private Const QS_KEY = &H1
Private Const QS_MOUSEMOVE = &H2
Private Const QS_MOUSEBUTTON = &H4
Private Const QS_POSTMESSAGE = &H8
Private Const QS_TIMER = &H10
Private Const QS_PAINT = &H20
Private Const QS_SENDMESSAGE = &H40
Private Const QS_HOTKEY = &H80
Private Const QS_MOUSE = (QS_MOUSEMOVE Or QS_MOUSEBUTTON)
Private Const QS_INPUT = (QS_MOUSE Or QS_KEY)
Private Const QS_ALLEVENTS = (QS_INPUT Or QS_POSTMESSAGE Or QS_TIMER Or QS_PAINT Or QS_HOTKEY)
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)
 
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private Declare Function MsgWaitForMultipleObjects Lib "user32" (ByVal nCount As Long, pHandles As Long, ByVal fWaitAll As Long, ByVal dwMilliseconds As Long, ByVal dwWakeMask As Long) As Long
 
' *********************************************************
' * MsgWait
' *
' * Sleeps for a specified time but allows
' * events to process immediately
' *
' * Input: ms - milliseconds to wait
' * Output: none
' *
' * Notes: Resolution in NT 3.5 and above is 10 ms
' * Copyright (c) 2002, Epson America, Inc. FAR Division
' *********************************************************
Sub MsgWait(ByVal ms As Long)
  Dim start As Long, timeRemaining As Long, timeNow As Long
 
  start = GetTickCount()
  timeRemaining = ms
  Do
    ' Sleep until timeout or event occurs
    MsgWaitForMultipleObjects 0, 0, 0, timeRemaining, QS_ALLINPUT
    timeNow = GetTickCount()
    If timeNow - start >= timeRemaining Then
      Exit Sub
    ElseIf timeNow < start Then
      ' Handle GetTickCount 49.7 day wrap around
      start = timeNow
    End If
    timeRemaining = timeRemaining - (timeNow - start)
    start = timeNow
    DoEvents
  Loop
End Sub