Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
920 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...