博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Application对象
阅读量:4703 次
发布时间:2019-06-10

本文共 2537 字,大约阅读时间需要 8 分钟。

使用application对象显示在线人数,Global.asax文件

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Security;using System.Web.SessionState;namespace ManageStudent{    public class Global : System.Web.HttpApplication    {        protected void Application_Start(object sender, EventArgs e)        {            //在应用程序启动时运行的代码            //初始化变量:UserCount()            Application.Lock();//临界变量,使用加锁功能            Application["UserCount"] = 0;            Application.UnLock();//临界变量被解锁        }        protected void Session_Start(object sender, EventArgs e)        {            //在新会话启动时运行的代码            //用户数量加1            Application.Lock();            Application["UserCount"] = Int32.Parse(Application["UserCount"].ToString()) + 1;            Application.UnLock();        }        protected void Application_BeginRequest(object sender, EventArgs e)        {        }        protected void Application_AuthenticateRequest(object sender, EventArgs e)        {        }        protected void Application_Error(object sender, EventArgs e)        {        }        protected void Session_End(object sender, EventArgs e)        {            //在新会话结束时运行的代码            //注意:只有在web.config文件中的sessionstate模式设置为InProc时。            //如果会话模式设置为StateServer或SQLServer,则不会引发该事件            //用户数量减一            Application.Lock();            Application["UserCount"] = Int32.Parse(Application["UserCount"].ToString())- 1;            Application.UnLock();        }        protected void Application_End(object sender, EventArgs e)        {        }    }}

接下来,需要在web.config配置文件中<system,web>标签内添加如下一条语句

 <sessionState mode="InProc"/>

在ApplicationDemo.aspx中写入代码

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ApplicationDemo.aspx.cs" Inherits="ManageStudent.ApplicationDemo" %>        

 

 

在cs文件中,写入代码,用于显示在线人数

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;namespace ManageStudent{    public partial class ApplicationDemo : System.Web.UI.Page    {        protected void Page_Load(object sender, EventArgs e)        {            if (!Page.IsPostBack)            {                lblUserCount.Text = "网站在线人数:" + Application["UserCount"] + "人";            }        }        protected void btnLogout_Click(object sender, EventArgs e)        {            //销毁Session            Session.Abandon();        }    }}

 

转载于:https://www.cnblogs.com/niexianxue/p/4439746.html

你可能感兴趣的文章
[转]大型网站系统架构的演化
查看>>
非常好的JSUI
查看>>
基于EasyNVR摄像机无插件直播流媒体服务器实现类似于单点登录功能的免登录直播功能...
查看>>
python学习0day
查看>>
函数指针的使用
查看>>
[转]javascript实现限制上传文件的大小
查看>>
控制台应用程序窗口无法输入汉字解决办法
查看>>
一个关于session的问题
查看>>
加快开发时间的8个CSS的预处理程序
查看>>
dom元素高度、屏幕高度 获取
查看>>
asp.net 设置session失效时间
查看>>
杭电多校第四场 E Matrix from Arrays
查看>>
ReactiveCocoa操作方法-线程\时间
查看>>
oracle 分析函数
查看>>
CHD-5.3.6集群上sqoop安装
查看>>
解决无/var/log/messages 问题
查看>>
js 判断是不是空、值是否存在
查看>>
分布式一致性协议-2PC与3PC(二)
查看>>
SCP-bzoj-1079
查看>>
Python 实践项目 游戏
查看>>