当前位置: 移动技术网 > IT编程>开发语言>.net > asp.net web定时器实现及注意事项

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

2018年01月26日  | 移动技术网IT编程  | 我要评论

陈百强死亡现场照片,复方甘草片价格,ca7022

 

在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定时参考

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网