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

December 31st, 2008

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.

Please use the trackback link when linking to this post.

Related Posts:

Add to Technorati Favorites

8 Responses to “Alternate Sleep Function for ASP, HTA, WSC, etc.”

  1. Aaron Says:

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

    Regards,

    AF

  2. Nilpo Says:

    You’re very welcome. I appreciate your comments.

    Nilpo

  3. David Says:

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

    David

  4. Nilpo Says:

    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.

  5. Mayr Fortuna Says:

    Hi there everybody?

    Just wana thak you Nipo for this wonderfull snippet!

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

    Thank you very much.
    Mayr Fortuna

  6. Tom Says:

    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.

  7. Processor Benchmark Says:

    Sweet…. This is what I’m looking for

  8. Ajith Nair Says:

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

Leave a Reply

.htaccess Apache article articles by Nilpo ASP ASP Free automation clipboard copy desktop Dev Shed docx Internet Explorer message box Microsoft PHP registry remove script scripting text tutorial tweak tweets Twitter UAC updates User Account Control VBS VBScript vista volume Windows Windows 7 Windows Guru Windows Script Windows Script Host Windows Scripting Windows Vista Windows XP Word WScript wscript.exe WSH XP