您的位置:首页 > 其它

在Global.asax中使用定时器来统计在线人数和每天每月的访问量

2009-06-01 14:45 531 查看
一、在 Application_Start 中创建定时器

//以下为使用多个定时器System.Timers.Timer的处理方法
//用thread重新包一下,定义两个定时器
System.Threading.Thread myTimer_1 = new System.Threading.Thread(new System.Threading.ThreadStart(write_1));
myTimer_1.Start();
System.Threading.Thread myTimer_2 = new System.Threading.Thread(new System.Threading.ThreadStart(write_2));
myTimer_2.Start();

二、使用定时器每10分钟更新一次在线人数检查一次是否要存入一天流量的信息

//使用第一个定时器,每10分钟更新一次在线人数
private void write_1()
{
//以下使用System.Timers.Timer类 每间隔10分钟存一次数据
System.Timers.Timer myTimer1 = new System.Timers.Timer(600000); //实例化Timer类,设置间隔时间为600000毫秒(10分钟存一次总人数);
myTimer1.Enabled = true; //是否执行System.Timers.Timer.Elapsed事件;
myTimer1.Elapsed += new System.Timers.ElapsedEventHandler(myTimer_Elapsed); //到达时间的时候执行事件myTimer_Elapsed;
myTimer1.AutoReset = true; //设置是执行一次(false)还是一直执行(true);
}
//使用第二个定时器,
private void write_2()
{
//以下使用System.Timers.Timer类 每间隔10分钟检查一次是否要存入一天流量的信息
System.Timers.Timer myTimer2 = new System.Timers.Timer(600000); //实例化Timer类,设置间隔时间为600000毫秒(10分钟存一次总人数);
myTimer2.Enabled = true; //是否执行System.Timers.Timer.Elapsed事件;
myTimer2.Elapsed += new System.Timers.ElapsedEventHandler(myTimer_peopleDay); //到达时间的时候执行事件myTimer_peopleDay;
myTimer2.AutoReset = true; //设置是执行一次(false)还是一直执行(true);
}

三、创建 myTimer过程来处理在线人数和统计每日、月、年的流量

//创建 myTimer_Elapsed 过程并定义第一个定时器事件,要用来处理在线人数的代码
private void myTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
//如果现在的在现人数大于原有的在现人数,则替换数据表中的在现人数
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"]; //将现在线人数赋于OnlineMax
Application["dataTimes"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
}
else
{
//将总访问人数写入数据库
SqlConnection con = Db.DB.createconnection();
con.Open();
SqlCommand cmd = new SqlCommand("update countpeople set totol=" + Application["countSession"].ToString(), con);
cmd.ExecuteNonQuery();
con.Close();
}
}
//创建 myTimer_peopleDay 过程并定义第二个定时器事件,要用来统计每日、月、年的流量
private void myTimer_peopleDay(object sender, System.Timers.ElapsedEventArgs e)
{
try
{
//当天晚上24时
if (DateTime.Now.Hour == 23)
{
if (DateTime.Now.Minute >= 50)
{
//当天晚上24时,写入一天的流量
//初始化一个iP数据访问对象
IPControl cont = new IPControl();
//获取今天访问量
Int32 countToday = Convert.ToInt32(cont.GetToday());
//获取本月访问量
Int32 countMonth = Convert.ToInt32(cont.GetMonth());

//存储过程名sp_InsertCountPeopleDay
SqlConnection con1 = Db.DB.createconnection();
con1.Open();
SqlCommand cmd1 = new SqlCommand("sp_InsertCountPeopleDay", con1);
cmd1.CommandType = CommandType.StoredProcedure; //存储过程名

//调用并设置存储过程参数
cmd1.Parameters.Add(new SqlParameter("@peopleDay", SqlDbType.Int));
cmd1.Parameters.Add(new SqlParameter("@dateTimes", SqlDbType.DateTime));

//给参数赋值
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; //取当前时间的日期
if (lastDay1.ToString() == lastDay.ToString()) //如果前日期等于本月最后一天的日期,则前本月的流量写入数据库
{
SqlConnection conM = Db.DB.createconnection();
conM.Open();
SqlCommand cmdM = new SqlCommand("sp_InsertCountPeopleMonth", conM);
cmdM.CommandType = CommandType.StoredProcedure; //存储过程名

//调用并设置存储过程参数
cmdM.Parameters.Add(new SqlParameter("@peopleMonth", SqlDbType.Int));
cmdM.Parameters.Add(new SqlParameter("@dateTimeMonth", SqlDbType.DateTime));

//给参数赋值
cmdM.Parameters["@peopleMonth"].Value = countMonth;
cmdM.Parameters["@dateTimeMonth"].Value = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

cmdM.ExecuteNonQuery();
conM.Close();
}
}
}
}
catch
{

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: