博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

统计在线人数

Posted on 2007-03-16 16:50  小飞龙(Jack)  阅读(200)  评论(0编辑  收藏  举报

 方法

   在"Global.asax"文件中添加下面事件

    void Application_Start(object sender, EventArgs e)
    {

        Application["count"] = 0;    //初始化  
    }

    void Session_Start(object sender, EventArgs e)
    {
        //对Application加琐以防并行性 
        Application.Lock();
        //添加一个在线人数     
        Application["Count"] = (int)Application["Count"] + 1;
        //解琐
        Application.UnLock();
    }

     void Session_End(object sender, EventArgs e)
    {
        //对Application加琐以防并行性 
        Application.Lock();
        //减少一个在线人数 
        Application["Count"] = (int)Application["Count"] - 1;
        //解琐
        Application.UnLock();
    }

void Page_Load(Object sender , EventArgs e)

{

  lblCount.Text = Application[ "Count" ].ToString();

}