怎样设计一个计时器,每隔一段时间执行一段代码?
下面代码怎样指定时间?除了这个方法还有其他每隔一段时间执行一段代码的方法吗?
[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>
下面代码怎样指定时间?除了这个方法还有其他每隔一段时间执行一段代码的方法吗?
[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>
还有一种思路:用windows service
http://www.cnblogs.com/greatandforever/archive/2008/07/08/1238270.html这个给你参考,我也开始学,就是记住Form.timer定时器要改成System.Timer.Timer,不然不能触发
http://www.cnblogs.com/greatandforever/archive/2008/07/08/1238270.html