ASP.NET AJAX Timer Trouble? Location is key.

If you’ve made much use of the ASP.NET AJAX Timer control, you may have noticed that it can behave somewhat unexpectedly.  In this post, I’m going to take a closer look at how the Timer works and the most significant factor that influences it:  Location.

Where the timer is placed on the page actually varies how it operates.  As a Timer’s delay interval approaches the processing time of its resulting PostBack, the difference that the Timer’s location makes becomes very significant.

Warning:  Timers and UpdatePanels are a potentially dangerous combination.  For long intervals, the simplicity can often be worth the performance trade-off.  However, as your interval becomes smaller, you should very seriously consider a lighter weight approach to polling for data updates.

That said, I know that you’re probably still going to be using them even when you shouldn’t.  So, let’s take a closer look at how the Timer actually works.

The Timer control’s underlying mechanism

It’s important to understand that the Timer control is simply an elaborate abstraction for combining JavaScript’s setTimeout and ASP.NET’s __doPostBack. 

Assuming that UpdatePanel1′s OnLoad event was handled in the same way that the Timer1′s OnTick was handled, a Timer declared like this:

<asp:Timer runat="server" id="Timer1" Interval="5000" />

Could be very roughly approximated by JavaScript such as:

function pageLoad() {window.setTimeout("__doPostBack('UpdatePanel1', '')", 5000);
}

There’s nothing magic about the Timer control.  It attempts to approximate the Timer control that we’re used to in WinForms development, but is still subject to the limitations of the stateless HTTP protocol.

Keeping this in mind, let’s look at two cases.  In both, the Timer will have an Interval of five seconds and will trigger a partial postback taking four seconds to complete.  However, due to Timer placement, the actual time between updates in these two cases will differ by around 80%.

Case 1:  A Timer inside UpdatePanel content

<asp:ScriptManager runat="server" ID="ScriptManager1" />
<asp:UpdatePanel runat="server" ID="UpdatePanel1"><ContentTemplate><asp:Timer runat="server" ID="Timer1" OnTick="Timer1_Tick" Interval="5000" /><asp:Literal runat="server" ID="Literal1" /></ContentTemplate>
</asp:UpdatePanel>
protected void Timer1_Tick(object sender, EventArgs e)
{System.Threading.Thread.Sleep(4000);Literal1.Text = DateTime.Now.ToLongTimeString();
}

This is probably the easiest way of implementing a Timer.  You can just drop it in the UpdatePanel, create an OnTick handler, and not worry about setting up triggers or anything else.

However, even though the Timer interval is very clearly specified as five seconds, this UpdatePanel will actually take just over nine seconds for each Tick to fire.  The reason for this large discrepancy is the location of the Timer.  Take a look at the partial postback response, and it becomes clear why:

Timer response when contained in an UpdatePanel

Because the Timer is inside the UpdatePanel’s ContentTemplate, it will be reinitialized every time the UpdatePanel refreshes.  It actually comes within one second of triggering another OnTick event, but is reset by the partial postback’s return.

This may or may not be desirable in a given scenario, but can be fairly unexpected.

Case 2:  A Timer outside UpdatePanel content

<asp:ScriptManager runat="server" ID="ScriptManager1" />
<asp:Timer runat="server" ID="Timer1" Interval="5000" OnTick="Timer1_Tick" />
<asp:UpdatePanel runat="server" ID="UpdatePanel1"><Triggers><asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" /></Triggers><ContentTemplate><asp:Literal runat="server" ID="Literal1" /></ContentTemplate>
</asp:UpdatePanel>
protected void Timer1_Tick(object sender, EventArgs e)
{System.Threading.Thread.Sleep(4000);Literal1.Text = DateTime.Now.ToLongTimeString();
}

Implementing the Timer this way removes the reinitialization interference, but exposes another potential problem.  Since the Timer ticks are now fixed at an absolute five second interval, only one second less than our partial postback requires to complete, the UpdatePanel spends nearly all of its time in async postback.

Depending on your application this may be exactly what you want, but it also might be very detrimental to the usability and performance of the application.  For example, if you were to locate a one second Timer outside an UpdatePanel that took two seconds to refresh, it would never manage to complete a single update.

Conclusion

As you can see, Timer placement can significantly and unintuitively affect how the Timer actually operates.  Understanding all of the influencing factors puts you in control and enables you to arrange your Timers to best suit your application.

I’m going to leave you with a bit of a pop-quiz.  I hope you were paying attention!  Consider the following code sample, from a question posted on the asp.net forums:

