<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DemoApplication.aspx.cs" Inherits="WebApplication1.DemoApplication" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div> <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="存放值" />
<asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="取值" />
<br />
<br />
<asp:Button ID="Button3" runat="server" onclick="Button3_Click" Text="上线" />
<br /> </div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading; namespace WebApplication1
{
public partial class DemoApplication : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ } protected void Button1_Click(object sender, EventArgs e)
{
//Application 应用程序对象
//存放值 application 的值存放在内存中,只有在网站停止时结束
//值是公共的
Application["name"] = "习大大";
} protected void Button2_Click(object sender, EventArgs e)
{
string s = Application["name"].ToString();
Response.Write("name存放的值是:" + s);
} protected void Button3_Click(object sender, EventArgs e)
{
Application.Lock();//锁起applicaton对象
int i = ;
if (Application["count"] == null)
{
Application["count"] = ;
}
else
{
i = Convert.ToInt32(Application["count"]);
Thread.Sleep();
i++;
Application["count"] = i;
} Response.Write("当前在线人数是:" + i);
Application.UnLock();//解锁application对象 }
}
}