Use Timer (Timer) in ASP. NET2.0)

Source: Internet
Author: User

In my experiment, ASP. NET allows you to use a Timer to perform some scheduled operations. This may benefit some of our Web programs.

Note: In the original article, only VB. NET code is available. For your convenience, I wrote C # code:

The following is an example of my test:

  1. First, define the timer during the Application_OnStart event in global. asax. The Code is as follows:

VB. NET



<% @ Import Namespace = "System. Timers" %>
<Script runat = "server">

Sub Application_OnStart (sender As Object, e As EventArgs)
''Creates a timer in milliseconds.
Dim aTimer As New System. Timers. Timer (10000)

''Specifies Fresher as the timer's Elapsed event handler
AddHandler aTimer. Elapsed, AddressOf Fresher

When the ''autoreset attribute is true, it is cycled every specified time;
''If it is false, it is executed only once.
ATimer. AutoReset = True
ATimer. Enabled = True

''Specify an initial value for Application (" TimeStamp ")
Application. Lock ()
Application ("TimeStamp") = DateTime. Now. ToString ()
Application. UnLock ()
End Sub

Sub Fresher (sender As Object, e As ElapsedEventArgs)
Application. Lock ()
Application ("TimeStamp") = DateTime. Now. ToString ()
Application. UnLock ()
End Sub

</Script>

 

C #

 

 



<% @ Application Language = "C #" %>
<% @ Import Namespace = "System. Timers" %>

<Script runat = "server">

Void Application_Start (object sender, EventArgs e)
{
// Code that runs when the application starts

Timer atimer = new Timer (10000 );

Atimer. Elapsed + = timer_execute;

Atimer. AutoReset = true;
Atimer. Enabled = true;

Application. Lock ();
Application ["TimeStamp"] = DateTime. Now. ToString ();
Application. UnLock ();
}

Void timer_execute (object sender, EventArgs e) // scheduled Function
{
Application. Lock ();
Application ["TimeStamp"] = DateTime. Now. ToString ();
Application. UnLock ();
}

Void Application_End (object sender, EventArgs e)
{
// Code that runs when the application is closed

}

Void Application_Error (object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs

}

Void Session_Start (object sender, EventArgs e)
{
// The code that runs when the new session starts

}

Void Session_End (object sender, EventArgs e)
{
// The code that runs when the session ends.
// Note: Only the sessionstate mode in the Web. config file is set
// The Session_End event is triggered only when InProc is used. If the session mode is set to StateServer
// Or SQLServer, the event is not triggered.

}

</Script>

 


  1. Then we simply write a timer. aspx to view the value of Application ("TimeStamp. The Code is as follows:

VB. NET



<%
Response. Write (Application ("TimeStamp "))
%>

C #



<%
Response. Write (Application ("TimeStamp "))
%>

Analysis:

According to 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 the timer, we can go to ASP.. NET global program (Application), such as updating the online user list every 5 minutes in the Community/forum system, the user experience is updated every other 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.

(For more information, see the source)

Http://blog.joycode.com/percyboy/articles/3595.aspx

 

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.