How to wait for a few seconds

Posted on 2003-02-28
Medium Priority
Last Modified: 2012-05-04

I am currently developing some software that waits for a couple (or more) seconds before continuing. I was using this:

While **check for seconds passed**

This does the trick but works horrors to the poor little processor.

There must be a way to wait without processing anything, just waiting, simply wait.

I tried a couple of API calls but they killed the event processing so my programming 'dies' until the wait ends.

Any help will be appreciated.

Question by:Esopo
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
  • 3
LVL 17

Accepted Solution

inthedark earned 200 total points
ID: 8045919
To minimise affect on the processor you can do 2 things, use sleep and also calculate end time before loop. Example:

Const SecondsToTimeFactor As Double = 1#/(1440#*60#)

Dim TimeToStop As Date

DelaySeconds = 2 ' wait 2 seconds
TimeToStop = Now + (SecondsToTimerFactor * DelaySeconds)

Do While Now<TimeToStop
    Sleep 200 ' never sleep for more than 1 seconds (1000MS)

You need this declaration:

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

Warning to not attempt to sleep for more than 1 seconds in a form.  This will cause a form to appear to hang.  If the form receives a windows message like close form/got/lost focus etc.  Messages will be queued until the function wakes up.
LVL 17

Expert Comment

ID: 8045948
I did some calculations and found that a program that waits in this way will use 1 seconds of CPU time every few weeks.
LVL 14

Author Comment

ID: 8046657

You are good!!!

I tried it out, it sure works!

I played with it a bit, and realizaed even a 1 ms sleep will release the processor to a point where it seems no extra processing is being done.

The thing is, I need the form to continue fully functional while waiting, and 200 ms makes it look like hanging while the user types on a textbox or something like it.

Anyway, this sleep idea works like a charm, I think you deserve an award for this.

PS: I can't believe how a simple 1 ms sleep can release the processor almost completely.

Best regards
LVL 17

Expert Comment

ID: 8047184
You should also checkout this link.  If you are not realy sleeping you should use the sleepex function.


Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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 …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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 process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses
Course of the Month9 days, 1 hour left to enroll

764 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