Results 1 to 7 of 7

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

  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: 1034
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)

  2. #2
    Lively Member
    Join Date
    Jun 2010
    Posts
    68

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

    sorry,i don't get any benefit by it. As example, in your form, you put a command for beep,i put there
    Code:
    Private Sub Command2_Click()
    Wait 5000
        Beep
    End Sub
    so it means beep will ring after 5 sec , am i get right or wrong ?

    anyway,thanks

  3. #3

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

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

    If you tried the code, you should be able to hear the beep approximately 5 seconds after you clicked the button. In the meantime, you ought to be able to do something else with the Form, like clicking the other buttons or typing some words in the TextBox. You will also notice that the CPU usage of this demo app does not increase much, if at all, during the time spent waiting. Those are the main benefits of using this code. I hope that explains it better now!
    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)

  4. #4
    Lively Member
    Join Date
    Jun 2010
    Posts
    68

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

    thanks bro for your reply and i mentioned before that its not working,beep not comes after 5 sec,i checked your project in windows 7

  5. #5
    Banned
    Join Date
    Nov 2012
    Posts
    1,171

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

    working good am on xp my beep isnt working also

    Private Sub Command1_Click()
    Wait 5000
    Me.Caption = "aaaaa"
    End Sub


    and working so i guess code is working and also this is needed better then other timer waiting codes

  6. #6
    New Member
    Join Date
    Mar 2013
    Posts
    4

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

    What's the difference about your code with timer object?

  7. #7

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

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

    The intrinsic VB Timer control is actually implemented by calling the SetTimer API function. Unlike the Timer control, SetTimer allows for an interval greater than one minute to be set.
    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