• asp.net 定时器


    在使用WinForm编程中,我们可以使用定时器实现规定周期的定时工作进行操作,而在Asp.Net 如何操作,却是个问题。

    而在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; 
            } 
     
            Timer Callbacks#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 
  • 相关阅读:
    html控件使用
    托盤
    托盘的实现
    ws2s函数
    网络验证
    右上角X灰化
    如何模拟一个http请求并把response的内容保存下载下来,导出到excel中(结尾福利)
    排序的几种算法(一):冒泡排序
    python中的break eturnpasscontinue用法
    python中socket模块详解
  • 原文地址:https://www.cnblogs.com/skylaugh/p/343788.html
Copyright © 2020-2023  润新知