In the experiment, you can use a timer (timer) to complete some scheduled actions in ASP. NET. This may affect some of our webProgramBeneficial.
The following is an example of testing:
- First, define the timer during the application_onstart event in global. asax,CodeAs follows:
[C #] global. asax
Then we simply write test. aspx to view the value of application ("timestamp. The Code is as follows:
Analysis:
Root global. in the asax code, we set a timer to execute the fresher () process every 10 seconds. In the fresher () process, we actually just re-write an application ("timestamp ") new value. In other words, the value of application ("timestamp") should be updated every 10 seconds.
Is that true? Refresh test. aspx repeatedly to observe the value of application ("timestamp"). It is indeed found that this value changes once every 10 seconds, and remains unchanged at other times. It is consistent with our expectation.
Meaning:
By introducing a timer, We can flexibly use the timer in the ASP. NET Global Program (Application) to perform some scheduled operations, for example:Community/In the forum system, the online user list is updated every 5 minutes, the user experience is updated every 1 hour, or key data is backed up every other day. This idea should be very attractive.
Discussion:
Q:Can a timer be used anywhere in ASP. NET code?
A:I have not tested the case of inserting a timer in normal *. aspx. However, from the characteristics of the B/S program, it is not a good choice even if the timer is inserted in *. aspx. For B/S programs, the request sent from the server to the client is an event. During the event processing, the server must respond quickly to generate HTML code for the client, and end the process. If *. aspx uses a timer (if allowed), the first is not necessary, and the second is easy to make the system too many timers (because every time *. aspx execution may insert a new timer) to paralyze the system.
Therefore, we recommend that you use this function only in application_onstart of Global. asax. You are welcome to share your comments.
2. The application of timer in Asp.net forums is extracted, and the application of timer in ASP. NET forums is analyzed 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 statstimer;
Static Timer emailtimer;
// Scheduled interval
Private Long Emailinterval = Forumconfiguration. getconfig (). threadintervalemail * 60000 ;
Private Long Statsinterval = Forumconfiguration. getconfig (). threadintervalstats * 60000 ;
Public String modulename {
Get {Return "Forumshttpmodule";}
}
// **************************************** *****************************
// Forumshttpmodule
//
///
/// initializes the httpmodule and performs the wireup of all application
/// events.
//
/// application the module is being run for
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 new 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 ) {
Statstimer= NewTimer (NewTimercallback (scheduledworkcallbackstatsinterval), application. Context, statsinterval, statsinterval );
}
}
}
/// <Summary>
///Release Timer
/// </Summary>
Public Void Dispose () {
Statstimer= Null;
Emailtimer= Null;
}
# Region Timer callbacks
/// <Summary>
///Emails to be sent in the timed sending queue
/// </Summary>
Private Void Scheduledworkcallbackemailinterval ( Object Sender) {
Try {
// Pause the timer when processing the email
Emailtimer. Change (system. Threading. Timeout. Infinite, emailinterval );
// Emails in the sending queue
//
Emails. sendqueuedemails (httpcontext) sender );
// Update Anonymous Users
//
Users. updateanonymoususers (httpcontext) sender );
}
Catch (Exception E) {
Forumexception fe= NewForumexception (forumexceptiontype. emailunabletosend,"Scheduled worker thread failed.", E );
Fe. Log ();
}
Finally {
//Restart 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 );
// 100 posts indexed each time
//
Search. indexposts (httpcontext) sender, 100 );
// Update Forum statistics
Sitestatistics. loadsitestatistics (httpcontext) sender, True , 1 );
}
Catch (Exception E) {
Forumexception fe= NewForumexception (forumexceptiontype. unknownerror,"Failure refreshing Ming scheduled statistics maintenance.", E );
Fe. Log ();
}
Finally {
//Wake-up timer
Statstimer. Change (statsinterval, statsinterval );
}
}
# Endregion