<asp:ScriptManager runat="server" id="ScriptManager1" />
<asp:UpdatePanel runat="server" ID="UpdatePanel1"><ContentTemplate><asp:Literal runat="server" ID="Literal1" /><asp:Timer runat="server" ID="Timer1" Interval="5000" OnTick="Timer1_Tick" /></ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel runat="server" ID="UpdatePanel2"><ContentTemplate><asp:Literal runat="server" ID="Literal2" /><asp:Timer runat="server" ID="Timer2"Interval="10000" OnTick="Timer2_Tick" /></ContentTemplate>
</asp:UpdatePanel>
protected void Timer1_Tick(object sender, EventArgs e)
{Literal1.Text = DateTime.Now.ToString();
}protected void Timer2_Tick(object sender, EventArgs e)
{Literal2.Text = DateTime.Now.ToString();
}

Roughly how often do you think I should expect to see Literal2′s timestamp update?  Leave a comment if you know the answer. 

Additionally, this code can be “fixed” to yield more intuitive results by adding only one property to one control.  What would you do?

转载于:https://www.cnblogs.com/happy-Chen/p/3688270.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/399515.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

在屏幕上打印杨辉三角

这就是杨辉三角&#xff0c;也叫贾宪三角。这于我们现在的学习联系最紧密的是2项式乘方展开式的系数规律。如图&#xff0c;在贾宪三角中&#xff0c;第3行的第三个数恰好对应着两数和的平方公式依次下去。 杨辉三角是一个由数字排列成的三角形数表&#xff0c;一般形式如下&am…

对SPA(单页面应用)的总结

目录 1、单页面应用&#xff08;SPA&#xff09;的概念&#xff1a;2、作用&#xff08;好处&#xff09;3、缺点4、实现SPA1、单页面应用&#xff08;SPA&#xff09;的概念&#xff1a; 1、single-page application是一种特殊的Web应用。它将所有的活动局限于一个Web页面中&a…

SpringSecurity6 | 失败后的跳转

✅作者简介:大家好,我是Leo,热爱Java后端开发者,一个想要与大家共同进步的男人😉😉 🍎个人主页:Leo的博客 💞当前专栏: Java从入门到精通 ✨特色专栏: MySQL学习 🥭本文内容: SpringSecurity6 | 失败后的跳转 📚个人知识库: Leo知识库,欢迎大家访问 学习…

[Android]AndroidBucket增加碎片SubLayout功能及AISubLayout的注解支持

以下内容为原创&#xff0c;转载请注明&#xff1a; 来自天天博客&#xff1a;http://www.cnblogs.com/tiantianbyconan/p/3709957.html 之前写过一篇博客&#xff0c;是使用Fragment来实现TabHost的效果&#xff0c;并且模拟TabHost的切换各个fragment生命周期的调用&#xff…

WordPress 获取当前页面 ID 的几大方法

https://zhangzifan.com/wordpress-get-id.html 在很多的 WordPress 主题或者插件功能的开发中&#xff0c;我们总是需要获取到 WordPress 给每个页面定义的 ID&#xff0c;不然也某些情况下是无法确定这是哪一个页面&#xff0c;针对于文章或者页面的 ID 获取基本可以使用 get…

王者荣耀交流协会第四次Scrum立会

拍照的是王磊同学&#xff0c;没有出镜。 开会时间&#xff1a;2017年10月30日下午18&#xff1a;45-19&#xff1a;16 共计31分钟 开会地点&#xff1a;一食堂二楼靠近窗户倒数第四排 今日完成工作进度&#xff1a; 袁玥同学完成了点击按钮刷新时间的功能代码&#xff1b; 王…

Python在信号与系统(1)——Hilbert兑换,Hilbert在国家统计局的包络检测应用,FIR_LPF滤波器设计,格鲁吉亚也迫使高FM(PM)调制...

谢谢董老师&#xff0c;董老师是个好老师。 心情久久不能平静&#xff0c;主要是高频这门课的分析方法实在是让我难以理解&#xff0c;公式也背只是&#xff0c;还是放放吧。 近期厌恶了Matlab臃肿的体积和频繁的读写对我的Mac的损害&#xff0c;所以学习了一下Python这一轻量级…

如何在WP-Config中设置WordPress错误日志

https://baijiahao.baidu.com/s?id1622279671500148245&wfrspider&forpc 你想在wp-config文件中设置WordPress错误日志吗&#xff1f;WordPress中的wp-config文件不仅控制您的WordPress网站设置&#xff0c;它也是一个非常有用的调试工具&#xff0c;可帮助您查找和修…

wordpress启用侧边栏小工具

http://www.seo628.com/1872.html wordpress后台默认不显示小工具选项&#xff0c;开发者需要启用小工具功能并把小工具在相应的前台位置调用出来&#xff0c;这样才能在后台直接拖动生成侧边栏。 激活小工具 激活小工具需要在functions.php中注册至少一个侧边栏 register_s…

