Solved

How to Create a Delay of 5 mins in an executing prorgam..

Posted on 2000-02-25
16
149 Views
Last Modified: 2010-05-02
hi all..
i want to know how to create a delay of 5 mins in a program..that i am executing..
I checked for related Functions in VB help.. but found none..
Please
help
Code would be useful..
Regards
Nitin
0
Comment
Question by:khacharn
[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
  • 5
  • 4
  • 3
  • +3
16 Comments
 
LVL 14

Expert Comment

by:mcrider
ID: 2560400
The following code will wait in the do loop for 5 minutes...

Dim x As Long
x = Timer + 300
Do While x > Timer
    DoEvents
Loop


Hope this helps!


Cheers!
0
 
LVL 7

Expert Comment

by:Vbmaster
ID: 2560964
I would recommend to use the Sleep API inside that loop, using a small interval (55ms is the lowest) will release much more CPU time than using only DoEvents (do not remove Doevents just add Sleep(55) before Doevents).

The API declaration for Sleep is...

   Declare Sub Sleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)
0
 
LVL 1

Expert Comment

by:HunterKiller
ID: 2561337
Note: your program wont be able to do nothing in this 5 minutes not even with CTRL+BREAK!
do this:

- in a general module:

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

- in your form:
Dim i as integer
For i = 1 to 5
  Sleep(60000)
next i
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Expert Comment

by:Vbmaster
ID: 2561428
HunterKiller: I think you need to start VB and try your code to see what it does, because using the Sleep API will interrupt the program, this is the reason you want to use a really short interval in a loop together with the DoEvents statement (The DoEvents statement will give the program CPU time for redrawing and user input).
0
 
LVL 1

Expert Comment

by:HunterKiller
ID: 2568551
could you read again the Q ? And could you read again my comment ? I told this already. And the guy says that it needs a pause. Sleep is the way.

[]'s
0
 

Expert Comment

by:explorer007
ID: 2571541
Hey Guys Don't Fight..man
Anways I will test both and the give points..OK
Could anyone tell me which Code will Relaease ny CPU time for other Programs to do their JOBS..
Regards
Nitin
0
 

Expert Comment

by:iqbalwali
ID: 2586878
This may work

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

Call Sleep(6000)

'This will pause your program for 1 min.
0
 

Author Comment

by:khacharn
ID: 2603394
HI ALL
i TRIED MCRIDER'S SOLUTION
bUT I AM OBSERVING THAT FOR THE NEXT 5 MINS MY CPU IS ENGAGED AND NOT FREED AS I WANT IT TO..
i WANT A SOLUTIN WHERE MY CPU TIME FOR MINUTES IS FREED FOR SOMEONE ELSE TO USE..
pLEASE HELP
asap
REGARDS
NITIN
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2603654
So add the Sleep API call as Vbmaster suggested... It would look like this:

'In a module put this
Declare Sub Sleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)



Dim x As Long
x = Timer + 300
Do While x > Timer
    Sleep 60000
    DoEvents
Loop


By the way, your program will not be able to do anything else while the Sleep API is running...



Cheers!®©
0
 
LVL 7

Expert Comment

by:Vbmaster
ID: 2603853
Actually, the code I was recommending is something like this... (compare it to mcriders code and have a CPU-usage-checking program going and you'll see the big difference)

   Dim x As Long

   x = Timer + 300
   Do While (x > Timer)
      DoEvents
      Sleep(55)
   Loop
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2603871
vbmaster, you're only sleeping for 55 milliseconds... ;-)
0
 
LVL 7

Expert Comment

by:Vbmaster
ID: 2604012
Ya I know, the reason I did not recomment Sleep(30000) is because that will freeze the program completely and prevent the window to repaint (which will make the program look... like a homeproject). Using a Do..DoEvents..Loop solution will make the program able to repaint BUT will take up a lot of CPU time. Using a short Sleep with the conjunction of a DoEvents statement has proved (to me at least) to be the most efficient solution.
0
 
LVL 14

Accepted Solution

by:
mcrider earned 50 total points
ID: 2604271
Cool...  Just for khacharn's info...


Using Norton System Monitor:

   * Looping without sleep takes 93% of the CPU.

   * Adding "Sleep 60000" take 0% CPU but as Vbmaster said, no screen paints occur.

   * Adding "Sleep 55" Takes 8% CPU but screen paints do occur.


Cheers!®©
0
 

Author Comment

by:khacharn
ID: 2604935
Guys you people are so perfect and Intelligent..
Thanx for all the info..
I will try the code and transfer the points(which i m sure you guys are least interested in..!!)
Regards
Nitin
0
 

Author Comment

by:khacharn
ID: 2737316
HI MCRIDER..
THANX FOR HELPING
BYE
Regards
Nitin
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2739588
Thanks for the points... Glad I could help...


Cheers!®©
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MS Access - Capture pressed key onclick 4 43
MsgBox 2 61
Need Nag Screen for Reboot if system up time is over 14 days 12 81
backup program with robocopy 6 70
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…
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
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…
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…

756 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