Click here to Skip to main content
11,581,397 members (59,264 online)
Click here to Skip to main content

AH, Ah, ah, ah…Staying Alive…Staying Alive

, 10 Jun 2009 CPOL 79.1K 92
Rate this:
Please Sign up or sign in to vote.
Sometimes you want your web page to 'stay alive'. That is, if a user is filling out a complicated form, you do not want the session to time out before they are finished. The user could get very angry and rightfully so: You might even get yelled at!

Sometimes you want your web page to 'stay alive'. That is, if a user is filling out a complicated form, you do not want the session to time out before they are finished. The user could get very angry and rightfully so: You might even get yelled at!

It's not simply a matter of increasing the session timeout to a very large value. If you do that, the sessions would be left active in the server memory for hours—long after the visitors have left the site. Increasing the session timeout IS a solution… but not necessarily a good solution.

The goal is that the session should stay active as long as the web page is open on the client machine… even if there are no post backs to reset the session timer. When the web page is closed, the session should time out normally.

I implemented a solution for this: The client will "ping" the server at intervals of less than the session timeout which will reset the session timer. This is known as the Heartbeat design pattern (I couldn't find a decent site/page to link to).

Miscellaneous Setup Stuff

For testing purposes, I set the Session Timeout to two minutes in web.config:

<system.web>
  <sessionState timeout="2">
  </sessionState>
 </system.web>

To trace what is happening, I used a utility function called ODS (it's in a class called MiscUtilities):

// ---- ODS (Output Debug String) ----------------------
public static void ODS(string Msg)
{
    String Out = String.Format("{0}  {1}", DateTime.Now.ToString("hh:mm:ss.ff"), Msg);
    System.Diagnostics.Debug.WriteLine(Out);
}

To watch the Session State events, I added debugging strings to the global.asax file:

<%@ Application Language="C#" %>
<script RunAt="server">
      
    void Application_Start(object sender, EventArgs e)
    {
        MiscUtilities.ODS("****ApplicationStart");
    }
    void Session_Start(object sender, EventArgs e)
    {
        MiscUtilities.ODS("Session_Start");
    }
    void Session_End(object sender, EventArgs e)
    {
        MiscUtilities.ODS("Session_End");
    } 

Here are the details: We need a method at the server for the client to call. We use a WebMethod.

  1. There must be a ScriptManager on the page.
  2. The ScriptManager must have EnablePageMethods set to true.
  3. The WebMethod must be public and static.
  4. The WebMethod must have the EnableSession attribute set to true.
<asp:ScriptManager ID="ScriptManager1" runat="server" 
    EnablePageMethods="true">
</asp:ScriptManager>

public partial class _Default : System.Web.UI.Page
{
    [WebMethod(EnableSession=true ) ]
    public static void PokePage()
    {
        // called by client to refresh session
        MiscUtilities.ODS("Server: I am poked");       
    }

We need JavaScript at the client to call the server function at fixed intervals:

<script type="text/javascript">
 
    var HeartBeatTimer;
 
    function StartHeartBeat()
    {
        // pulse every 10 seconds
        if (HeartBeatTimer == null)
            HeartBeatTimer = setInterval("HeartBeat()", 1000 * 10);
    }
 
    function HeartBeat()
    {
        // note: ScriptManger must have: EnablePageMethods="true"
        Sys.Debug.trace("Client: Poke Server");
        PageMethods.PokePage();
    }

<body id="MyBody"  onload="StartHeartBeat();">

Here is what the output looks like without the heartbeat:

10:22:43.03 ****ApplicationStart 
10:22:45.13 Session_Start 
10:25:00.00 Session_End 

Here is the output with the heartbeat:

 
10:26:06.10  ****ApplicationStart
10:26:08.05  Session_Start
Client: Poke Server
10:26:18.93  Server: I am poked
Client: Poke Server
10:26:28.95  Server: I am poked
Client: Poke Server
10:26:38.96  Server: I am poked
Client: Poke Server
10:26:48.98  Server: I am poked

    . . . (lines deleted)

Client: Poke Server
10:29:59.45  Server: I am poked
Client: Poke Server
10:30:09.47  Server: I am poked
Client: Poke Server
10:30:19.48  Server: I am poked

    . . . (lines deleted)

It looks like the session is staying alive while the client is idle: Excellent!

I hope someone finds this useful.

Steve Wellens

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Steve Wellens
EndWell Software, Inc.
United States United States
I am an independent contractor/consultant working in the Twin Cities area in Minnesota. I work in .Net, Asp.Net, C#, C++, XML, SQL, Windows Forms, HTML, CSS, etc., etc., etc.

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 5 Pin
phil.o23-Feb-12 7:58
memberphil.o23-Feb-12 7:58 
GeneralComparing your work to other implementations Pin
Omar Gamil18-Jun-11 4:00
memberOmar Gamil18-Jun-11 4:00 
GeneralRe: Comparing your work to other implementations Pin
Steve Wellens18-Jun-11 4:34
memberSteve Wellens18-Jun-11 4:34 
GeneralRe: Comparing your work to other implementations Pin
Omar Gamil18-Jun-11 4:37
memberOmar Gamil18-Jun-11 4:37 
GeneralMy vote of 5 Pin
Keith Barrow29-May-11 12:22
mentorKeith Barrow29-May-11 12:22 
GeneralMy vote of 1 Pin
Jason Ti14-Dec-10 23:06
memberJason Ti14-Dec-10 23:06 
GeneralSending ajax request is not a good solution Pin
Jason Ti14-Dec-10 23:04
memberJason Ti14-Dec-10 23:04 
GeneralRe: Sending ajax request is not a good solution Pin
Steve Wellens15-Dec-10 2:46
memberSteve Wellens15-Dec-10 2:46 
GeneralRe: Sending ajax request is not a good solution Pin
Jason Ti20-Dec-10 22:18
memberJason Ti20-Dec-10 22:18 
GeneralRe: Sending ajax request is not a good solution Pin
Steve Wellens21-Dec-10 3:42
memberSteve Wellens21-Dec-10 3:42 
GeneralRe: Sending ajax request is not a good solution Pin
Jason Ti5-Jan-11 2:06
memberJason Ti5-Jan-11 2:06 
GeneralRe: Sending ajax request is not a good solution [modified] Pin
Steve Wellens5-Jan-11 3:14
memberSteve Wellens5-Jan-11 3:14 
GeneralRe: Sending ajax request is not a good solution Pin
Jason Ti7-Jan-11 2:16
memberJason Ti7-Jan-11 2:16 
GeneralRe: Sending ajax request is not a good solution Pin
Steve Wellens7-Jan-11 3:09
memberSteve Wellens7-Jan-11 3:09 
GeneralGood stuff Pin
xoulrage7-Dec-09 20:42
memberxoulrage7-Dec-09 20:42 
GeneralThanks Pin
Pak51427-Nov-09 11:56
memberPak51427-Nov-09 11:56 
GeneralAuthenication Pin
Mr President11-Aug-09 7:46
memberMr President11-Aug-09 7:46 
GeneralNice trick...You could also use... Pin
Member 32316995-Aug-09 6:15
memberMember 32316995-Aug-09 6:15 
GeneralSource code Pin
dazology5-Aug-09 4:54
memberdazology5-Aug-09 4:54 
GeneralRe: Source code Pin
Steve Wellens5-Aug-09 5:48
memberSteve Wellens5-Aug-09 5:48 
GeneralSimply Superb Pin
janeshh3-Aug-09 23:25
memberjaneshh3-Aug-09 23:25 
GeneralRe: Simply Superb Pin
dazology5-Aug-09 4:53
memberdazology5-Aug-09 4:53 
QuestionFull source Please !! [modified] Pin
tolpromobil3-Aug-09 22:57
membertolpromobil3-Aug-09 22:57 
GeneralTerrific! Thank You! Thank You! Thank You! Pin
Ron Kunce3-Aug-09 6:15
memberRon Kunce3-Aug-09 6:15 
QuestionHow about this! Pin
azamsharp3-Aug-09 1:42
memberazamsharp3-Aug-09 1:42 
AnswerRe: How about this! Pin
Steve Wellens3-Aug-09 3:24
memberSteve Wellens3-Aug-09 3:24 
GeneralNice work I might use this.... Pin
Wahab Hussain2-Aug-09 17:49
memberWahab Hussain2-Aug-09 17:49 
GeneralI poke you Pin
Irwan Hassan18-Jun-09 7:24
memberIrwan Hassan18-Jun-09 7:24 
GeneralRe: I poke you Pin
Steve Wellens18-Jun-09 12:08
memberSteve Wellens18-Jun-09 12:08 
GeneralRe: I poke you Pin
MahmoudMoravej7-Aug-09 6:56
memberMahmoudMoravej7-Aug-09 6:56 
GeneralGood one Pin
Coding C#15-Jun-09 22:09
memberCoding C#15-Jun-09 22:09 
GeneralInteresting! Pin
R. M. Masudur11-Jun-09 1:38
memberR. M. Masudur11-Jun-09 1:38 
GeneralVery useful Pin
tirzan10-Jun-09 23:34
membertirzan10-Jun-09 23:34 
Generalmy kind of article Pin
Yankee Imperialist Dog!10-Jun-09 2:21
memberYankee Imperialist Dog!10-Jun-09 2:21 
GeneralI found it useful :) Pin
Seishin#9-Jun-09 21:10
memberSeishin#9-Jun-09 21:10 
GeneralGood content and fun to read Pin
Joe Enos9-Jun-09 19:58
memberJoe Enos9-Jun-09 19:58 
GeneralAwesome! Pin
roguewarrior9-Jun-09 18:18
memberroguewarrior9-Jun-09 18:18 
GeneralRe: Awesome! Pin
BeeGone17-Jun-09 1:49
memberBeeGone17-Jun-09 1:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150603.1 | Last Updated 10 Jun 2009
Article Copyright 2009 by Steve Wellens
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid