VBScript: Sleep function (or Wait for few minutes)


WScript provides a function called Sleep. You can use sleep function to pause the script for specified milliseconds. Yeah! all good. But I wanted to pause the script for few minutes (sometimes more than 30 minutes). Specifing 15 minutes in milliseconds means 900000, 30 minutes means 1800000 milliseconds. I didn’t wan to deal with bigger numbers and didn’t feel right.  So I wrote a small subroutine to pause the script by specified minutes.  Here it is and feel free use it.
 
‘*********************************************************
‘ Sub Routine: GoToSleep
‘ Parameters: Number of Minutes
‘ Purpose: Pause the script for specified minutes
‘ Written by: Anand Venkatachalapathy
‘ Date Written: July 1 2009
‘*********************************************************
Sub GoToSleep (iMinutes)
 Dim Starting, Ending, t
 Starting = Now
 Ending = DateAdd("n",iMinutes,Starting)
 Do
  t = DateDiff("n",Now,Ending)
  If  t <= 0 Then Exit Do
  WScript.Sleep 10000
 Loop
End Sub
‘******* End of Sub Routine **************
 
If you really care how this subroutine works, read ahead.
 
1. Register the current time (Starting = Now)
2. Add the specified minutes to the current time and find the time after x minutes (Ending = DateAdd("n",iMinutes,Starting))
3. Do a "Do/Loop" loop till the current time is the "Time" after x minutes (see the Do Loop above).

4 thoughts on “VBScript: Sleep function (or Wait for few minutes)

  1. Wow… Pretty useless script.Then lines where a simple WScript.Sleep 15 * 60 * 1000 could have sufficed for 15 minutes.

  2. Lol!! Not very efficient code, considering you are processing so many lines of code when it could be dnoe with just 1 line, on a slow machine you’ll probably draw out the sleep to 5 mins!

    1. The other reason to use “Sleep” instead of a loop is that the script will stop using processor resources unless it has to service an event.
      This is especially important if you are using an operating system that supports multi tasking. 😉

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s