Search Search

#1 worldwide
FREE Coding Lessons

since 1996
   THE BEST WAY to learn ASP & Asp.net!
Advertise Here!
click for details
Credits Host:
DiscountASP.net
Server Admin:
The "Team"
Contact Info.
Charles M. Carroll

my Blog
[prev. Lesson]  OLEDB & ODBC Drivers differences
     [next Lesson]  Scale: Virtues of Nothing

IsClientConnected -- eliminating stray tasks by Charles Carroll

Unbeknownst to most users if their script begins execution it continues invisibly in the background even if the user hits the stop button or moves to a different page on your site or the web. A database loop could tie up the CPU for quite a while, even though the user hit the "stop" button --  the task would still be "spinning/executing" on the server even though the user was long gone. This is particularly true in database tasks.

response.IsClientConnected()
is available beginning with IIS4 (if you want to see if you have it, see /freebook/asp/versioncheck.aspx) and can be used within a page to determine if the user is still fetching that page, i.e.

Here is some code that will loop until the script times out.  If the user hits stop, then the script will still continue invisibly even if they are executing a different page.

   filename=/learn/test/infiniteloop.asp

<Test Script Below>


<%server.scripttimeout=20
' this times out the script in 20 seconds whether done or not%>
<TITLE>infiniteloop.asp</TITLE>
<body bgcolor="#FFFFFF">
<% 
DO
    x=x+1
    response.write x & "<br>"
LOOP 
%>
</body></html>

Here is the fixed code that will loop until the script times out AND WILL STOP IF THE USER HITS "STOP" OR ACCESSES A DIFFERENT PAGE. Notice the mod test to ensure the echeck is only made when x is a multiple of 1000 (1000, 2000, 3000, 4000, etc.) to reduce how many times the client connection is checked.

   filename=/learn/test/infiniteloopfixed.asp

<Test Script Below>


<%server.scripttimeout=20
' this times out the script in 20 seconds whether done or 
' the user hit the STOP button
%>
<TITLE>infiniteloopfixed.asp</TITLE>
<body bgcolor="#FFFFFF">
<% 
DO
    x=x+1
    response.write x & "<br>"
    IF x MOD 1000=0 AND response.isclientconnected=false THEN
        EXIT DO
    END IF
LOOP
%>
</body></html>

this code above will stop when user hits the stop button.

I recommend that all loops through database recordsets incorporate this so that they don't execute invisibly wasting CPU and database server resources. So here is a database table script that incorporates this functionality and quits reading the database when the user hits stop button or accesses a different page:

   filename=/learn/test/dbtableisclientconnected.asp

<Test Script Below>


<%response.buffer=true%>
<TITLE>dbtableisclientconnected.asp</TITLE>
<body bgcolor="#FFFFFF">
<% 
' ASP program that displays a database in table form
myDSN="DSN=Student;uid=student;pwd=magic"
mySQL="select * from publishers"

set conntemp=server.createobject("adodb.connection")
conntemp.open myDSN
set rstemp=conntemp.execute(mySQL)
howmanyfields=rstemp.fields.count -1
%>
<table border=1><tr>
<% 'Put Headings On The Table of Field Names
for i=0 to howmanyfields %>
    <td><b><%=rstemp(i).name %></B></TD>
<% next %>
</tr>
<% ' Now lets grab all the records
howoften=200   ' how many records to check client connection
DO UNTIL rstemp.eof
    counter=counter+1
    response.write "<tr>" & vbcrlf
    for i = 0 to howmanyfields
        fieldval=rstemp(i)
        If isnull(fieldval) THEN
            fieldval="-null-"
        END IF
        If trim(fieldval)="" THEN
            fieldval="&nbsp;"
        END IF
           response.write "<td valign=top>" &  fieldval & "</td>" & vbcrlf
    next
    response.write "</tr>" & vbcrlf
    IF counter mod howoften = 0 THEN
        IF response.isclientconnected=false THEN
            EXIT DO
        ELSE
            response.write "</table>" & vbcrlf & "<table border=1><tr>"
            response.flush
        END IF
    END IF
    rstemp.movenext
LOOP
response.write "</table>"
rstemp.close
set rstemp=nothing
conntemp.close
set conntemp=nothing
%>
</body></html>










There are many worthy charities!!. But perhaps help starving children in Africa or South America AND help Charles too. a $5 tip buys him lunch at McDonalds, a $20 tip buys his kid Hitoshi a new computer game, a $39 tip buys his daughter Michiko a few nice outfits. See our donor list.