asp.net web实现定时器功能

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

 基本代码很简单:

Global.asax文件内容:

[csharp]  view plain copy
  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
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ASP.NET 提供了一个名为 Timer 的控件,用于在 Web 应用程序中实现定时器功能。Timer 控件是一个 AJAX 控件,可以在页面上启用客户端回发以及服务器端事件处理程序。 要使用 Timer 控件,首先需要将它添加到页面上,并设置其 Interval 属性来指定定时器的时间间隔,单位为毫秒。然后,可以在定时器的 Tick 事件处理程序中编写需要定期执行的代码。 以下是一个简单的示例,演示如何在 ASP.NET 页面中使用 Timer 控件: ```ASP.NET <asp:ScriptManager runat="server"></asp:ScriptManager> <asp:UpdatePanel runat="server"> <ContentTemplate> <asp:Timer ID="Timer1" runat="server" Interval="5000" OnTick="Timer1_Tick"></asp:Timer> <asp:Label ID="Label1" runat="server" Text=""></asp:Label> </ContentTemplate> </asp:UpdatePanel> ``` 在上述示例中,我们首先添加了一个 ScriptManager 控件,用于启用 AJAX 支持。然后,我们在 UpdatePanel 中放置了 Timer 控件和一个 Label 控件。每隔 5 秒,Timer 控件会触发 Timer1_Tick 事件,我们可以在事件处理程序中更新 Label 控件的文本。 在代码后台,我们需要定义 Timer1_Tick 事件处理程序: ```C# protected void Timer1_Tick(object sender, EventArgs e) { Label1.Text = DateTime.Now.ToString(); } ``` 在上述事件处理程序中,我们简单地将当前时间显示在 Label1 控件中。 这只是一个简单的示例,你可以根据实际需求编写更复杂的定时器功能。记得在页面生命周期中合适的地方启用和停用 Timer 控件,以避免不必要的资源消耗。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值