Application of timer in asp.net forums

Source: Internet
Author: User
Tags httpcontext log mail
Asp.net| Timing

In the asp.net use of timers, broken treasure before the blog has written "in asp.net use timer (timer)", here mainly for ASP.net forums to describe its specific implementation.

In asp.net forums, the timer is applied as follows:
1. Update Forum Statistics
2. A post that specifies the number of bars in a timed index
3. The mail in the timed mass queue

The call to the timer in forums is placed in the Init method of the custom HttpModule (if you are not using HttpModule, you can also call the timer in Application_OnStart in globals.aspx).

Timer
Static Timer Statstimer;
Static Timer Emailtimer;

Timed interval
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
//
Omit other code

Forumconfiguration forumconfig = Forumconfiguration.getconfig ();

If the timer is used and the timer is not initialized
if (Forumconfig!= null
&& forumconfig.isbackgroundthreadingdisabled = = False) {
if (Emailtimer = null)
New timer
Create a new TimerCallback delegate, the method to execute in 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>
Release timer
</summary>
public void Dispose () {
Statstimer = null;
Emailtimer = null;
}

Timer Callbacks
#region Timer Callbacks
/**////<summary>
Messages to be sent in a scheduled send queue
</summary>
private void Scheduledworkcallbackemailinterval (object sender) {
try {
Pause timer when processing mail
Emailtimer.change (System.Threading.Timeout.Infinite, emailinterval);

Send a message in a queue
//
Emails.sendqueuedemails ((HttpContext) sender);


Update anonymous users
//
Users.updateanonymoususers ((HttpContext) sender);
}
catch (Exception e) {
Forumexception fe = new Forumexception (Forumexceptiontype.emailunabletosend, "scheduled Worker Thread failed.", e);
Fe. Log ();
}
finally {
Restart Timer
Emailtimer.change (Emailinterval, emailinterval);
}
}

/**////<summary>
Timed index posts and regularly update forum statistics
</summary>
private void Scheduledworkcallbackstatsinterval (object sender) {
try {
Sleep Timer
Statstimer.change (System.Threading.Timeout.Infinite, statsinterval);

100 posts per index
//
Search.indexposts ((HttpContext) sender, 100);

Update Forum Statistics
Sitestatistics.loadsitestatistics ((HttpContext) sender, True, 1);
}
catch (Exception e) {
Forumexception fe = new Forumexception (Forumexceptiontype.unknownerror, failure performing scheduled statistics Maintenance. ", e);
Fe. Log ();
}
finally {
wake-up Timer
Statstimer.change (Statsinterval, statsinterval);
}
}
#endregion
In fact, a little improvement can be applied to our own projects, such as just a project recently, because the amount of data is too large, every time from the database is very slow, and then changed to use the timer, every 12 hours to the latest data list generated static text.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.