我用vb.net写得网站。里面有一个简易得记录当前在线人数代码,如下:
‘global.aspx.vb
  Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
        ' 在应用程序启动时激发
        Application("CurrentUsers") = 0
    End Sub    Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
        ' 在会话启动时激发
         Application.lock()
        Application("CurrentUsers") = Application("CurrentUsers") + 1
        Application.unlock()
  sub ession_End(ByVal sender As Object, ByVal e As EventArgs)
        ' 在会话结束时激发
        Application.Lock()
        Application("CurrentUsers") = Application("CurrentUsers") - 1
        Application.Unlock()
    End Sub  'web.config
    <sessionState 
            mode="InProc"
            stateConnectionString="tcpip=127.0.0.1:42424"
            sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes"
            cookieless="true" 
            timeout="20" 
    />
在显示页面中index.aspx
     label1.text=<%Application("CurrentUsers") %>  
问题就出现了:.net网站计数只增不减。不管用户是否下线。
请问这是怎么回事?

解决方案 »

  1.   

    當一個用戶訪問網站時,會話開始,在線人數+1.
    當session過期時,會話結束,在線人數-1.但是在同時,該客戶的瀏覽器仍在訪問網站,因此會立即產生一個新的會話,在線人數+1!
    因此實際上Application("CurrentUsers")並沒有減小
      

  2.   

    boblaw :能否说详细点。急!!!
      

  3.   

    這個方法肯定不是好的方法.
    可以這樣做:在數據庫中增加一個online表,所以在線的都有一條記錄,並有一個字段為更新日間,更新時間小於10分鐘(自己設置)的就刪除.
    方法有很多,也可以使用cookies等,但是都不是非常準確.
      

  4.   


    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Web;
    using System.Web.SessionState;
    using System.Diagnostics;
    namespace Example 
    {
    /// <summary>
    /// Global 的摘要说明。
    /// </summary>
    public class Global : System.Web.HttpApplication
    {
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.IContainer components = null; public Global()
    {
    InitializeComponent();
    }

    protected void Application_Start(Object sender, EventArgs e)
    {
    Application["counter"] = 0;
    }
     
    protected void Session_Start(Object sender, EventArgs e)
    {
    Application.Lock();
    Application["counter"] = (int)Application["counter"] + 1;
    Application.UnLock();
    } protected void Application_BeginRequest(Object sender, EventArgs e)
    { } protected void Application_EndRequest(Object sender, EventArgs e)
    { } protected void Application_AuthenticateRequest(Object sender, EventArgs e)
    { } protected void Application_Error(Object sender, EventArgs e)
    {
    String Message = "\n\nURL:\n http://localhost/" + Request.Path
    + "\n\nMESSAGE:\n " + Server.GetLastError().Message
    + "\n\nSTACK TRACE:\n" + Server.GetLastError().StackTrace;
    String LogName = "Application";
    if (!EventLog.SourceExists(LogName)) 
    {
    EventLog.CreateEventSource(LogName, LogName);
    }
    EventLog Log = new EventLog();
    Log.Source = LogName;
    Log.WriteEntry(Message, EventLogEntryType.Error);
    } protected void Session_End(Object sender, EventArgs e)
    {
    Application.Lock();
    Application["counter"] = (int)Application["counter"] - 1;
    Application.UnLock();
    } protected void Application_End(Object sender, EventArgs e)
    { }

    #region Web 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.components = new System.ComponentModel.Container();
    }
    #endregion
    }
    }