在Global.asax中使用定時器來統(tǒng)計在線人數(shù)和每天每月的訪問量
一、在 ApplicationStart 中創(chuàng)建定時器
//以下為使用多個定時器System.Timers.Timer的處理方法 //用thread重新包一下,定義兩個定時器 System.Threading.Thread myTimer1 = new System.Threading.Thread(new System.Threading.ThreadStart(write1)); myTimer1.Start(); System.Threading.Thread myTimer2 = new System.Threading.Thread(new System.Threading.ThreadStart(write2)); myTimer2.Start();
二、使用定時器每10分鐘更新一次在線人數(shù)檢查一次是否要存入一天流量的信息
//使用第一個定時器,每10分鐘更新一次在線人數(shù) private void write1() { //以下使用System.Timers.Timer類 每間隔10分鐘存一次數(shù)據(jù) System.Timers.Timer myTimer1 = new System.Timers.Timer(600000); //實例化Timer類,設(shè)置間隔時間為600000毫秒(10分鐘存一次總?cè)藬?shù)); myTimer1.Enabled = true; //是否執(zhí)行System.Timers.Timer.Elapsed事件; myTimer1.Elapsed += new System.Timers.ElapsedEventHandler(myTimerElapsed); //到達時間的時候執(zhí)行事件myTimerElapsed; myTimer1.AutoReset = true; //設(shè)置是執(zhí)行一次(false)還是一直執(zhí)行(true); } //使用第二個定時器, private void write2() { //以下使用System.Timers.Timer類 每間隔10分鐘檢查一次是否要存入一天流量的信息 System.Timers.Timer myTimer2 = new System.Timers.Timer(600000); //實例化Timer類,設(shè)置間隔時間為600000毫秒(10分鐘存一次總?cè)藬?shù)); myTimer2.Enabled = true; //是否執(zhí)行System.Timers.Timer.Elapsed事件; myTimer2.Elapsed += new System.Timers.ElapsedEventHandler(myTimerpeopleDay); //到達時間的時候執(zhí)行事件myTimerpeopleDay; myTimer2.AutoReset = true; //設(shè)置是執(zhí)行一次(false)還是一直執(zhí)行(true); }
三、創(chuàng)建 myTimer過程來處理在線人數(shù)和統(tǒng)計每日、月、年的流量
//創(chuàng)建 myTimerElapsed 過程并定義第一個定時器事件,要用來處理在線人數(shù)的代碼 private void myTimerElapsed(object sender, System.Timers.ElapsedEventArgs e) { //如果現(xiàn)在的在現(xiàn)人數(shù)大于原有的在現(xiàn)人數(shù),則替換數(shù)據(jù)表中的在現(xiàn)人數(shù) int MaxOnline = Convert.ToInt32(Application["OnlineMax"]); int MinOnline = Convert.ToInt32(Application["OnlineWhx"]); if (MaxOnline < MinOnline) { SqlConnection con = Db.DB.createconnection(); con.Open(); SqlCommand cmd = new SqlCommand("update countpeople set totol=“" + Application["countSession"].ToString() + "“,OnLine=+“" + Application["onlineWhx"] + "“,DataTimes=“" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "“", con); cmd.ExecuteNonQuery(); con.Close(); Application["OnlineMax"] = Application["OnlineWhx"]; //將現(xiàn)在線人數(shù)賦于OnlineMax Application["dataTimes"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); } else { //將總訪問人數(shù)寫入數(shù)據(jù)庫 SqlConnection con = Db.DB.createconnection(); con.Open(); SqlCommand cmd = new SqlCommand("update countpeople set totol=" + Application["countSession"].ToString(), con); cmd.ExecuteNonQuery(); con.Close(); } } //創(chuàng)建 myTimerpeopleDay 過程并定義第二個定時器事件,要用來統(tǒng)計每日、月、年的流量 private void myTimerpeopleDay(object sender, System.Timers.ElapsedEventArgs e) { try { //當(dāng)天晚上24時 if (DateTime.Now.Hour == 23) { if (DateTime.Now.Minute >= 50) { //當(dāng)天晚上24時,寫入一天的流量 //初始化一個iP數(shù)據(jù)訪問對象 IPControl cont = new IPControl(); //獲取今天訪問量 Int32 countToday = Convert.ToInt32(cont.GetToday()); //獲取本月訪問量 Int32 countMonth = Convert.ToInt32(cont.GetMonth());
//存儲過程名spInsertCountPeopleDay SqlConnection con1 = Db.DB.createconnection(); con1.Open(); SqlCommand cmd1 = new SqlCommand("spInsertCountPeopleDay", con1); cmd1.CommandType = CommandType.StoredProcedure; //存儲過程名
//調(diào)用并設(shè)置存儲過程參數(shù) cmd1.Parameters.Add(new SqlParameter("@peopleDay", SqlDbType.Int)); cmd1.Parameters.Add(new SqlParameter("@dateTimes", SqlDbType.DateTime));
//給參數(shù)賦值 cmd1.Parameters["@peopleDay"].Value = countToday; cmd1.Parameters["@dateTimes"].Value = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
cmd1.ExecuteNonQuery(); con1.Close();
//在一個月的最后一天寫入本月的訪問量 //取本月最后一天(30或者31日) DateTime lastDay = Convert.ToDateTime(DateTime.Now.AddMonths(1).ToString("yyyy-MM-01")).AddDays(-1); int lastDay1 = DateTime.Now.Day; //取當(dāng)前時間的日期 if (lastDay1.ToString() == lastDay.ToString()) //如果前日期等于本月最后一天的日期,則前本月的流量寫入數(shù)據(jù)庫 { SqlConnection conM = Db.DB.createconnection(); conM.Open(); SqlCommand cmdM = new SqlCommand("spInsertCountPeopleMonth", conM); cmdM.CommandType = CommandType.StoredProcedure; //存儲過程名
//調(diào)用并設(shè)置存儲過程參數(shù) cmdM.Parameters.Add(new SqlParameter("@peopleMonth", SqlDbType.Int)); cmdM.Parameters.Add(new SqlParameter("@dateTimeMonth", SqlDbType.DateTime));
//給參數(shù)賦值 cmdM.Parameters["@peopleMonth"].Value = countMonth; cmdM.Parameters["@dateTimeMonth"].Value = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
cmdM.ExecuteNonQuery(); conM.Close(); } } } } catch {
} }
|