Solved

Is there a non-CPU intensive way of waiting?

Posted on 2004-04-27
7
696 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
LVL 86

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 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

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…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…

710 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