views:

408

answers:

1

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  A: 

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.

Joel Coehoorn