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?
PCT3Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

simpsolCommented:
Thread.Sleep(Number of Seconds)
e.g
Thread.Sleep(6000)
0
sch13824Commented:
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
0
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
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?
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

tpaynCommented:
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
0
PCT3Author Commented:
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!.
0
PCT3Author Commented:
simpsol:

apparently there is more to it - do you have sample code hat you can share?
0
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
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.
0
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
BTW, you don't need the "Dim dNow etc" line ... I forgot and left that one in!
0
tpaynCommented:
Hi PCT3,
I would strongly advise you not to use a thread sleep function to “wait” in a function. Especially if you going to wait for 60 minutes. Running a tight loop with DoEvents is also not advisable due to context switching and processor load.

Presuming you using Microsoft Access and running your function from a form have a look at the Form.OnTimer event and the TimerInterval properties and refactor your code so you can call it in chunks.

Reasons for not using the sleep function:
Sleep is a blocking function. If your thread is a foreground or STA thread it will mean that your application will not be able to exit during this period and also that the message queue will not pump for this application. When you blocking a STA/foreground thread you risk the overflowing of the message queue. If it is a background thread your background thread will never be woken if the application exists and that’s never a good idea if you need to release resources etc.

If you still want to use the Sleep function add the following to a Module:

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

Now if you want to wait for 60 minutes you will use it in your code as

Call Sleep(3600000)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
simpsolCommented:
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.
0
PCT3Author Commented:
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.
0
PCT3Author Commented:
tpayn:

Sorry - I just had the declare statement mispositioned. Works fine now. Thank you very much for your help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Applications

From novice to tech pro — start learning today.