ASP Response Object : IsClientConnected Property

The IsClientConnected property is a read-only property that indicates if the client is currently connected to the server. This property is set to false when an end user clicks the browser's Refresh or Stop buttons, goes to another page on your server, or leaves your server entirely.
Example:

<%
  If Response.IsClientConnected Then
    'send information to client
  Else
    Response.End
  End If
%>
This script only sends the client information if they are still connected to the server, otherwise it calls Response.End to quit processing the script.

Tips & Hints

Don't Send Data to Disconnected Clients
IIS maintains a Request queue for every connection a user makes to the server. If a user gets impatient and hits the refresh button, then there will be a new request placed on the end of the queue, and a disconnected request in the middle of the queue. Since this middle request is no longer valid (the user is not connected), the server shouldn't waste it's time processing the script and trying to send the results to the user. By using the IsClientConnected property, you can avoid processing these disconnected requests, thereby increasing the performance and response time of your server.

Starting with IIS 5.0, this process is automated. Whenever ASP is about to execute a new request, it checks to see how long the request has been in the queue. If it has been there for more than 3 seconds, ASP will automatically check to see if the client is still connected before sending any data. The IIS metabase variable AspQueueConnectionTestTime can be used to modify this timeout of 3 seconds.

You can also use the IsClientConnected property to quit processing loops if the client disconnects in the middle of the loop.
For example:

<%
  While NOT recordset.EOF AND Response.IsClientConnected
    Response.Write recordset("table_data") & "<br>"
    Response.Flush
  Wend
%>
This will process all of the records of a database RecordSet (ADO) object, as long as the client is still connected. If the client hits the Stop button, then only a partial list of records will be shown. Notice that Response.Flush is called after each Response.Write. This flushes the buffer and allows the user to see progress, hopefully decreasing the chance that they will disconnect.


Back to the ASP Response Object
Back to ASP Objects
Resources:
http://www.devguru.com/
http://msdn.microsoft.com/asp/

All code is my own (although RMS thinks it should have GNU in front of it).