.NET Timers aborting in web application

J

Jeff Greenland

Hello everyone,

I am having problems with Timers in a web application. They just seem to
stop running after 15 minutes or so.

My web application is set up like this:

When a user hits a page in the site, that page (.aspx) instantiates a
compiled class (.DLL). The instantiation process creates a Timer that runs
in the background to perform tasks every so often (such as notifying clients
every 24 hours if something is overdue, etc.).

The problem seems to be that if the site goes idle, the timer just dies
after awhile. I've searched high and low to find a solution for this, but
can't seem to find any answers. I have checked the processModel of my
machine.config and all of the timeout settings are "Infinite".

So, just for fun, I built one of my timers to run every minute and log some
text to a file. That way I could tell when it is aborting. I start up the
application and it prints a line every minute in the log, so I decide to go
to bed. I look at it in the morning to see that it stopped after 18 lines
in the log. I probably used the site for about 3 minutes before going to
bed, so I'm assuming this is a 15 minute timeout.

Nothing in the timer code tells them to abort or reschedule or anything.
The only line of code for this timer is to print the line to the log.

The only solution I have found that works is to use the scheduler (this is a
Windows 2000 server, by the way) in the control panel. I have created a
scheduled task to run every 10 minutes that will open Internet Explorer and
hit my web site, therefore keeping it active (or starting it back up if it
went idle).

It seems like there should be a way to keep the timers persistent for an
infinite amount of time. Could it be the garbage collector destroying an
object reference? Is it just the web application shutting down?

Oh, and as another note, the web application doesn't *seem* to be shutting
down entirely if it is shutting down. I have my system set up so that when
the first instantiation occurs, a few logs are printed out. That way I can
see what's happening when the application starts. Every time I change
web.config or recompile my DLLs, the application restarts (as it should) and
I see the new logs. However, when I leave my application running overnight,
then come in in the morning, the application has not shut down because no
"startup messages" are appearing in my logs. Yet the timers are dying
still.

Sorry about the long-winded question, but I thought I needed to provide a
good amount of information.

I greatly appreciate any help someone can provide. Thank you in advance for
looking at this.

Hegg

(wasn't sure which NG to post in, so I posted it in 2 of them)
 
C

Chris Jackson

With ASP.NET, the application dies once the last session has died. You can
modify the amount of time that a session will stay alive, but there is no
way to make it infinite. The class System.Web.SessionState.HttpSessionState
has an Int32 field called _timeout (accessed through the Timeout property)
which specifies the number of minutes that a session will stay alive, so you
could set this value to System.Int32.MaxValue in order to keep it alive as
long as possible, but you can never keep it alive infinitely. Instead of
using ASP.NET to run this scheduled process, you should use a Windows
service that runs this as often as necessary.
 
A

Alvin Bruney

Chris,
I believe his problem is related to the timer being on the page. If the
timer is moved to global.asax, it will fix the situation since the timer
becomes global to the application and not susceptible to session ends. The
advantage of this is the timer dies when the app gets recycled which is a
nicety. In the OP's post, the problem occurs when the page goes away, or
gets torn down or session ends, it takes the timer with it.

Actually I believe it is a bit more complicated than this. I believe the
timer, running as a background thread, eventually dies when the session end
event causes this thread to stop executing. Which is why it goes out after
roughly the session time out. Your solution, while it would work, would
still not be bullet proof because maxvalue is still a finite number.

I've put about 20 seconds of thought into this by the way so it may not be
spot on.
 
J

Jeff Greenland

Thanks Chris, I was actually able to find a solution by taking advice from
Alvin. Basically the simple solution is to create your timers via the
application_Start() event in your application's "global.asax" file.

Thanks for the reply, I appreciate it.

Jeff
 

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

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top