Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Is there a non-CPU intensive way of waiting?

Posted on 2004-04-27
7
Medium Priority
?
699 Views
Last Modified: 2012-05-04
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
0
Comment
Question by:metalaureate
7 Comments
 
LVL 20

Assisted Solution

by:hes
hes earned 800 total points
ID: 10929359
use the sleep API
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

When you want to sleep
Sleep ('Number of milliseconds to sleep)

Word of caution when you test this in the IDE it will sleep the entire IDE, since that is what is running not your program
0
 
LVL 5

Assisted Solution

by:ennixo
ennixo earned 200 total points
ID: 10929362
yes: the Timer Control

Insert a timer to your form
Set the property Enabled = False
when you want the program to wait:
Timer1.Interval = 1000 ' for example for 1 second
Timer1.Enabled = True

Private Sub Timer1_Timer()
    Timer1.Enabled = False
    it has been waiting or 1 second
End Sub
0
 

Author Comment

by:metalaureate
ID: 10929382
There are no forms in y progra, so hes's solution is the way to go?

Any drawbacks the Sleep approach? Seems drastic!
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 1000 total points
ID: 10929395
Yes...and No.

You can use the sleep API (http://www.mentalis.org/apilist/Sleep.shtml) to make your system wait for "n" seconds without hogging resources.  The problem with this approach, however, is that your app is completely suspended until those "n" seconds are over.  During that period, your app will be unresponsive to the user and it will not receive any system messages.

A decent compromise is to use a combination of a Timer control loop with a very short Sleep interval of say 10 milliseconds.  This is usually sufficient to bring resource usage down to a reasonable level.

The code to do this is below.

Regards,

Idle_Mind

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Private Sub Command1_Click()
    wait (10)
    MsgBox "Done"
End Sub

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

Author Comment

by:metalaureate
ID: 10929413
Thanks everyone.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 10929419
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
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 10929436
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
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

876 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question