<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DemoApplication.aspx.cs" Inherits="WebApplication1.DemoApplication" %> <!DOCTYPE html PUBLIC
<%@ 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 = 1; if (Application["count"] == null) { Application["count"] = 1; } else { i = Convert.ToInt32(Application["count"]); Thread.Sleep(3000); i++; Application["count"] = i; } Response.Write("當前線上人數是:" + i); Application.UnLock();//解鎖application對象 } } }