Wednesday, December 23, 2009

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). Specifying 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).

No comments:

Post a Comment

Followers

hit counter