Link to home
Start Free TrialLog in
Avatar of metalaureate
metalaureateFlag for United States of America

asked on

Is there a non-CPU intensive way of waiting?

Dear Experts,

In VB6, is there a way of making a program wait for n seconds, without going round and round in a loop checking the system Timer, which seems to hog a lot of CPU.

Thanks
SOLUTION
Avatar of hes
hes
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of metalaureate

ASKER

There are no forms in y progra, so hes's solution is the way to go?

Any drawbacks the Sleep approach? Seems drastic!
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks everyone.
The code I posted uses a loop.  If you don't want the loop, then use the DateAdd() function as I did to compute the ending time of your delay, and then check the current time in a timer event.  Set the timer interval to about 250 milliseconds.

Idle_Mind
Also, my wait() sub is wrong.  It shoud be:

Private Sub wait(seconds As Integer)
    Dim startTime As Date
    Dim endTime As Date
   
    startTime = Now
    endTime = DateAdd("s", seconds, startTime)
    Do While Now < endTime
        Sleep 10
        DoEvents
    Loop
End Sub

Sorry....I'm going to go make my pot of coffee now.    =)

Idle_Mind