How to get the online Users Count in ASP.NETIntroduction
Today I wish to get the how many users in online dotnetlogix.com, base on that idea, I come cross to a simple and easy solution to implement this.
Implementation
Basically we have to count no of users or visitors online in the whole application, based on that we have to use the Application object in ASP.NET to keep the count or read the count as well.
But next question when we have to increase count by one visitor enter into www.dotnetlogixin.com? It’s simple, because when every visitor enters into site, IIS will create a session object for users. So we can increase count by one when session is start.Same we can reduce the count by one when session is end.
Now we have to compose the code to implement what we are discusses above.
Step 1
Create a web project using visual studio 2010 or 2008.
Step 2
Then right click on your web project and then click add new Item -> then select the Global Application Class from template list window.
It will look like following,
<%@ Application Language="C#" %>
<script runat="server">
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
}
void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown
}
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
}
void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started
}
void Session_End(object sender, EventArgs e)
{
// Code that runs when a session ends. // Note: The Session_End event is raised only when the sessionstate mode // is set to InProc in the Web.config file. If session mode is set to StateServer // or SQLServer, the event is not raised.
}
</script>
Step 3
Now we have to add code for first portion of the implementation. Its mean when application start, we will set no of online users is 0.
Code
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
Application["CurrentUsers"] = 0;
}
Then we have to increase count by one when users come into site.
Code
void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started
Application.Lock();
Application["CurrentUsers"] = (int)Application["CurrentUsers"] + 1;
Application.UnLock();
}
In above code there is small tricky pint when we are use the Application object to update point. The Web application is multi thread application so, when we are trying to common object, we have to lock first that object then we have to update and then finally we have to release as well.
lock the application object,
Application.Lock();
Release application object
Application.UnLock();
Third part of this implementation, just we have to decrease count by one when session is end.
Code
void Session_End(object sender, EventArgs e)
{
// Code that runs when a session ends. // Note: The Session_End event is raised only when the sessionstate mode // is set to InProc in the Web.config file. If session mode is set to StateServer // or SQLServer, the event is not raised. // Code that runs when a new session is started
Application.Lock();
if ((int)Application["CurrentUsers"] > 0)
{
Application["CurrentUsers"] = (int)Application["CurrentUsers"] - 1;
}
Application.UnLock();
}
Finally, you have to display this count. If you have master page, then you can add simple in top left corner or right corner in within the div or label object as like follows,
Online Users: <%=Application ["CurrentUsers"].ToString()%>
That’s all. If want to refresh this count based on the interval, then you can use the timer to update label.
Thank you for reading.