Search
Enter Keywords:
Home
VBScript "Sleep" Function PDF Print E-mail
User Rating: / 14
PoorBest 
Written by Michael Salsbury   
Wednesday, 15 June 2005

Sometimes during a VBScript, you need the system to "go to sleep" for a little while so that you can wait for something to happen.  For example, you might want to start a process running and give it 30 seconds to finish before you do something else, or you might want check a given directory for a file every minute or so. 

More recent versions of the Windows Script Host (WSH) include a VBScript Sleep command that will pause the script for a specified number of milliseconds.  For example:

WScript.Sleep(1000)

Will cause the script to pause for 1 second (i.e., 1000 milliseconds).  It's fine to use this command so long as you know every machine running your VBScript has at least version 5.1 of the Windows Script Host installed.  Older versions (like those in Windows 95 and, I think, Windows 98) may not have WSH 5.1 or later.  Those versions won't have a Sleep command in them, and the script will end with an error if you use the Sleep command there.

I was using VBScript a while ago to handle some tasks associated with software distribution across the company LAN when I discovered the above information.  Since I couldn't be sure whether the script I had written would always be running on a machine where WSH 5.1 or later was present, I needed to code for "pre-5.1" versions as well, so that the script's behavior would remain the same across all machines on the LAN.

The sample code below provides a way to do that, more or less regardless of the VBScript/WSH version you're running under.  It's been tested under Windows 98, Windows 98SE, Windows NT 4.0, and Windows XP.

To use the function, call it and specify how long you want the script to wait.  For example:

GoSleep 10

Tells the script to go to sleep for approximately 10 seconds.  I say "approximately" because it's possible under certain circumstances it might not come out of the loop until 11 seconds or later if the script doesn't get control of the system again around the 10-second mark.

As with all my script code, this is provided without warranty or support. Use at your own risk.

Function GoSleep(seconds)
   '
   ' Check the Windows Script Host Version.
   '
   wsv = WScript.Version
   '
   ' If it's 5.1 or newer, use the built-in Sleep command,
   ' since it might be more reliable or better-performing.
   '
   if wsv >= "5.1" then
      WScript.Sleep(seconds * 1000)
   else
     '
     ' If the version isn't at least 5.1, use a quick-and-dirty
     ' method of determining the elapsed time since the function
     ' was called.
     '
     startTime = Time() ' gets the current time
     endTime = TimeValue(startTime) + TimeValue(elapsed) ' calculates when time is up
     '
     ' While the current time is less than the calculated end time,
     ' add zero to x.
     '
     While endTime > Time()
         '
         ' If there's nothing in here but a comment, the while
         ' doesn't seem to process correctly, so we'll just tell 
         ' VBScript that we need it to let Windows do other things
         ' for a little while...
         '
         DoEvents
     Wend
   end if
End Function


Related Blogs:

Related Links:

Last Updated ( Friday, 24 March 2006 )
< Previous   Next >

Main Menu
Home
Blog
Photos
Links
Search
Site Index
Feedback
Administrator
Featured Links
BlogInspiration
SpamToons
Shawn Prince's Blog
Jack Ludwig's Blog
Mike Cramer's Site
Fark
Slashdot
Woot!
Cigar Envy
John Kricfalusi's Blog
CigarBlog 101
Cigars 101 Forum
Sponsored Links


View Site Stats