// 定时器 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 |
新闻热点
疑难解答
图片精选