asp.net
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.
Here's an example of my test usage:
The timer is defined first in the Application_OnStart event procedure in Global.asax, and the code is as follows:
[VB.net] Global.asax
<%@ import namespace= "System.Timers"%>
<script runat= "Server" >
Sub Application_OnStart (sender as Object, E as EventArgs)
' Create a timer, unit: milliseconds
Dim Atimer as New System.Timers.Timer (10000)
' Specify the fresher as the Elapsed event handler for the timer
AddHandler atimer.elapsed, AddressOf fresher
' Autoreset property is true, cycles at specified intervals;
' 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>
Then we simply write a test.aspx to see the value of application ("TimeStamp"). The code is as follows:
[VB.net] Test.aspx
<%
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.
Significance:
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.
Explore
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.