[API]
FILETIME
CreateWaitableTimerA
CreateWaitableTimerW
GetTickCount
SetWaitableTimer
MsgWaitForMultipleObjects
CloseHandle
Sleep

[DEFINE]
Private Const SleepValueLow As Long = 25&
Private DoEventsEx_Sleep_WaitableTimers As Boolean
'see also consts in DoEventsEx

[CODE]
Public Sub DoEventsEx_Sleep(ByVal IntervalMS As Long)
  Dim xType1 As FILETIME
  Dim xLong1 As Long
  Dim xLong2 As Long

  If DoEventsEx_Sleep_WaitableTimers Then
    If IsUnicode Then
      xLong1 = CreateWaitableTimerW(0, True, 0&)
      Else
      xLong1 = CreateWaitableTimerA(0, True, 0&)
      End If
    If xLong1 = 0& Then GoTo CustomTimerW9x
    xType1.dwLowDateTime = -1&
    xType1.dwHighDateTime = -1&
    Call SetWaitableTimer(xLong1, xType1, 0&, 0&, 0&, 0&)
    xType1.dwLowDateTime = IntervalMS * -10000&
    Call SetWaitableTimer(xLong1, xType1, 0&, 0&, 0&, False)
    Do Until MsgWaitForMultipleObjects(1&, xLong1, False, -1&, 255&) = 0&
      DoEvents
      Loop
    DoEvents
    Call CloseHandle(xLong1)
    Else
CustomTimerW9x:
    xLong1 = GetTickCount And &H3FFFFFFF
    Do
      DoEvents
      Sleep SleepValueLow
      DoEvents
      xLong2 = GetTickCount And &H3FFFFFFF
      If xLong2 < xLong1 Then
        If xLong2 + &H40000000 - xLong1 > IntervalMS Then Exit Do
        Else
        If (xLong2 - xLong1) > IntervalMS Then Exit Do
        End If
      Loop
    DoEvents
    End If
  End Sub
