Asp.net Timer

Source: Internet
Author: User
In the use of winform programming, we can use the timer to implement the scheduled work of the specified cycle for operations, but how to operate in ASP. NET is a problem.

There is a good example in ASP. NET forums. The timer application is as follows:
1. Update Forum statistics
2. regularly index the specified number of posts
3. Scheduled sending of emails in the group queue

In forums, the timer is called in the init method of the custom httpmodule (if you do not use the httpmodule, you can also call the timer in application_onstart in globals. aspx ).

// Timer
Static timer statstmer;
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
//
// Skip other Code

Forumconfiguration forumconfig = forumconfiguration. getconfig ();

// If the timer is used and the timer has not been initialized
If (forumconfig! = NULL
& Forumconfig. isbackgroundthreadingdisabled = false ){
If (emailtimer = NULL)
// Create a timer
// Create a timercallback delegate. The method to be executed is in scheduledworkcallbackemailinterval.
Emailtimer = new timer (New timercallback (scheduledworkcallbackemailinterval), application. Context, emailinterval, emailinterval );

If (forumconfig. isindexingdisabled = false
& Statstimer = NULL ){
Statstmer = new timer (New timercallback (scheduledworkcallbackstatsinterval), application. Context, statsinterval, statsinterval );
}
}
}

/** // <Summary>
/// Release the timer
/// </Summary>
Public void dispose (){
Statstimer = NULL;
Emailtimer = NULL;
}

Timer callbacks # region timer callbacks
/** // <Summary>
/// Scheduled sending of the emails to be sent in the queue
/// </Summary>
Private void scheduledworkcallbackemailinterval (Object sender ){
Try {
// Pause the timer when processing the email
Emailtimer. Change (system. Threading. Timeout. Infinite, emailinterval );

// Send emails in the 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 the timer.
Emailtimer. Change (emailinterval, emailinterval );
}
}

/** // <Summary>
/// Regularly index posts and regularly update Forum statistics
/// </Summary>
Private void scheduledworkcallbackstatsinterval (Object sender ){
Try {
// Sleep Timer
Statstimer. Change (system. Threading. Timeout. Infinite, statsinterval );

// Index 100 posts each time
//
Search. indexposts (httpcontext) sender, 100 );

// Update Forum statistics
Sitestatistics. loadsitestatistics (httpcontext) sender, true, 1 );
}
Catch (exception e ){
Forumexception Fe = new forumexception (forumexceptiontype. unknownerror, "Failure authentication Ming scheduled statistics maintenance.", e );
Fe. Log ();
}
Finally {
// Wake up the timer
Statstimer. Change (statsinterval, statsinterval );
}
}
# Endregion

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.