Results 1 to 7 of 7

Thread: [VB6] Function Wait (non-freezing & non-CPU-intensive)

Threaded View

  1. #1

    Thread Starter
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Arrow [VB6] Function Wait (non-freezing & non-CPU-intensive)

    This routine waits for the specified amount of time without blocking other events or increasing the CPU usage. Fully tested only on XP. Using PeekMessage with a value of -1 for the hWnd parameter seems to be unreliable under Vista and Windows 7, but the documentation makes no mention of this.

    Code:
    Private Type MSG
        hWnd    As Long
        Message As Long
        wParam  As Long
        lParam  As Long
        Time    As Long
        Pt_X    As Long
        Pt_Y    As Long
    End Type
    
    Private Declare Function KillTimer Lib "user32.dll" (ByVal hWnd As Long, ByVal nIDEvent As Long) As Long
    Private Declare Function PeekMessage Lib "user32.dll" Alias "PeekMessageW" (ByRef lpMsg As MSG, ByVal hWnd As Long, ByVal wMsgFilterMin As Long, ByVal wMsgFilterMax As Long, ByVal wRemoveMsg As Long) As Long
    Private Declare Function SetTimer Lib "user32.dll" (ByVal hWnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, Optional ByVal lpTimerFunc As Long) As Long
    Private Declare Function WaitMessage Lib "user32.dll" () As Long
    
    'This routine waits for the specified amount of time before resuming with the next line of code
    Public Function Wait(ByVal Milliseconds As Long) As Boolean
        Const PM_QS_POSTMESSAGE = &H980000, WM_TIMER = &H113&
        Dim TimerID As Long, M As MSG
    
        TimerID = SetTimer(0&, App.ThreadID, Milliseconds)
        If TimerID Then
            Do:  Wait = WaitMessage
                 If PeekMessage(M, -1&, WM_TIMER, WM_TIMER, PM_QS_POSTMESSAGE) Then If M.wParam = TimerID Then Exit Do
            Loop Until DoEvents < 0
            TimerID = KillTimer(0&, TimerID):     Debug.Assert TimerID
        End If
    End Function
    The attached Form demonstrates usage of this simple function.


    Name:  Wait.png
Views: 1356
Size:  12.4 KB



    UPDATE

    Version 2 of the code now works more reliably under Vista and Windows 7.



    Check out the related code: Shell & Wait
    Attached Files Attached Files
    Last edited by Bonnie West; Oct 11th, 2015 at 11:33 PM.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width