Session End when browser closed?

A

Andrew Banks

I'm wanting to track the number of users using an application.

I'm using App variables and incrementing an int value by 1 everytime a new
session is started. I'm having a problem decrementing this int once they
close their browser.

I assumed that putting some code in Session_End in Global.asax to do this
would work but it seems it doesn't. Is a session not ended once a user
closes their browser or does the session not end until the timeout period as
set in web.config has passed?

Any help is most appreciated

Sample code is below;

// Extract from Global.asax
protected void Session_Start(Object sender, EventArgs e)
{
Application.Lock();
Application["TotalPlayers"] = (int)Application["TotalPlayers"] + 1;
Application.UnLock();
}

protected void Session_End(Object sender, EventArgs e)
{
Application.Lock();
Application["TotalPlayers"] = (int)Application["TotalPlayers"] - 1;
Application.UnLock();
}

// Extract from web.config
<sessionState
mode="InProc"
stateConnectionString="tcpip=127.0.0.1:42424"
sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes"
cookieless="false"
timeout="5"
/>
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi Andrew,

I assumed that putting some code in Session_End in Global.asax to do this
would work but it seems it doesn't. Is a session not ended once a user
closes their browser or does the session not end until the timeout period as
set in web.config has passed?

The session ends if it's inactive ( not object requested) for more than the
timeout period.

From MSDN ( Session states ):
ASP.NET provides the cross-request state information (shopping carts, data
scrolling, and so on) infrastructure that Web applications require, with
built-in session-state functionality that enables you to take the following
actions:

a.. Raise appropriate session-lifetime management events (Session_OnStart,
Session_OnEnd, and so on) that can be handled in application code.
Note The Session_OnEnd event is supported only the in-process
session-state mode. This event is not raised if you use State Server or SQL
Server modes.
a.. Automatically release session data if the browser does not revisit an
application within a specified time-out period.


Cheers,
 
S

Steve Caliendo

Closing a browser window does not trigger the user session to end. Mainly,
only the session timeout causes the session to end (or a session.abandon)

Steve
 
A

Andrew Banks

Is there anyway I can change the value of my app variable then when the
browser is closed then?

Thanks for your input

Steve Caliendo said:
Closing a browser window does not trigger the user session to end. Mainly,
only the session timeout causes the session to end (or a session.abandon)

Steve


Andrew Banks said:
I'm wanting to track the number of users using an application.

I'm using App variables and incrementing an int value by 1 everytime a new
session is started. I'm having a problem decrementing this int once they
close their browser.

I assumed that putting some code in Session_End in Global.asax to do this
would work but it seems it doesn't. Is a session not ended once a user
closes their browser or does the session not end until the timeout
period
as
set in web.config has passed?

Any help is most appreciated

Sample code is below;

// Extract from Global.asax
protected void Session_Start(Object sender, EventArgs e)
{
Application.Lock();
Application["TotalPlayers"] = (int)Application["TotalPlayers"] + 1;
Application.UnLock();
}

protected void Session_End(Object sender, EventArgs e)
{
Application.Lock();
Application["TotalPlayers"] = (int)Application["TotalPlayers"] - 1;
Application.UnLock();
}

// Extract from web.config
<sessionState
mode="InProc"
stateConnectionString="tcpip=127.0.0.1:42424"
sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes"
cookieless="false"
timeout="5"
/>
 
S

Steve Caliendo

Maybe a client-side java applet would work. The problem is that the browser
does not, and cannot, post back to the server when it's closed. The joys of
Stateless environments are never ending.

Steve

Andrew Banks said:
Is there anyway I can change the value of my app variable then when the
browser is closed then?

Thanks for your input

Steve Caliendo said:
Closing a browser window does not trigger the user session to end. Mainly,
only the session timeout causes the session to end (or a session.abandon)

Steve


Andrew Banks said:
I'm wanting to track the number of users using an application.

I'm using App variables and incrementing an int value by 1 everytime a new
session is started. I'm having a problem decrementing this int once they
close their browser.

I assumed that putting some code in Session_End in Global.asax to do this
would work but it seems it doesn't. Is a session not ended once a user
closes their browser or does the session not end until the timeout
period
as
set in web.config has passed?

Any help is most appreciated

Sample code is below;

// Extract from Global.asax
protected void Session_Start(Object sender, EventArgs e)
{
Application.Lock();
Application["TotalPlayers"] = (int)Application["TotalPlayers"] + 1;
Application.UnLock();
}

protected void Session_End(Object sender, EventArgs e)
{
Application.Lock();
Application["TotalPlayers"] = (int)Application["TotalPlayers"] - 1;
Application.UnLock();
}

// Extract from web.config
<sessionState
mode="InProc"
stateConnectionString="tcpip=127.0.0.1:42424"
sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes"
cookieless="false"
timeout="5"
/>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top