Local static variables in Timer.Tick event (Stopping a Timer)

左心房为你撑大大i 提交于 2019-12-11 15:44:00

问题


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?

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

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


回答1:


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.

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.



来源:https://stackoverflow.com/questions/1105780/local-static-variables-in-timer-tick-event-stopping-a-timer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!