Application--在线统计人数
时间:2011-01-24 来源:hfliyi
代码
1 void Application_Start(object sender, EventArgs e)
2 {
3 //在应用程序启动时运行的代码
4 Application["Count"] = 0;
5 System.IO.FileStream fs = System.IO.File.Open(Server.MapPath("Count.txt"), System.IO.FileMode.OpenOrCreate);
6 System.IO.StreamReader sr = new System.IO.StreamReader(fs);
7 Application["AllUsers"] = Convert.ToInt32(sr.ReadLine());
8 sr.Close();
9 fs.Close();
10 }
11 void Application_End(object sender, EventArgs e)
12 {
13 //在应用程序关闭时运行的代码
14
15 }
16 void Application_Error(object sender, EventArgs e)
17 {
18 //在出现未处理的错误时运行的代
19
20 }
21 void Session_Start(object sender, EventArgs e)
22 {
23 //在新会话启动时运行的代码
24 Application.Lock();
25 Application["Count"] = Convert.ToInt32(Application["Count"]) + 1;
26 Application["AllUsers"] = Convert.ToInt32(Application["AllUsers"]) + 1;
27 System.IO.FileStream fs = new System.IO.FileStream("Count.txt", System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite);
28 System.IO.StreamWriter sw = new System.IO.StreamWriter(fs);
29 sw.WriteLine(Application["AllUsers"]);
30 sw.Close();
31 fs.Close();
32 Application.UnLock();
33 }
34
35 void Session_End(object sender, EventArgs e)
36 {
37 Application.Lock();
38 Application["Count"] = Convert.ToInt32(Application["Count"]) - 1;
39 Application.UnLock();
40
41 }
页面只要读出Application的内容就行啦:
Response.Write(Application["Count"]+"当前人数"+"<br/>总人数:"+Application["AllUsers"]);
PS:Application运行在服务端
相关阅读 更多 +