Sleep Timer Component

The ASPSmith Articles, ASPAlliance.com

Sleep Timer Component

By Steven Smith
http://www.aspalliance.com/stevesmith/articles/sleeptimer.asp
[Example]

Sometimes it is important to be able to wait a short period of time in an ASP or VB application before continuing on. Most recently, I had to write an application that used Site Server content replication to migrate files from one folder to another. In order to ensure that the process completed successfully, I had to check the process's status periodically and wait for it to either fail or succeed. Obviously, I could have done a WHILE ERR<>0...WEND loop, but that would have been unnecessarily harsh on the cpu -- I really don't need to check the status of the process a thousand times per second.

So, this component, Timer.Sleep, uses the windows kernel Sleep command, and is only a few lines of code. It has one method, DoSleep, which takes one parameter, the number of milliseconds to sleep. You can download the component DLL here: Timer.dll (20k)

The source code for the example is:
   <% OPTION EXPLICIT %>
   <%
   'Try to avoid buffering and caching...
   Response.Buffer = False
   Response.Expires = 0
   Response.ExpiresAbsolute = Now() - 1
   Response.AddHeader "cache-control", "private"
   Response.AddHeader "pragma", "no-cache"
   Server.ScriptTimeout = 0
10   %>

11   <!-- #INCLUDE VIRTUAL="/stevesmith/include/articleformat.asp" -->
12   <!-- #INCLUDE VIRTUAL="/stevesmith/include/DisplayStatusMessage.asp" -->
13   <%
14   Response.Write("This example is no longer supported - the COM object is no longer installed on this server.")
15   Response.End
16   'Declare Variables
17   Dim objTimer
18   Dim I
19   Dim strCountdown
20   
21   'Instantiate components
22   Set objTimer = Server.CreateObject("Timer.Sleep")
23   
24   'Display top of page template
25   Call ArticleHeader("Sleep/Timer Example","","")
26   %>

27   <p>
28   <b>Look at your status bar at the bottom of your browser.</b>
29   </p>
30   <p>This is a very simple example that demonstrates the use of the timer
31   dll. We're going to be writing to the status bar of your browser, once per second,
32   with a countdown.
33   </p>
34   <p>
35   <b>Note:</b>
36   If your browser is set up to cache web pages, you may not see the countdown take place
37   after your first visit to this page.
38   </p>
39   <%
40   
41   'Display bottom of page template
42   Call ArticleFooter()
43   
44   'Count down from 10 to 0, then display LIFTOFF in the status window
45   For I = 10 To 0 Step -1
46    Call objTimer.DoSleep(1000)
47    strCountdown = strCountdown & I & "... "
48    If I = 0 Then
49    strCountdown = strCountdown & " LIFTOFF!!!"
50    End If
51    DisplayStatusMessage(strCountdown)
52   Next
53   %>

The included file that writes to the status window is documented here:
   <%
   Public Sub DisplayStatusMessage(strMessage)
    Dim NL
    NL = chr(10) & chr(13)
    Response.Write "<script type=" & Chr(34) & "text/javascript" & Chr(34) & ">" & NL
    Response.Write "<!--" & NL
    Response.Write "parent.window.status = '" & strMessage & "';" & NL
    Response.Write "//--></script>"
   End Sub
10   %>