详解 Spotlight on MySQL监控MySQL服务器

前一章详解了Spotlight on Unix 监控Linux服务器 &#xff0c;今天再来看看Spotlight on MySQL怎么监控MySQL服务器。 注&#xff1a;http://www.cnblogs.com/Javame/p/3685512.html 第一步: 下载并安装mysql-connector-3.5x Spotlight on MySQL 连接mysql必须使用mysql-connec…

lua------------------Unity3D研究院编辑器之打开unity不可识别的文件(十三)

Unity3D研究院编辑器之打开unity不可识别的文件&#xff08;十三&#xff09; 雨松MOMO 【Unity3D拓展编辑器】 围观8597次 9 条评论 编辑日期&#xff1a;2017-03-02 字体&#xff1a;大 中 小 有些特殊后缀名的文件在unity里是不可识别的。如下图所示&#xff0c;这里我把文本…

一起Polyfill系列:Function.prototype.bind的四个阶段

昨天边参考es5-shim边自己实现Function.prototype.bind&#xff0c;发现有不少以前忽视了的地方&#xff0c;这里就作为一个小总结吧。 一、Function.prototype.bind的作用 其实它就是用来静态绑定函数执行上下文的this属性&#xff0c;并且不随函数的调用方式而变化。 示例&am…

盘点18个免费的WordPress主题后台选项开发框架

https://yusi123.com/3205.html/3 13.Warp Framework Warp框架不仅支持WordPress和Joomla,还可以可扩展到其他的适用Web程序。使用Warp框架你可以轻松的定制你需要的功能。 该框架是来自Yootheme团队。看看他们出的主题&#xff0c;你就知道这个绝对是精品了。精心设计的界面和…

lua----------------使用VS2015搭建lua开发环境的一些侥幸成功经验,

所以本篇博文介绍在Windows平台下&#xff0c;使用VS2015搭建lua开发环境的一些侥幸成功经验&#xff0c;安装过程参考网上教程&#xff0c;安装过程如下&#xff08;参考http://www.byjth.com/lua/33.html&#xff09; 一 生成lua5.3.lib 1、下载并编译lua源码 首先进入lua官…

WordPress中使主题支持小工具以及添加插件启用函数

https://www.jb51.net/article/76810.htm 这篇文章主要介绍了WordPress中使主题支持widget以及添加插件启用函数的方法,使WP可以使用小工具widget与通过register_activation_hook()来添加启用插件的函数,需要的朋友可以参考下 让主题支持小工具 WordPress 的小工具&#xff08;…

Thunder团队第三周 - Scrum会议6

Scrum会议6 小组名称&#xff1a;Thunder 项目名称&#xff1a;i阅app Scrum Master&#xff1a;宋雨 工作照片&#xff1a; 代秋彤照相&#xff0c;所以图片中没有该同学。 参会成员&#xff1a; 王航&#xff1a;http://www.cnblogs.com/wangh013/ 李传康&#xff1a;http://…

如何使WordPress博客添加多个sidebar侧边栏

https://www.cnblogs.com/lydbk/p/4609736.html 如何使WordPress博客添加多个sidebar侧边栏 在制作wordpress模版的时候,也许你会遇到一个sidebar侧栏不能完全满足你的需求&#xff0c;或者侧栏内容过多导致页面过长&#xff0c;那么我们可以考虑使用两个或者更多侧栏。 考虑…

glove中文词向量_Summary系列glove模型解读

一、Glove模型简介语义文本向量表示可以应用在信息抽取&#xff0c;文档分类&#xff0c;问答系统&#xff0c;NER&#xff08;Named Entity Recognition&#xff09;和语义解析等领域中&#xff0c;大都需要计算单词或者文本之间的距离或者相似度&#xff0c;因此&#xff0c;…

lynx---CentOS终端访问IP

1、官网 http://lynx.isc.org 2、稳定版本 http://invisible-mirror.net/archives/lynx/tarballs/lynx2.8.8rel.2.tar.gz 3、下载 cd /usr/local/ wget http://invisible-mirror.net/archives/lynx/tarballs/lynx2.8.8rel.2.tar.gz 4、解压 tar xzf lynx2.8.8rel.2.tar.gz 5…

OnLongClickListener长按事件设置墙纸

在AndroidApp应用中&#xff0c;OnLongClick事件表示长按2秒以上触发的事件&#xff0c;本章我们通过长按图像设置为墙纸来理解其具体用法。知识点&#xff1a;OnLongClickListener  OnLongClickListener接口与之前介绍的OnClickListener接口原理基本相同&#xff0c;只是该接…