Timer.Tick事件中的局部静态变量(停止计时器) [英] Local static variables in Timer.Tick event (Stopping a Timer)

查看:170
本文介绍了Timer.Tick事件中的局部静态变量(停止计时器)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ASP.NET的页面上有一个计时器.

I have a timer on a page in ASP.NET.

经过一段时间后,我想禁用计时器.

After a certain period of time elapses, I want to disable the timer.

我想在计时器滴答事件中放置一个静态变量,以跟踪经过了多少秒.

I want to put a static variable in the timers tick event that will track how many seconds have elapsed.

我的问题是,这行得通吗?

My question is, will this work?

如果用户X和Y正在查看页面,他们是否都将具有单独的局部静态变量?

If user X and Y are viewing the page will they both have separate local static variables?

在经过一定时间后关闭ASP.NET计时器的最佳方法是什么?

What is the best method of shutting down an ASP.NET timer after a certain elapsed time?

推荐答案

不,它不起作用. VB.Net中的Static是语法糖,它以线程安全的方式在类级别隐藏隐藏的共享变量.因为可以一次为该页面的所有用户共享一个共享的类变量,所以这根本无法实现您的期望.

No, it won't work. Static in VB.Net is syntactic sugar that hides a hidden shared variable at the class level in a thread-safe way. Since a shared class variable would be, well, shared for all users of that page at once, this would not work at all how you hoped.

相反,将您的datetime字段存储在会话中并进行检查.还要记住,计时器本身正在客户端上运行(假设您正在使用ajax计时器控件).因此,禁用它的唯一方法是在服务器事件期间,例如在您的Tick事件触发时.

Instead, store your datetime field in the session and check that. Also remember that the timer itself is running on the client (assuming you're using an ajax timer control). So the only way to disable it is during a server event, such as when your Tick event fires.

这篇关于Timer.Tick事件中的局部静态变量(停止计时器)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