Find Online number of Users on a Website

This code snippet shows how to count number of users who are visiting a website.

Here is code in global.asax.cs file.

 public class Global : System.Web.HttpApplication
    {

        void Application_Start(object sender, EventArgs e)
        {
            int count=0;
            // Code that runs on application startup
            Application["OnlineUsers"] = count;

        }

        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)
        {
            Session["start"] = DateTime.Now;
            Application.Lock();

            Application["OnlineUsers"] = Convert.ToInt32(Application["OnlineUsers"]) + 1;

            Application.UnLock();
            // Code that runs when a new session is started

        }

        void Session_End(object sender, EventArgs e)
        {
            Application.Lock();
            Application["OnlineUsers"] = (int)Application["OnlineUsers"] - 1;
            Application.UnLock();
            // 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.

        }


you can see the number of users are visited on UI in page_load event this code.


int UserCount=Convert.ToInt32(Application["OnlineUsers"]);
            Label2.Text = UserCount.ToString();
            Label4.Text = Session["start"].ToString();


    }