Solved

Is there a non-CPU intensive way of waiting?

Posted on 2004-04-27
7
697 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month8 days, 20 hours left to enroll

617 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