添加全局應用程序類項目Global.asax,在文件中做如下修改
void Application_Start(object sender, EventArgs e)
{
string str1 = "";
string path = Server.MapPath("~/count.txt");
System.IO.StreamReader str =new System.IO.StreamReader(path);
string ss = str.ReadLine();
int x = Int32.Parse(ss);
//在應用程序啓動時運行的代碼
Application.Lock(); //臨界變量,使用加鎖功能,其他用戶不能訪問。
Application["UserCount"] = 0;
Application.UnLock(); //臨界變量被解鎖。
Application.Lock(); //臨界變量,使用加鎖功能,其他用戶不能訪問。
Application["StatCount"] = x;
Application.UnLock(); //臨界變量被解鎖。
}
void Application_End(object sender, EventArgs e)
{
//在應用程序關閉時運行的代碼
// string path = Server.MapPath("~/count.txt");
// string str=Application["StatCount"].ToString() ;
}
void Application_Error(object sender, EventArgs e)
{
//在出現未處理的錯誤時運行的代碼
}
void Session_Start(object sender, EventArgs e)
{
//在新會話啓動時運行的代碼
Application.Lock(); //臨界變量,使用加鎖功能,其他用戶不能訪問。
Application["UserCount"] = Int32.Parse(Application["UserCount"].ToString()) + 1;
Application.UnLock(); //臨界變量被解鎖。
//測試某一頁的訪問量※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※
// String pageurl = Request.Url.ToString();//獲取用戶訪問的頁面
// if(pageurl.EndsWith ("index.aspx")) //判斷訪問的是否是默認頁
// {
//鎖定變量
Application.Lock();
//頁面訪問量加一
Application["StatCount"] =Int32.Parse(Application["StatCount"].ToString()) + 1;
//解鎖
Application.UnLock();
// }
}
void Session_End(object sender, EventArgs e)
{
//在會話結束時運行的代碼。
Application.Lock();
Application["UserCount"] = Int32.Parse(Application["UserCount"].ToString()) - 1;
Application.UnLock();
}