HOWTO: Use IsClientConnected to Check If Browser is Connected

ID: Q182892


The information in this article applies to:


SUMMARY

When a browser requests an Active Server Pages (ASP) page from the server, but does not wait for the entire page to be downloaded, the server continues to process the request, wasting CPU cycles. However, in Internet Information Server (IIS) 4.0, you can use the Response.IsClientConnected property to determine if the browser is still connected, and if not, to cease processing the ASP page to conserve CPU cycles.


MORE INFORMATION

Below, is a sample ASP page that demonstrates one application of the IsClientConnected property.

WARNING: ANY USE BY YOU OF THE CODE PROVIDED IN THIS ARTICLE IS AT YOUR OWN RISK. Microsoft provides this code "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.


   <%@ LANGUAGE="VBSCRIPT" %>
   <%
   '
   '   !!! WARNING !!!
   '
   '   This page has code in it that may use 100% CPU cycles for at least
   '   30 seconds. Do not run this code on a production server, restrict
   '   its use to a test server.
   '

   Function IsConnectedAfter(Seconds)
      Dim StartTime 'time the function started
      Dim PauseTime 'time the pause loop started

      ' Use PerfMon to monitor the CPU cycles on the Web server. You
      ' will notice that if you click stop in the browser, the CPU
      ' will settle down sooner than if the loop had continued.

      IsConnectedAfter = True
      StartTime = Now

      Do While DateDiff("s", StartTime, Now) < Seconds
         PauseTime = Now
         Do While DateDiff("s", PauseTime, Now) < 1
            'Do Nothing
         Loop
         Response.Write "."
         If Response.IsClientConnected = False then
            IsConnectedAfter = False
            Exit Function
         End If

      Loop
   End Function

   %>
   <HTML>
   <HEAD>
   <META NAME="GENERATOR" Content="Microsoft Visual InterDev 1.0">
   <META HTTP-EQUIV="Content-Type" content="text/html; charset=iso-8859-1">
   <TITLE>Document Title</TITLE>
   </HEAD>
   <BODY>

   <h1>!!! WARNING !!!</h1>
   <p>This page has code in it that may use <b>100% CPU cycles</b>
   for at least 30 seconds. Do not run this code on a production
   server, restrict its use to a test server.

   <p>Use PerfMon to monitor the CPU cycles on the Web server.
   Press STOP in the Web browser, and you will see that the
   CPU cycles will settle down sooner than they would have
   without checking the IsClientConnected property.

   <HR>

   <%
      If IsConnectedAfter(30) then
         Response.Write "<p>The client is still connected</p>"
      Else
         'The browser is no longer connected. This would be a
         'good place to abort any transactions, clean up any
         'variables, and so forth.
      End If
   %>

   </BODY>
   </HTML> 


REFERENCES

For the latest Knowledge Base articles and other support information on Visual InterDev and Active Server Pages, see the following page on the Microsoft Technical Support site:

http://support.microsoft.com/support/vinterdev/

Additional query words:


Keywords          : kbASP kbVisID kbVisID100 kbVisID600 kbWebServer kbGrpASP VIServer VIScripting 
Version           : WINDOWS:1.0,6.0; winnt:
Platform          : WINDOWS winnt 
Issue type        : kbhowto 

Last Reviewed: May 11, 1999