Reputation: 20468
plz see my global.asax :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using DataLayer;
using NiceFileExplorer.Classes;
using System.IO;
using System.Text;
using System.Data;
using System.Web.Hosting;
namespace NiceFileExplorer
{
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
Application["OnlineUsers"] = 0;
}
protected void Session_Start(object sender, EventArgs e)
{
Application.Lock();
OnlineUsers.InsertRow(
Session.SessionID,
DateTime.Now, //Session Start Time
DBNull.Value, //Session End Time
true);
//OnlineUsers Is A Table In MS SQL SERVER 2008
//InsertRow Is A StoredProcedure Of OnlineUsers Table Like Below :
// ALTER Procedure [dbo].[sp_OnlineUsers_Insert]
// @Session_ID nvarchar(300),
// @Session_Start datetime,
// @Session_End datetime = NULL,
// @Online bit
//As
//Begin
// Insert Into OnlineUsers
// ([Session_ID],[Session_Start],[Session_End],[Online])
// Values
// (@Session_ID,@Session_Start,@Session_End,@Online)
// Declare @ReferenceID int
// Select @ReferenceID = @@IDENTITY
// Return @ReferenceID
//End
Application["OnlineUsers"] = (int)Application["OnlineUsers"] + 1;
Application.UnLock();
}
protected void Session_End(object sender, EventArgs e)
{
Application.Lock();
OnlineUsers.UpdateRow_Some_Fields_By_SessionID(
Session.SessionID,
DateTime.Now,
false);
//UpdateRow_Some_Fields_By_SessionID Is A StoredProcedure Of OnlineUsers Table Like Below :
// ALTER Procedure [dbo].[sp_OnlineUsers_Update_Some_Fields_By_SessionID]
// @Session_ID nvarchar(300),
// @Session_End datetime,
// @Online bit
//As
//Begin
// Update OnlineUsers
// Set
// [Session_End] = @Session_End,
// [Online] = @Online
// Where
// [Session_ID] = @Session_ID
//End
Application["OnlineUsers"] = (int)Application["OnlineUsers"] - 1;
Application.UnLock();
}
protected void Application_End(object sender, EventArgs e)
{
}
}
}
i have to labels in my project for showing OnlineUsers!
Application["OnlineUsers"].ToString();
OnlineUsers.Count_Users().ToString();
//Count_Users Is A StoredProcedure Of OnlineUsers Like Below :
//ALTER Procedure [dbo].[sp_OnlineUsers_Count]
// As
// Begin
// Select
// [Session_ID],
// [Session_Start],
// [Session_End],
// [Online]
// From OnlineUsers
// Where
// [Online] = 1
// Declare @Count int
// Select @Count = @@ROWCOUNT
// Return @Count
// End
SomeTimes Lable 1 Shows Us : 5
but Lable 2 Shows Us : 255
what did i do wrong about them ?
why there is a big difference between them?
EDIT
my sessionState in web.config is like this :
<sessionState mode="InProc" cookieless="false" timeout="1" />
thanks in advance
Upvotes: 0
Views: 472
Reputation: 46067
Session_End
is only fired when the session times out, so that's why the counts don't match. Try using Session.IsNewSession
to key off your Session_End
logic instead.
Upvotes: 2
Reputation: 7797
Most likely your OnlineUsers table is holding a large number of users that never got cleaned up.
Session_End isn't a very reliable event and doesn't run 100% of the time. For instance, if the application stops unexpectedly, it won't run Session_End for each open Session. You would then have a listing of users stuck in OnlineUsers that would never get cleaned up. Application["OnlineUsers"] of course also wouldn't get cleaned up, but if your application was restarting, then it's be set back to 0, so you're not going to notice that big difference there.
Upvotes: 2