Using Timers (timer) in asp.net2.0

Source: Internet
Author: User
Tags datetime

I found in the experiment that a timer could be used to perform some timed actions in asp.net. This may be useful for some of our WEB programs.

Note: In the original text only vb.net code, in order to facilitate everyone, I wrote the C # version of the code:

Here's an example of my test usage:

    1. The timer is defined first in the Application_OnStart event procedure in Global.asax, and the code is as follows:

vb.net



< %@ Import Namespace = " System.Timers "  % >
< script Runat = " Server " >

Sub Application_OnStart (Sender as   Object , E as EventArgs)
'' Create a timer, in units: milliseconds
        Dim Atimer as   New System.Timers.Timer ( 10000 )

'' To designate fresher as the Elapsed event handler for a timer
        AddHandler atimer.elapsed, AddressOf Fresher

'' when the Autoreset property is true, the cycle occurs once at a specified time;
        '' If False, executes only once.
Atimer.autoreset =   True
Atimer.enabled =   True

'' first 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) // functions that are timed to execute
{
Application.Lock ();
application[ " TimeStamp " ]  = DateTime.Now.ToString ();
Application.UnLock ();
}

void Application_End ( Object sender, EventArgs e)
{
// code that runs when the application shuts down

}

void Application_Error ( Object sender, EventArgs e)
{
// code that runs when an unhandled error occurs

}

void Session_Start ( Object sender, EventArgs e)
{
// code that runs when a new session starts

}

void Session_End ( Object sender, EventArgs e)
{
// code that runs at the end of the session.
// Note: Only the sessionstate mode in the Web.config file is set to
// The Session_End event is raised when InProc. If the session mode is set to StateServer
// or SQL Server, the event is not raised.

}

</ Script >


    1. Then we simply write a timer.aspx to see the value of application ("TimeStamp"). The code is as follows:

vb.net



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

C#



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

Analysis :

According to the code in Global.asax, we set a timer to perform a fresher () process every 10 seconds, and in the fresher () we actually just rewritten a application ("TimeStamp") new value. In other words, the value of application ("TimeStamp") should be updated every 10 seconds.

Is that what it is? The value of application ("TimeStamp") is observed by Test.aspx repeated refreshes, and it does find that the value changes every 10 seconds and remains unchanged at other times. is in line with our expectations.

meaning :

By introducing a timer, we can use the timer in the ASP.net Global Program (application) to perform some timed operations, such as: in the Community/forum system, update the online user list every 5 minutes, update user experience every 1 hours, Or backup critical data every other day, and so on. This idea should be very tempting.

discussion :

Q: is it possible to use timers anywhere in the asp.net code?
A: I have not tested the case of inserting a timer in a normal *.aspx. However, from the features of B/s program, it is not a good choice even if it is feasible to insert a timer in *.aspx. Because for the B/s program, the server to the client's request itself is an event, in this event processing, the server must respond quickly, generate the corresponding HTML code for the client, and then end the process. If the timer (if allowed) is used in *.aspx, then the first is not much needed, and the second is to disable the system because the timer is too high (because every *.aspx is likely to insert a new timer).

Therefore, I recommend that it be safer to use only in Global.asax Application_OnStart. Friends who are interested in this issue are welcome to express their views on this.

(reprint please indicate the source)

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

</

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.