|
在Global.asax中使用定時(shí)器來(lái)統(tǒng)計(jì)在線人數(shù)和每天每月的訪問(wèn)量
一、在 ApplicationStart 中創(chuàng)建定時(shí)器
//以下為使用多個(gè)定時(shí)器System.Timers.Timer的處理方法 //用thread重新包一下,定義兩個(gè)定時(shí)器 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();
二、使用定時(shí)器每10分鐘更新一次在線人數(shù)檢查一次是否要存入一天流量的信息
//使用第一個(gè)定時(shí)器,每10分鐘更新一次在線人數(shù) private void write1() { //以下使用System.Timers.Timer類 每間隔10分鐘存一次數(shù)據(jù) System.Timers.Timer myTimer1 = new System.Timers.Timer(600000); //實(shí)例化Timer類,設(shè)置間隔時(shí)間為600000毫秒(10分鐘存一次總?cè)藬?shù)); myTimer1.Enabled = true; //是否執(zhí)行System.Timers.Timer.Elapsed事件; myTimer1.Elapsed += new System.Timers.ElapsedEventHandler(myTimerElapsed); //到達(dá)時(shí)間的時(shí)候執(zhí)行事件myTimerElapsed; myTimer1.AutoReset = true; //設(shè)置是執(zhí)行一次(false)還是一直執(zhí)行(true); } //使用第二個(gè)定時(shí)器, private void write2() { //以下使用System.Timers.Timer類 每間隔10分鐘檢查一次是否要存入一天流量的信息 System.Timers.Timer myTimer2 = new System.Timers.Timer(600000); //實(shí)例化Timer類,設(shè)置間隔時(shí)間為600000毫秒(10分鐘存一次總?cè)藬?shù)); myTimer2.Enabled = true; //是否執(zhí)行System.Timers.Timer.Elapsed事件; myTimer2.Elapsed += new System.Timers.ElapsedEventHandler(myTimerpeopleDay); //到達(dá)時(shí)間的時(shí)候執(zhí)行事件myTimerpeopleDay; myTimer2.AutoReset = true; //設(shè)置是執(zhí)行一次(false)還是一直執(zhí)行(true); }
三、創(chuàng)建 myTimer過(guò)程來(lái)處理在線人數(shù)和統(tǒng)計(jì)每日、月、年的流量
//創(chuàng)建 myTimerElapsed 過(guò)程并定義第一個(gè)定時(shí)器事件,要用來(lái)處理在線人數(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 { //將總訪問(wèn)人數(shù)寫(xiě)入數(shù)據(jù)庫(kù) 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 過(guò)程并定義第二個(gè)定時(shí)器事件,要用來(lái)統(tǒng)計(jì)每日、月、年的流量 private void myTimerpeopleDay(object sender, System.Timers.ElapsedEventArgs e) { try { //當(dāng)天晚上24時(shí) if (DateTime.Now.Hour == 23) { if (DateTime.Now.Minute >= 50) { //當(dāng)天晚上24時(shí),寫(xiě)入一天的流量 //初始化一個(gè)iP數(shù)據(jù)訪問(wèn)對(duì)象 IPControl cont = new IPControl(); //獲取今天訪問(wèn)量 Int32 countToday = Convert.ToInt32(cont.GetToday()); //獲取本月訪問(wèn)量 Int32 countMonth = Convert.ToInt32(cont.GetMonth());
//存儲(chǔ)過(guò)程名spInsertCountPeopleDay SqlConnection con1 = Db.DB.createconnection(); con1.Open(); SqlCommand cmd1 = new SqlCommand("spInsertCountPeopleDay", con1); cmd1.CommandType = CommandType.StoredProcedure; //存儲(chǔ)過(guò)程名
//調(diào)用并設(shè)置存儲(chǔ)過(guò)程參數(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();
//在一個(gè)月的最后一天寫(xiě)入本月的訪問(wèn)量 //取本月最后一天(30或者31日) DateTime lastDay = Convert.ToDateTime(DateTime.Now.AddMonths(1).ToString("yyyy-MM-01")).AddDays(-1); int lastDay1 = DateTime.Now.Day; //取當(dāng)前時(shí)間的日期 if (lastDay1.ToString() == lastDay.ToString()) //如果前日期等于本月最后一天的日期,則前本月的流量寫(xiě)入數(shù)據(jù)庫(kù) { SqlConnection conM = Db.DB.createconnection(); conM.Open(); SqlCommand cmdM = new SqlCommand("spInsertCountPeopleMonth", conM); cmdM.CommandType = CommandType.StoredProcedure; //存儲(chǔ)過(guò)程名
//調(diào)用并設(shè)置存儲(chǔ)過(guò)程參數(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 {
} }
|