宝玉的blog

专注于web开发技术
随笔 - 78, 评论 - 1563, 引用 - 157

导航

关于


目前致力于ChinaCommunityServer的开发。

msn: junminliu(at)msn.com

标签

每月存档

最新留言

  • re:Openlab V2.0 Beta
    <p>宝玉你好: &nbsp; &nbsp; &nbsp; 我是个.net新手,最近看了openlab(openlab_V2.0_Beta)的源码。 ...
    by isforge(注册) on 2009/6/28 10:10:37
  • re:Openlab V2.0 Beta
    <p>宝玉你好: &nbsp; &nbsp; &nbsp; 我是个.net新手,最近看了openlab(openlab_V2.0_Beta)的源码。 ...
    by isforge(注册) on 2009/6/28 10:10:31
  • re:Openlab V2.0 Beta
    <p>宝玉你好: &nbsp; &nbsp; &nbsp; 我是个.net新手,最近看了openlab(openlab_V2.0_Beta)的源码。 ...
    by isforge(注册) on 2009/6/28 10:10:30
  • re:Openlab V2.0 Beta
    <p>宝玉你好: &nbsp; &nbsp; &nbsp; 我是个.net新手,最近看了openlab(openlab_V2.0_Beta)的源码。 ...
    by isforge(注册) on 2009/6/28 10:10:29
  • re:Openlab V2.0 Beta
    <p>宝玉你好: &nbsp; &nbsp; &nbsp; 我是个.net新手,最近看了openlab(openlab_V2.0_Beta)的源码。 ...
    by isforge(注册) on 2009/6/28 10:10:25
  • re:Openlab V2.0 Beta
    <p>宝玉你好: &nbsp; &nbsp; &nbsp; 我是个.net新手,最近看了openlab(openlab_V2.0_Beta)的源码。 ...
    by isforge(注册) on 2009/6/28 10:10:25
  • re:Openlab V2.0 Beta
    <p>宝玉你好: &nbsp; &nbsp; &nbsp; 我是个.net新手,最近看了openlab(openlab_V2.0_Beta)的源码。 ...
    by isforge(注册) on 2009/6/28 10:10:25
  • re:Openlab V2.0 Beta
    <p>宝玉你好: &nbsp; &nbsp; &nbsp; 我是个.net新手,最近看了openlab(openlab_V2.0_Beta)的源码。 ...
    by isforge(注册) on 2009/6/28 10:10:25
  • re:Silverlight中,防止ComboBox抢焦点
    在家”用网路”赚全世界的钱! 这是真正实现跨国事业最好的机制。藉由网路无远弗届的力量, 让全球超过180个国家变成一个单一市场,在你加入的那一刻, 网路能到达的地方,就是你收入能到达的地方。 ...
    by jackielongteng(注册) on 2009/6/14 13:19:48
  • re:Silverlight中,防止ComboBox抢焦点
    <p>我是初学者,您已经写了一个 组件上传的功能 。。我在2008下测试通过,,,但是弄2005测试的时候 发现 progress.aspx.cs页面的</p> <p&...
    by jxh12345j(注册) on 2009/4/7 8:55:12
  • ufnnutdh - Google Search
    ufnnutdh - Google Search
    by (匿名) on 2008/10/27 17:44:45
  • veysaync - Google Search
    veysaync - Google Search
    by (匿名) on 2008/10/5 5:20:49
  • mzgmhgio - Google Search
    mzgmhgio - Google Search
    by (匿名) on 2008/9/22 23:34:49
  • rhmhnyma - Google Search
    rhmhnyma - Google Search
    by (匿名) on 2008/9/22 7:48:44
  • re: 发布一个爱心小软件——网页抓图
    Maxthon应该有这个功能
    by passos(匿名) on 2008/7/21 20:05:23

广告

 

在Asp.Net中使用定时器,破宝之前已有Blog写过《在 ASP.NET 中使用计时器(Timer)》,这里主要针对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;
        }


        
Timer Callbacks

