asp.net web实现定时器功能
来源:互联网 发布:薛之谦 离婚 知乎 编辑:程序博客网 时间:2024/04/30 12:03
在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);
- }
其实这样做有两点需要考虑1 Application_Start必须发生,2 IIS可能进行回收,导致发生Application_End事件
1Application_Start必须发生
要求必须有用户访问网页,如果没有人访问网页,则Application_Start不会发生,timer不会启动,定时也就无从谈起。
2Application_End事件
例如发布了文件,修改了webconfig等都会导致Application_End事件,当然还有长时间没人访问,IIS的回收机制也会导致。然后timer也就不存在了,定时也就无从谈起。其实在这个之后可以通过代码访问自己的网站,保证Application_End事件发生后,Application_Start发生。
其实还有人提出使用HttpRuntime.Cache,就是添加一个变量,设定一个过期时间,在过期时通过回调函数调用需要的操作。这种方式能不能用,我没有测试,理论上可行,但个人感觉还是会存在上边的问题。
其实这种定时操作,我个人还是建议使用服务程序去处理。
HttpRuntime.Cache定时参考
0 0
- asp.net web实现定时器功能
- asp.net web定时器实现及注意事项
- Asp.net 实现验证码功能的Web控件
- Asp.net 实现验证码功能的Web控件
- asp.net web 打印功能
- asp.net实现pop功能
- asp.net实现pop功能
- asp.net实现pop功能
- asp.net实现pop功能
- asp.net实现下载功能
- ASP.NET手机短信功能实现
- <asp.net> 实现返回功能
- asp.net实现pop功能
- asp.net实现编辑器功能
- ASP.NET ScriptManager 帮助您实现 Web 应用程序的 AJAX 功能
- asp.net 定时器
- asp.net 定时器
- asp.net Cache定时器
- Http请求中Content-Type讲解以及在Spring MVC中的应用
- 观察者模式
- java面向对象总结
- 2015-09-18
- mysql 添加时间戳
- asp.net web实现定时器功能
- String
- 利用正则表达式获取地址栏参数
- codeforces 578C Weakness and Poorness
- 【JAVA】数据库中一些ResultSet的用法实例
- T568A标准和T568B标准
- 黑马程序员-Java基础学习第五天总结
- 例题 6-11 四分树 UVa 297
- 画中仙