惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
T
Threatpost
Latest news
Latest news
N
News | PayPal Newsroom
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Help Net Security
Help Net Security
D
Darknet – Hacking Tools, Hacker News & Cyber Security
AI
AI
Simon Willison's Weblog
Simon Willison's Weblog
TaoSecurity Blog
TaoSecurity Blog
The Last Watchdog
The Last Watchdog
L
LINUX DO - 热门话题
Google DeepMind News
Google DeepMind News
T
Threat Research - Cisco Blogs
O
OpenAI News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
The Exploit Database - CXSecurity.com
NISL@THU
NISL@THU
Application and Cybersecurity Blog
Application and Cybersecurity Blog
S
Securelist
小众软件
小众软件
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Martin Fowler
Martin Fowler
S
SegmentFault 最新的问题
Cisco Talos Blog
Cisco Talos Blog
云风的 BLOG
云风的 BLOG
AWS News Blog
AWS News Blog
GbyAI
GbyAI
N
News and Events Feed by Topic
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
美团技术团队
Engineering at Meta
Engineering at Meta
A
About on SuperTechFans
博客园 - 三生石上(FineUI控件)
S
Schneier on Security
博客园 - 聂微东
V2EX - 技术
V2EX - 技术
T
Troy Hunt's Blog
SecWiki News
SecWiki News
S
Secure Thoughts
B
Blog RSS Feed
Hugging Face - Blog
Hugging Face - Blog
WordPress大学
WordPress大学
腾讯CDC
H
Heimdal Security Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Apple Machine Learning Research
Apple Machine Learning Research
月光博客
月光博客
www.infosecurity-magazine.com
www.infosecurity-magazine.com
P
Privacy International News Feed

博客园 - 中国土匪

MSSql Datetime转换成char 的样式汇总 OpenSocial容器开发,资料备注 列出数据库中有数据的表的全部数据 比较两个数据库之间差异的存储过程 关于Web Application中的类无法在页面中引用的问题 图形字符名称表(备查) 决心--重新拾起技术! BitComet的IE插件BitCommet Helper可能给系统造成错误的巨大Bug!!!求解决方案 Page事件发生的先后顺序 操作IIS过程中碰到的问题总结 当了3个礼拜老师感想 给CheckboxList分页 关于ItemCommand事件 (转自混沌居) 在.net1.1中发送邮件的几种办法 常用正则表达式总结 datalist分页方式汇总(更新了一种方法) membership.findusersbyname模糊匹配的写法 用BitComet的奇怪问题 装了红帽子9了
在 ASP.NET 中使用计时器(Timer) (转载)
中国土匪 · 2006-08-29 · via 博客园 - 中国土匪

我在实验中发现在 ASP.NET 中可以使用计时器(Timer)完成一些定时动作。这一点可能会对我们的一些 Web 程序有益。

下面首先介绍我测试使用的一个例子:

  1. 首先在 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>


  2. 然后我们简单写一个 test.aspx 来查看 Application("TimeStamp") 的值。代码如下:

    [VB.NET] test.aspx
    <%
        Response.Write(Application("TimeStamp"))
    %>

分析

根据 global.asax 中的代码,我们设定了一个计时器,每隔 10 秒钟执行一次 Fresher() 过程;在 Fresher() 过程中我们事实上只是重新写入了一个 Application("TimeStamp") 新值。换句话说,Application("TimeStamp") 的值是应该每隔 10 秒钟更新一次的。

是不是这样的呢?通过 test.aspx 的反复刷新观察 Application("TimeStamp") 的值,的确发现这个值在每隔 10 秒地变化一次,而其他时候则保持不变。与我们的预期是一致的。

意义

通过引入计时器我们可以在 ASP.NET 的全局性程序(Application)中灵活的使用计时器完成一些定时操作,比如:在社区/论坛系统中,每隔 5 分钟更新一次在线用户列表,每隔 1 个小时更新一次用户经验值,或者每隔一天备份一次关键数据等等。这个思路应该是很诱人的。

探讨

Q: 是否在 ASP.NET 代码的任何地方都可以使用计时器呢?
A: 我没有测试过在普通 *.aspx 中插入计时器的情形。但从 B/S 程序的特点来看,即使在 *.aspx 中插入计时器可行,也不是一种好的选择。因为对于 B/S 程序来说,服务器接到客户端的请求本身就是一个事件,在这个事件处理过程中,服务器必须迅速的作出回应,为客户端产生相应的 HTML 代码,然后结束这一过程。如果在 *.aspx 使用计时器(如果允许的话),则第一没有太大必要,第二很容易使系统因为插入的计时器过多(因为每一次 *.aspx 的执行都有可能插入一个新的计时器)而使系统瘫痪。

因此,我建议只在 global.asax 的 Application_OnStart 中使用比较安全一些。欢迎对此感兴趣的朋友对此发表见解。
原文链接:http://blog.joycode.com/percyboy/articles/3595.aspx