Asp.net中定期对Application的更新

我这里使用的是vs2008作为开发工具,使用的是C#语言

1。建立好Global.asax文件,这个文件在VS2008中是需要自己添加的

2。在Global.asax的Application.start中声明一个timer,然后书写代码

代码如下

Using System.Timer;

protected void Application_Start(object sender, EventArgs e)

{

Application["MyApp"] = MyApp;

Timer GetNewDataTimer = new Timer();

GetNewDataTimer.Interval = 1000;

//声明时间到时的事件,事件委托

GetNewDataTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);

GetNewDataTimer.AutoReset = true;

GetNewDataTimer.Enabled = true;

GetNewDataTimer.Start();

}

protected void OnTimedEvent(object ob, System.Timers.ElapsedEventArgs e)

{

Address_DataLayer.DsPositionAndStaff = Address_DataLayer.GetAllPositionAndStaffDataSet(true);

Application["AllPositionAndStaff"] = Address_DataLayer.DsPositionAndStaff;

}

其实就是怎么简单,Global.asax就类似一个类文件,所不同的他是全局类,无论前台如何,它都回做他自己的工作。