Alternate Sleep Function for ASP, HTA, WSC, etc.

Many scripters have gotten all too used to using the WScript object’s Sleep method for pausing or delaying script execution. Since this object isn’t available in ASP, Windows Script Components, or HTML Applications, programmers are required to create viable alternatives.

A Google search will return a plethora of possibilities each with their own pros and cons. Several third-party controls have also been created for this purpose. However, I prefer to stick to purely native means. I wanted a pure VBScript sleep function. Here’s what I came up with.

Sub Sleep(intSeconds)
	dteStart = Time()
	dteEnd = DateAdd("s", intSeconds, dteStart)
 
	While dteEnd > Time()
		DoNothing
	Wend
End Sub
 
Sub DoNothing
	'While/Wend has quirks when it is empty
End Sub

This is a simple Sleep method as a subroutine. It accepts a single integer value indicating the amount of time to sleep in seconds. It then snapshots the current time and adds the specified number of seconds to find an expiration time in the future.

A While/Wend loop is used as a timer until that expiration time is reached indicating that the specified number of seconds have elapsed.

Since loops in VBScript don’t always function well when they are empty or contain only comments, I’ve allowed my loop to make a call to an empty subroutine instead.

The syntax for this method is identical to that of WScript’s own Sleep method. This should work in any ASP, Windows Script Component (WSC), or HTML Application.

Tags

Like the read? Share it!

10 Comments

  • Great snippet. Thanks for the help. That’s a great banner by the way.

    Regards,

    AF

  • You’re very welcome. I appreciate your comments.

    Nilpo

  • Have you seen the CPU running during the sleep? 50% CPU charge in my local computer…

    David

  • Hello David.

    It doesn’t hurt your CPU at all to run at or near capacity, even for extended periods of time. (Assuming, of course, that you have proper cooling.) That is, after all, what the hardware is designed to do. What becomes important is what your CPU is doing.

    In this case, the CPU is running seemingly menial tasks. It’s not performing complex calculations. In essence it’s simply counting. And since this is a low-priority task it will have little effect on any other processes. If another process requires more CPU power, this will simply step down and hand it over willingly.

    All in all, while the CPU usage is a measure of what your CPU is doing, it’s not a very accurate measure of how hard it’s actually working. So even though the CPU usage might indicate that the CPU is under a higher than normal load; In this case, it’s not really under any additional stress.

    If you’re not comfortable with those levels, try taking it for a test drive. Set a script to sleep for a minute and then perform some normal tasks. Open your browser and Microsoft Word, play a video, etc. You shouldn’t notice any slower system response while performing these tasks despite the fact that the sleep timer is running.

  • Hi there everybody?

    Just wana thak you Nipo for this wonderfull snippet!

    It´s in use at http://www.dhstation.com for the chat facility stack sinchro!

    Thank you very much.
    Mayr Fortuna

  • Hi Nilpo,

    thanks for your snippet, but David is totally correct in stating the performance consumption of your code. While “waiting” for the dteEnd time to be reached it consumes lots of lots of CPU cycles to check the current time again and again. That’s nearly sort of an infinite loop driving your CPU load nearly up to 100%.

    Of course you are able to do other things in between. That’s what you have a multi tasking operating system for.

    Unfortunately I do not know of a real VBScript Sleep function consuming no CPU cycles.

    Regards, Tom.

  • Sweet…. This is what I’m looking for

  • Exactly what I was looking for. Thank you for the snippet.

  • Hi,

    Is this not better?

    Use an existing SQL ADO connection that may be in your script and offload the pause to MSDE/SQL

    con.execute “WAITFOR DELAY ’00:00:02′”

    Zero impact on the CPU as it sleeps the thread properly.

    Cheers,

    Paul

  • Hi Paul,

    That could be a good option. I’m curious whether that will pause script execution or only delay the connection thread. Have you tried it to see if it actually works?

    Nilpo

Leave a Reply

Contact

Wanna say hello?
Drop us a line!

You'll find us here

1 Microsoft Way,
Redmond,
WA 98052, United States