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 |