Solved

Is there a non-CPU intensive way of waiting?

Posted on 2004-04-27
7
692 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 200 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 50 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 250 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 85

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 85

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

746 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now