One of the most classic methods of application: Counting the number of people online, which requires the use of our global application class to count logged-in user information:
here is the code snippet: void Application_Start (object sender, EventArgs e) { Code to run when the application starts application["Count"] = 0; System.IO.FileStream fs = System.io.file.open (Server.MapPath ("Count.txt"), system.io.filemode.openorcreate); System.IO.StreamReader sr = new System.IO.StreamReader (FS); application["allusers"] = Convert.ToInt32 (Sr.readline ()); Sr.close (); Fs.close (); } void Application_End (object sender, EventArgs e) { Code to run when the application shuts down } void Application_Error (object sender, EventArgs e) { Code to run when an unhandled error occurs } void Session_Start (object sender, EventArgs e) { Code to run when a new session is started Application.Lock (); application["Count"] = Convert.ToInt32 (application["Count"]) + 1; application["allusers"] = Convert.ToInt32 (application["AllUsers"]) + 1; System.IO.FileStream fs = new System.IO.FileStream ("Count.txt", System.io.filemode.openorcreate, System.io.fileaccess.readwrite); system.io.streamwriter SW = new System.IO.StreamWriter (FS); Sw.writeline (application["allusers"]); Sw.close (); Fs.close (); Application.UnLock (); } void Session_End (object sender, EventArgs e) { The code that runs at the end of the session. Note: www.3ppt.com raises the Session_End event only if the sessionstate mode in the Web. config file is set to InProc. If the session mode is set to StateServer or SQL Server, the event is not raised. Application.Lock (); application["Count"] = Convert.ToInt32 (application["Count"])-1; Application.UnLock (); } Page just read out the content of application: Response.Write (application["Count"]+ "Current number" + " Total number: "+application[" allusers "]); |