asp.net web定时器实现及注意事项

在web程序上实现定时器,有一定难度,浏览器端其实只需要使用js的定时器就可以实现,但服务端如何实现呢?都是使用Global.asax加Timer 实现的,但很多文章却没有提到这种设计的问题。

基本代码很简单:

Global.asax文件内容:

System.Timers.Timer timer = null;void Application_Start(object sender, EventArgs e){if (timer != null){timer.Stop();timer.Close();timer = null;}int Interval = 1000 * 60 * 10;//十分钟timer = new System.Timers.Timer(Interval);//十分钟timer.Elapsed += new System.Timers.ElapsedEventHandler(Send);timer.Interval = Interval;timer.Enabled = true;timer.Start();DHC.EAS.Common.LogInfo.Info("当前的应用正被初始化……");// 在应用程序启动时运行的代码6// Castle.ActiveRecord.Framework.Config.XmlConfigurationSource source = new Castle.ActiveRecord.Framework.Config.XmlConfigurationSource("../../appconfig.xml");IConfigurationSource source = System.Configuration.ConfigurationManager.GetSection("activerecord") as IConfigurationSource;Castle.ActiveRecord.ActiveRecordStarter.Initialize(System.Reflection.Assembly.Load("Eas.Entity"), source);//可以针对任何在该程序集下的类DHC.EAS.Common.LogInfo.Info("当前的应用初始化完毕.");}public void Send(object sender, System.Timers.ElapsedEventArgs e){DHC.EAS.Bo.SendMsg.Send();}protected void Application_BeginRequest(Object sender, EventArgs e){}void Application_End(object sender, EventArgs e){// 在应用程序关闭时运行的代码DHC.EAS.Common.LogInfo.Info("当前的应用被关闭");new DHC.EAS.Common.AppException("当前的应用被关闭");if (timer != null){timer.Stop();timer.Close();timer = null;}}void Application_Error(object sender, EventArgs e){// 在出现未处理的错误时运行的代码Exception ex = Server.GetLastError().GetBaseException();new DHC.EAS.Common.AppException("当前的应用发生错误",ex);//处理完及时清理异常Server.ClearError();}void Session_Start(object sender, EventArgs e){// 在新会话启动时运行的代码//DHC.EAS.Common.LogInfo.Info("回话时间间隔(分钟)" + Session.Timeout.ToString());//DHC.EAS.Common.LogInfo.Info("开始一个Session = " + Session.SessionID);}void Session_End(object sender, EventArgs e){// 在会话结束时运行的代码。// 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为// InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer// 或 SQLServer,则不会引发该事件。//DHC.EAS.Common.LogInfo.Info("结束一个Session = " + Session.SessionID);}其实这样做有两点需要考虑1Application_Start必须发生,,2 IIS可能进行回收,导致发生Application_End事件

其实还有人提出使用HttpRuntime.Cache,就是添加一个变量,设定一个过期时间,在过期时通过回调函数调用需要的操作。这种方式能不能用,我没有测试,理论上可行,但个人感觉还是会存在上边的问题。

其实这种定时操作,我个人还是建议使用服务程序去处理。

HttpRuntime.Cache定时参考

版权声明:本文为博主原创文章,未经博主允许不得转载。

在乎的应该是沿途的风景以及看风景的心情。

asp.net web定时器实现及注意事项

相关文章:

你感兴趣的文章:

标签云: