Application 应用程序对象使用介绍

2018-06-18 04:39:38来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

<%@ 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对象

        }
    }
}

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:转载文章----IL反编译利器——Ildasm.exe和Reflector.exe:

下一篇:毛毛雨的博客乐园—内容简介