Using a Server Timer Component in ASP.NET

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I need a method to be called every 10 Seconds in an ASP.NET web application, and I was wondering the best way of doing it. I created a static Method on a Page and passed it to a Server Timer that is also created as a static instance variable of the same page. The Timer is activated on Application Start. It is working fine until now, except that in the called method, I can't get a reference to the Cache object, since it is only available during a request processing via the Context. So to pass application level data to the method, I created another static variable on the same page

All this looks messy to me, although it is working. I would like to know the right way of doing it.
 
Just put your timer in an object in the Application Cache, and store it
without any expiration. The object doesn't have to render any interface; in
a sense, you are installing a "Service" in the Application Cache. The object
has an event handler for the timer's "Elapsed" event, and the event handler
does the work.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

Abu Haider said:
Thanks, but this approach uses a client side timer. I need a server side
timer which should work even if any page is loaded in a client browser.
A method must be called every 10 seconds as long the application is in
memory. And a reference to the application Cache is also required to
manipulate application level data.
 
Thats a better idea. Thanks. But how should I get a reference to the Cache in that object? Should it be added to it as a private variable

Because when the Timer event fires, HttpContext is null, as it is not processing a request. I can't seem to get a reference to the Cache on the fly

Is the only way would be adding a reference of the Cache to the object?
 
Thats a better idea. Thanks. But how should I get a reference to the Cache
in that object? Should it be added to it as a private variable?
Because when the Timer event fires, HttpContext is null, as it is not
processing a request. I can't seem to get a reference to the Cache on the
fly.
Is the only way would be adding a reference of the Cache to the object?

Not that I know of. What does it need to reference the Cache for? Is this
something which could be stored in a database perhaps?

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.


Abu Haider said:
Thats a better idea. Thanks. But how should I get a reference to the Cache
in that object? Should it be added to it as a private variable?
Because when the Timer event fires, HttpContext is null, as it is not
processing a request. I can't seem to get a reference to the Cache on the
fly.
 
Back
Top