During my experiments, I discovered that you can use timers (Timer) to complete some scheduled actions in ASP.NET. This may be beneficial to some of our Web programs.
Let's first introduce an example I used for testing:
First, define a timer in the Application_OnStart event process in global.asax. 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 Fresher as the timer's Elapsed event handler
AddHandler aTimer.Elapsed, AddressOf Fresher
' When the AutoReset property is true, it loops every specified time;
' If false, only execute 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 view the value of Application("TimeStamp"). The code is as follows:
[VB.NET]test.aspx
<%
Response.Write(Application("TimeStamp"))
%>
< meta http-equiv="Refresh" content="3;
url=http://www.cenpok.net">