3 Attachment(s)
[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.
UPDATE
Version 2 of the code now works more reliably under Vista and Windows 7.
Check out the related code: Shell & Wait
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 ? :confused:
anyway,thanks :thumb:
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! ;)
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
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
Re: [VB6] Function Wait (non-freezing & non-CPU-intensive)
What's the difference about your code with timer object?
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.