asp.net web实现定时器功能

来源:互联网 发布:薛之谦 离婚 知乎 编辑:程序博客网 时间:2024/04/30 12:03

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

 基本代码很简单:

Global.asax文件内容:

[csharp] view plaincopy
  1. System.Timers.Timer timer = null;  
  2.    void Application_Start(object sender, EventArgs e)   
  3.    {  
  4.        if (timer != null)  
  5.        {  
  6.            timer.Stop();  
  7.            timer.Close();  
  8.            timer = null;  
  9.        }  
  10.        int Interval = 1000 * 60 * 10;//十分钟  
  11.        timer = new System.Timers.Timer(Interval);//十分钟  
  12.        timer.Elapsed += new System.Timers.ElapsedEventHandler(Send);  
  13.        timer.Interval = Interval;  
  14.        timer.Enabled = true;   
  15.        timer.Start();         
  16.         
  17.        DHC.EAS.Common.LogInfo.Info("当前的应用正被初始化......");  
  18.        // 在应用程序启动时运行的代码6  
  19.       // Castle.ActiveRecord.Framework.Config.XmlConfigurationSource source = new Castle.ActiveRecord.Framework.Config.XmlConfigurationSource("../../appconfig.xml");  
  20.        IConfigurationSource source = System.Configuration.ConfigurationManager.GetSection("activerecord"as IConfigurationSource;  
  21.        Castle.ActiveRecord.ActiveRecordStarter.Initialize(System.Reflection.Assembly.Load("Eas.Entity"), source);//可以针对任何在该程序集下的类    
  22.   
  23.        DHC.EAS.Common.LogInfo.Info("当前的应用初始化完毕.");  
  24.    }  
  25.    public void Send(object sender, System.Timers.ElapsedEventArgs e)  
  26.    {  
  27.        DHC.EAS.Bo.SendMsg.Send();         
  28.    }   
  29.    
  30.    protected void Application_BeginRequest(Object sender, EventArgs e)  
  31.    {  
  32.          
  33.    }       
  34.    void Application_End(object sender, EventArgs e)   
  35.    {  
  36.        //  在应用程序关闭时运行的代码  
  37.        DHC.EAS.Common.LogInfo.Info("当前的应用被关闭");  
  38.        new DHC.EAS.Common.AppException("当前的应用被关闭");  
  39.        if (timer != null)  
  40.        {  
  41.            timer.Stop();  
  42.            timer.Close();  
  43.            timer = null;  
  44.        }  
  45.    }  
  46.          
  47.    void Application_Error(object sender, EventArgs e)   
  48.    {   
  49.        // 在出现未处理的错误时运行的代码  
  50.        Exception ex = Server.GetLastError().GetBaseException();    
  51.        new DHC.EAS.Common.AppException("当前的应用发生错误",ex);  
  52.        //处理完及时清理异常   
  53.        Server.ClearError();          
  54.    }  
  55.   
  56.    void Session_Start(object sender, EventArgs e)   
  57.    {  
  58.        // 在新会话启动时运行的代码  
  59.        //DHC.EAS.Common.LogInfo.Info("回话时间间隔(分钟)" + Session.Timeout.ToString());  
  60.        //DHC.EAS.Common.LogInfo.Info("开始一个Session = " + Session.SessionID);  
  61.    }  
  62.      
  63.   
  64.    void Session_End(object sender, EventArgs e)   
  65.    {  
  66.        // 在会话结束时运行的代码。   
  67.        // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为  
  68.        // InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer   
  69.        // 或 SQLServer,则不会引发该事件。  
  70.        //DHC.EAS.Common.LogInfo.Info("结束一个Session = " + Session.SessionID);  
  71.   
  72.    }  

其实这样做有两点需要考虑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