
1、如果直接在页面中拖入Timer控件,则Response不会有反应,即Response.Write()或Response.Redirect不能获得正确结果
2、应该如下使用:
Global.cs
protected void Application_Start(Object sender, EventArgs e)
{
System.Timers.Timer timer1 = new System.Timers.Timer();
timer1.Interval = 1000; // 30000 毫秒 = 30秒
timer1.Elapsed += new System.Timers.ElapsedEventHandler(timer1_Elapsed);
timer1.AutoReset = true;
timer1.Enabled = true;
timer1.Start();
Application.Lock();
// 第一次先初始化
Application["thetime"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
Application.UnLock();
}
static int i = 0;
void timer1_Elapsed(object source, System.Timers.ElapsedEventArgs e)
{
i++;
Application.Lock();
// 可以读取数据库内容,对缓存十分有用,这里用简单的例子
Application["thetime"] = i.ToString();
Application.UnLock();
}
在具体页面中:
Response.Write(Application["thetime"].ToString());
可以使用页面定时刷新来实现1秒显示Response.Write(Application["thetime"].ToString());一次
nbsp;
发表于 @ 2008年04月19日 15:41:00|评论(loading...)|编辑