williambirkin

恭喜发财!

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

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

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

        public void Init(HttpApplication application)
        
{

            

            
//创建定时器
            if (forumConfig != null && forumConfig.IsBackgroundThreadingDisabled == false)
            
{
                
if (emailTimer == null)
                    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);
                }

            }

        }

        private void ScheduledWorkCallbackEmailInterval(object sender)
        
{
            
try
            
{
                
// 停止timer当邮件处理完成
                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
            
{
                
//当邮件处理完成后,重新启动timer。
                emailTimer.Change(EmailInterval, EmailInterval);
            }

        }

posted on 2006-12-21 11:41  williambirkin  阅读(209)  评论(0编辑  收藏  举报