实验中发现在 ASP.NET 中可以使用计时器(Timer)完成一些定时动作。这一点可能会对我们的一些 Web 程序有益。

下面首先介绍测试使用的一个例子:

  1. 首先在 global.asax 中的 Application_OnStart 事件过程中定义计时器,代码如下:

    [C#] global.asax

    <%@ Application Language="C#" %>
    <%@ Import Namespace="System.Timers" %>

    <script runat="server">

       
    void Application_Start(object sender, EventArgs e)
        {
           
    // 在应用程序启动时运行的代码

            Timer atimer
    = new Timer(10000);

            atimer.Elapsed
    += timer_execute;

            atimer.AutoReset
    = true;
            atimer.Enabled
    = true;
           
            Application.Lock();
            Application[
    "TimeStamp"] = DateTime.Now.ToString();
            Application.UnLock();
        }

       
    void timer_execute(object sender, EventArgs e)  //定时执行的函数
        {
            Application.Lock();
            Application[
    "TimeStamp"] = DateTime.Now.ToString();
            Application.UnLock();
        }

           
          
    </script>

    然后我们简单写一个 test.aspx 来查看 Application("TimeStamp") 的值。代码如下:

  2. C#

    <%
        Response.Write(Application(
    "TimeStamp"))
    %
    >

分析

根 据 global.asax 中的代码,我们设定了一个计时器,每隔 10 秒钟执行一次 Fresher() 过程;在 Fresher() 过程中我们事实上只是重新写入了一个 Application("TimeStamp") 新值。换句话说,Application("TimeStamp") 的值是应该每隔 10 秒钟更新一次的。

是不是这样的呢?通过 test.aspx 的反复刷新观察 Application("TimeStamp") 的值,的确发现这个值在每隔 10 秒地变化一次,而其他时候则保持不变。与我们的预期是一致的。

意义

通 过引入计时器我们可以在 ASP.NET 的全局性程序(Application)中灵活的使用计时器完成一些定时操作,比如:在社区/论坛系统中,每隔 5 分钟更新一次在线用户列表,每隔 1 个小时更新一次用户经验值,或者每隔一天备份一次关键数据等等。这个思路应该是很诱人的。

探讨

Q: 是否在 ASP.NET 代码的任何地方都可以使用计时器呢?
A: 我没有测试过在普通 *.aspx 中插入计时器的情形。但从 B/S 程序的特点来看,即使在 *.aspx 中插入计时器可行,也不是一种好的选择。因为对于 B/S 程序来说,服务器接到客户端的请求本身就是一个事件,在这个事件处理过程中,服务器必须迅速的作出回应,为客户端产生相应的 HTML 代码,然后结束这一过程。如果在 *.aspx 使用计时器(如果允许的话),则第一没有太大必要,第二很容易使系统因为插入的计时器过多(因为每一次 *.aspx 的执行都有可能插入一个新的计时器)而使系统瘫痪。

因此,我建议只在 global.asax 的 Application_OnStart 中使用比较安全一些。欢迎对此感兴趣的朋友对此发表见解。

2.提取了asp.net forums中定时器的应用,并分析了在Asp.Net Forums中,对定时器有如下应用:
1. 更新论坛统计信息
2. 定时索引指定条数的帖子
3. 定时群发队列中的邮件

Forums中对定时器的调用是放在自定义HttpModule的Init方法中(如果您没有使用HttpModule,也可以在Globals.aspx中的Application_OnStart 中调用定时器)。

        // 定时器
        static Timer statsTimer;
       
static Timer emailTimer;

       
// 定时间隔
        private long EmailInterval = ForumConfiguration.GetConfig().ThreadIntervalEmail * 60000;
       
private long StatsInterval = ForumConfiguration.GetConfig().ThreadIntervalStats * 60000;

       
public String ModuleName
           
get { return "ForumsHttpModule"; } 
        }
    


       
// *********************************************************************
       
//  ForumsHttpModule
       
//
        /// <summary>
       
/// Initializes the HttpModule and performs the wireup of all application
       
/// events.
       
/// </summary>
       
/// <param name="application">Application the module is being run for</param>

        public void Init(HttpApplication application)

           
// Wire-up application events
           
//
           
// 略去其他代码
            
            ForumConfiguration forumConfig
= ForumConfiguration.GetConfig();

           
// 如果使用定时器并且定时器还没初始化
            if( forumConfig != null
           
&&  forumConfig.IsBackgroundThreadingDisabled == false ) {
               
if (emailTimer == null)
                   
// 新建定时器
                   
// 新建一个TimerCallback委托,具体要执行的方法在ScheduledWorkCallbackEmailInterval中
                    emailTimer = new Timer(new TimerCallback(ScheduledWorkCallbackEmailInterval), application.Context, EmailInterval, EmailInterval);

               
if( forumConfig.IsIndexingDisabled == false 
               
&&    statsTimer == null ) {
                    statsTimer
= new Timer(new TimerCallback(ScheduledWorkCallbackStatsInterval), application.Context, StatsInterval, StatsInterval);
            }

        }

        }


       
/// <summary>
       
/// 释放定时器
       
/// </summary>

        public void Dispose() {
            statsTimer
= null;
            emailTimer
= null;
        }


       
#region Timer Callbacks
       
/// <summary>
       
/// 定时发送队列中待发送的邮件
       
/// </summary>

        private void ScheduledWorkCallbackEmailInterval (object sender) {
           
try {
               
// 当处理邮件时暂停定时器
                emailTimer.Change( System.Threading.Timeout.Infinite, EmailInterval );

               
// 发送队列中的邮件
               
//
                Emails.SendQueuedEmails( (HttpContext) sender);


               
// 更新匿名用户
               
//
                Users.UpdateAnonymousUsers( (HttpContext) sender);
            }

           
catch( Exception e ) {
                ForumException fe
= new ForumException( ForumExceptionType.EmailUnableToSend, "Scheduled Worker Thread failed.", e );
                fe.Log();
            }

           
finally {
               
// 重新启动定时器
                emailTimer.Change( EmailInterval, EmailInterval );
            }

        }


       
/// <summary>
       
/// 定时索引帖子和定时更新论坛统计信息
       
/// </summary>

        private void ScheduledWorkCallbackStatsInterval(object sender) {
           
try {
               
// 休眠定时器
                statsTimer.Change( System.Threading.Timeout.Infinite, StatsInterval );

               
// 每次索引100篇帖子
               
//
                Search.IndexPosts( (HttpContext) sender, 100);

               
// 更新论坛统计信息
                SiteStatistics.LoadSiteStatistics( (HttpContext) sender, true, 1 );
            }

           
catch( Exception e ) {
                ForumException fe
= new ForumException( ForumExceptionType.UnknownError, "Failure performing scheduled statistics maintenance.", e );
                fe.Log();
            }

           
finally {
               
// 唤醒定时器
                statsTimer.Change( StatsInterval, StatsInterval);
            }

        }

       
#endregion
posted on 2009-03-11 10:56  steven_2005  阅读(1003)  评论(0编辑  收藏  举报