Link to home
Start Free TrialLog in
Avatar of PCT3
PCT3

asked on

How do I wait a specified length of time in a VB routine?

I want to halt a routine after sending a batch of emails and then start up again after a specified period of time (currently 60 minutes, but that can change).

Is there a wait or halt processing cmd available in VB?
Avatar of simpsol
simpsol
Flag of United States of America image

Thread.Sleep(Number of Seconds)
e.g
Thread.Sleep(6000)
Avatar of sch13824
sch13824

I would recommend something more like the following. Calling DoEvents every half second or so lets the system process events like repainting the screen of the application.

At the top of your module declare the Windows kernal library sleep function
private Declare Sub Sleep Lib "kernel32" Alias "Sleep" (byval dwMilliseconds as Long)

Sub MySleep(ByVal hours as int, ByVal mins as int, ByVal secs as int)
   ResumeTime = Now + TimeSerial(hours,mins, secs)
   Do While Now < ResumeTime
      Sleep(500)
      DoEvents
   Loop
End Sub
Avatar of Scott McDaniel (EE MVE )
If you "halt a routine", you would of course render your database inoperative for that length of time - in other words, you really couldn't do anything else with the database. You could of course work with other applications and such.

Is that what you want to do?
VB6 or VB.Net?

VB6 use a plain old Timer control and process the Timer event.

VB.Net use a TimerCallback
http://msdn.microsoft.com/en-us/library/system.threading.timercallback.aspx
Avatar of PCT3

ASKER

LSMConsulting:

OK for database to be dedicated solely to this function until completed

Simpsol:

Is this really all there is to it? Such a simple solution-great!.
Avatar of PCT3

ASKER

simpsol:

apparently there is more to it - do you have sample code hat you can share?
You would need to include code in the routine to suspend all other app-level processing until the time had passed.

You could do that like this:

Sub YourSub()

<code here>

Dim dNow As Date
Dim dStart As Date
dStart = Now

Do
  If DateAdd("n", 60, dStart) > Now Then Exit Do
  DoEvents
Loop

<rest of your sub's code here>

If you're doing this on a Form, you should make that form Modal (in the object's properties), otherwise the user could move away from that form and run other processes in the database.
BTW, you don't need the "Dim dNow etc" line ... I forgot and left that one in!
ASKER CERTIFIED SOLUTION
Avatar of tpayn
tpayn

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If you want to wait fot 60 minutes (sorry I did not take the 60 minutes literally in your original post) then the best option would be to create a service which goes to sleep for 60 minutes that way no UI is requried and you can do other things with the database in the meantime if needed.
Avatar of PCT3

ASKER

tpayn:

Yes, I am using Microsoft Access 2007 and I want to run the current request ASAP, so your solution looks simple enough for now & I understand your warnings - time later to go back and do it a better way. However, when I insert the 2 lines of code you provided, I get a compile error when I try to execute.
Avatar of PCT3

ASKER

tpayn:

Sorry - I just had the declare statement mispositioned. Works fine now. Thank you very much for your help.