Microsoft KB Archive/182892

= How To Use IsClientConnected to Check If Browser Is Connected =

Article ID: 182892

Article Last Modified on 5/2/2006

-

APPLIES TO


 * Microsoft Visual InterDev 1.0 Standard Edition
 * Microsoft Visual InterDev 6.0 Standard Edition
 * Microsoft Active Server Pages 4.0

-



This article was previously published under Q182892



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 and 5.0, you can use the Response.IsClientConnected property to determine if the browser is still connected and, if it is not, to cease processing the ASP page to conserve CPU cycles.



MORE INFORMATION
The following sample ASP page 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

%>      Document Title  

!!! 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.

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.



<%     If IsConnectedAfter(30) then Response.Write " The client is still connected " 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  %>

 