其实稍加改进就可以应用到我们自己的项目中,例如前不久刚做一个项目,因为数据量过于庞大,每次从数据库取非常慢,然后改成使用定时器,每隔12小时将最新的数据列表生成静态的文本。

BTW: 有技术八股文之嫌哦:P

打印 | 张贴于 2004-12-20 14:04:00 | Tag:CnForums

留言反馈

#回复: asp.net forums中定时器的应用 编辑
不错,和cs的挺像的
2007-05-15 17:51:00 | [匿名用户:dotnet源码学习]
#re: asp.net forums中定时器的应用 编辑
AHUJKGALJHGALHL
2006-11-11 21:22:00 | [匿名用户:钟仔]
#re: asp.net forums中定时器的应用 编辑
请教各位高手:
我的timer1_Elapsed过程:
System.DateTime currentTime=new System.DateTime();
currentTime=System.DateTime.Now;

string strMD = currentTime.ToString("s");
TextBox1.Text = strMD;

怎么TextBox1怎么没有变化的?
2006-04-14 15:37:00 | [匿名用户:kobe]
#re: asp.net forums中定时器的应用 编辑
不好用得!好烦!!!
2006-02-21 02:57:00 | [匿名用户:our ]
#re: asp.net forums中定时器的应用 编辑
怎么每个网站的东西都是一样的啊~~~~~能不能不要这样啊,太没有意思了~~~~现在想要一个东西,只要看一个网站就行了~~~反正行不行,其它的网站也是一样的内容,~~~
2005-12-18 23:12:00 | [匿名用户:5634]
#Asp.Net Forums之邮件发送 编辑
Forums中很多地方要用到邮件发送,如:邮件注册、找回密码、邮件订阅等。
添加新贴的代码流程非常慢。每次添加帖子,应用程序首先要确保没有重复贴,然后格式化帖子内容和表情图像,记号并索引,如果必要还要将帖子添加到相应的队列中,对附件进行有效性检查,最终完成发贴后,给预订者发出...
2005-07-01 10:07:00 | [匿名用户:lookfy]
#re: asp.net forums中定时器的应用 编辑
555定时器的应用
2005-06-25 15:40:00 | [匿名用户:wyy]
#HttpContext对象和RewritePath方法 编辑
Ping Back来自:www.donews.net
2005-03-09 23:51:00 | [匿名用户:阿力]
#re: asp.net forums中定时器的应用 编辑
向前辈们学习
2005-01-21 09:10:00 | [匿名用户:btstudy]
#re: asp.net forums中定时器的应用 编辑
确实是个问题!
2004-12-24 12:10:00 | [匿名用户:宝玉]
#re: asp.net forums中定时器的应用 编辑
emailTimer = new Timer(new TimerCallback(ScheduledWorkCallbackEmailInterval), application.Context, EmailInterval, EmailInterval);

这里调用发送Email 时检测是否可以发送Email用的是WebConfig中的配置,而不是后台管理中配置中设置的是否启用Email。Mail服务器配置不正确的话,如果在WebConfig中没有禁用Email,它就会不断地尝试发送Email表中的记录,我查看了一下异常信息,Email发送失败竟然有3百多万次,不知道这是不是造成网站挂掉的原因。

不知道为什么这里使用在WebConfig和数据库两个Mail配置方案。你研究一下。。呵呵。。

我把WebConfig中Email禁了,不知还会不会挂。。
2004-12-24 10:35:00 | [匿名用户:ganghao]
#re: asp.net forums中定时器的应用 编辑
代码哪里有下载呢?
2004-12-22 16:03:00 | [匿名用户:aLink]
#re: asp.net forums中定时器的应用 编辑
我在想:为什么不使用类似DNN的Scheduling技术?
2004-12-20 23:34:00 | [匿名用户:Unruled Boy(灵感之源)]
#re: asp.net forums中定时器的应用 编辑
类似的东西,迷失写过一个也很不错,一直没有时间对asp.net forums进行仔细的研究,有空对比一下看
:)
2004-12-20 17:34:00 | [匿名用户:lion]
对不起,目前本随笔不允许发表新评论.

Powered by: Joycode.MVC引擎 0.5.1.0