title: Can Application property of HttpApplication class be used to calculate current Active Users?
body: <p>I am using the following code in the ASP.NET web forms Global.aspx page to count the number of current active users. It does work when I do local testing.</p>
  <p>Understanding is that Application is an Application-level variable and can be accessed at the application level. So value stored in one session will be available in other sessions.</p>
  <pre><code>&lt;%@ Application Language=&quot;C#&quot; %&gt;  
    
  &lt;script runat=&quot;server&quot;&gt;  
    
      void Application_Start(object sender, EventArgs e)   
      {  
          // Code that runs on application startup  
          Application[&quot;TotalOnlineUsers&quot;] = 0;  
      }  
        
      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  
          Application.Lock();  
          Application[&quot;TotalOnlineUsers&quot;] = (int)Application[&quot;TotalOnlineUsers&quot;] + 1;  
          Application.UnLock();  
      }  
    
      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.  
          Application.Lock();  
          Application[&quot;TotalOnlineUsers&quot;] = (int)Application[&quot;TotalOnlineUsers&quot;] - 1;  
          Application.UnLock();  
      }  
           
  &lt;/script&gt;
  </code></pre>
  <p>But while going through this link <a href="https://docs.microsoft.com/en-us/dotnet/api/system.web.httpapplication?redirectedfrom=MSDN&amp;view=netframework-4.8" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/dotnet/api/system.web.httpapplication?redirectedfrom=MSDN&amp;view=netframework-4.8</a>, it says that <em><strong>member variables can be used to store per-request data</strong></em>.</p>
  <p>Based on this statement and below complete paragraph, there can be multiple instances of HttpApplication for a single application, if not application will be super slow as one HttpApplication instance can process only one request at one point in time</p>
  <p>Because of this, each HttpApplication will have its own Application variable and the count will be saved at HttpApplication level.</p>
  <blockquote>
  <p>Instances of the HttpApplication class are created in the ASP.NET
  infrastructure, not by the user directly. One instance of the
  HttpApplication class is used to process many requests in its
  lifetime. However, it can process only one request at a time. Thus,
  member variables can be used to store per-request data.</p>
  <p>An application raises events that can be handled by custom modules
  that implement the IHttpModule interface or by event handler code that
  is defined in the Global.asax file. Custom modules that implement the
  IHttpModule interface can be put in the App_Code folder or in a DLL in
  the Bin folder.</p>
  </blockquote>
  <p>So, to get the exact count without any chance of miscalculating, should I use static variables?</p>