在 ASP.NET 中使用计时器(Timer)
2006-04-02 10:36
435 查看
我在实验中发现在 ASP.NET 中可以使用计时器(Timer)完成一些定时动作。这一点可能会对我们的一些 Web 程序有益。
下面首先介绍我测试使用的一个例子:
首先在 global.asax 中的 Application_OnStart 事件过程中定义计时器,代码如下:
[VB.NET] global.asax
<%@ import Namespace="System.Timers" %>
<script runat="server">
Sub Application_OnStart(sender As Object, e As EventArgs)
' 创建一个计时器,单位:毫秒
Dim aTimer As New System.Timers.Timer(10000)
' 将 Fresher 指定为计时器的 Elapsed 事件处理程序
AddHandler aTimer.Elapsed, AddressOf Fresher
' AutoReset 属性为 true 时,每隔指定时间循环一次;
' 如果为 false,则只执行一次。
aTimer.AutoReset = True
aTimer.Enabled = True
' 先给 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>
然后我们简单写一个 test.aspx 来查看 Application("TimeStamp") 的值。代码如下:
[VB.NET] test.aspx
<%
Response.Write(Application("TimeStamp"))
%>
< meta http-equiv="Refresh" content="3;
url=http://www.cenpok.net">
下面首先介绍我测试使用的一个例子:
首先在 global.asax 中的 Application_OnStart 事件过程中定义计时器,代码如下:
[VB.NET] global.asax
<%@ import Namespace="System.Timers" %>
<script runat="server">
Sub Application_OnStart(sender As Object, e As EventArgs)
' 创建一个计时器,单位:毫秒
Dim aTimer As New System.Timers.Timer(10000)
' 将 Fresher 指定为计时器的 Elapsed 事件处理程序
AddHandler aTimer.Elapsed, AddressOf Fresher
' AutoReset 属性为 true 时,每隔指定时间循环一次;
' 如果为 false,则只执行一次。
aTimer.AutoReset = True
aTimer.Enabled = True
' 先给 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>
然后我们简单写一个 test.aspx 来查看 Application("TimeStamp") 的值。代码如下:
[VB.NET] test.aspx
<%
Response.Write(Application("TimeStamp"))
%>
< meta http-equiv="Refresh" content="3;
url=http://www.cenpok.net">
相关文章推荐
- 在ASP.NET中使用计时器(Timer)
- 在 ASP.NET 中使用计时器(Timer)(2003/10/23 20:06)
- 在 ASP.NET 中使用计时器(Timer)
- 在 ASP.NET 中使用计时器(Timer)的整理(2003/10/23 20:36)
- 在 ASP.NET 中使用计时器(Timer)(2003/10/23 20:06)
- ASP.NET中使用计时器(Timer)(转)
- 在 ASP.NET 中使用计时器(Timer)
- 在 ASP.NET 中使用计时器(Timer)转
- ASP.NET中使用计时器(Timer)来实现群发邮件等功能
- 在 ASP.NET 中使用计时器(Timer)
- 在 ASP.NET 中使用计时器(Timer)的整理(2003/10/23 20:36)
- 在 ASP.NET 中使用计时器(Timer)的整理
- ASP.NET中使用计时器(Timer)来实现群发邮件等功能(转载)
- 在 ASP.NET 中使用计时器(Timer)
- 在 ASP.NET 中使用计时器(Timer)
- 自己开发能在asp.net项目正常使用的定时器WebTimer,让定时器听话起来
- ASP.NET中的三种Timer(计时器)的区别和用法
- asp.net中Timer定时器在web中无刷新的使用
- ASP.NET - 演练:对多个 UpdatePanel 控件使用 ASP.NET Timer 控件
- System.Timers.Timer在asp.net中的使用方法