Home

ASP ADVANCED

By default, the maximum amount of time an Active Server Pages script will execute is 90 seconds.  This prevents scripts that become caught in infinite loops from running forever.  It's a useful safeguard.

Nevertheless, on occasion you may need to allow a script to execute for longer than 90 seconds.  For example, if you're using a script to output a very large HTML page, you wouldn't want the script to time out when only part of the page is displayed.  You can control the maximum amount of time for which a script is allowed to execute by using the ScriptTimeout property of the server object, as in this example:- 

<% Server.ScriptTimeout=150 %>
<HTML>
<HEAD><TITLE>Falling star</TITLE></HEAD>
<BODY>
<%
RANDOMIZE
starx=60
FOR k=1 to 10
nextsecond=DATEADD("s",10,time)
DO WHILE TIME<nextsecond
LOOP
starx=starx+3*RND( ) -1
FOR i=1 TO starx
Response.Write("&nbsp;")
NEXT
Response.Write("*<P>")
NEXT
%>
</BODY>
</HTML>

This script creates a falling star that falls very slowly.  The star (an asterisk) is printed to the browser window in 10-second intervals (see Figure below).

A Falling Star

A Falling Star

Normally, the script would time out before the star finishes falling.  However, the first line in this page prevents this from happening, because the ScriptTimeout property of the server object has been set to 150 seconds.

You can't use the Server.ScriptTimeout property to reduce the maximum amount of time for which a script will execute to less than 90 seconds.  To force your scripts to finish executing before 90 seconds elapses, you need to change the Script.Timeout property by using the Internet Service Manager.  This property is located on the App Options page of the Application Configuration dialog box.  If you set the scriptTimeout property to -1, your scripts will never time out.

Allowing scripts to run for long periods of time could create a significant drain on the resources of your Web server.  In fact, a script in an Active Server Page may continue to execute even after the person who requested the Active Server Page has gone away.  In this case, if the script continues to execute, it benefits no one.  Fortunately, a property of the Response object can help.  The IsClientConnected property can be used to check whether a connection is still open between a browser and the server.  You can use this property to end further script processing if a user has abandoned a page.  For example, the following script continues to execute until the script times out or the browser becomes disconnected from the server:

<HTML>
<HEAD><TITLE>Obnoxious Page</TITLE></HEAD>
<BODY>
<%
WHILE 1=1
Response.Write("Hello. How are you?")
IF NOT Response.IsClientConnected THEN Response.End
WEND
%>
</BODY>
</HTML>

Note that the IsClientConnected propertv only reflects whether a browser is still connected since the last Response.Write method call.  If you have a long-running script that doesn't output anything to a browser, this property won't be useful.