Solved

Is there a non-CPU intensive way of waiting?

Posted on 2004-04-27
7
694 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Write data from SQL to CSV file - position 1 61
ms access #TYPE! error on report when no data 4 77
how to know if my Checkbox is True in VB6.0? 9 55
RUNRMTCMD from AS/400 12 47
Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
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 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…
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…

777 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