Using a Server Timer Component in ASP.NET

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.
 
K

Kevin Spencer

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.
 
G

Guest

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?
 
K

Kevin Spencer

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top