Hello. I have a timer in my app that fires every 15 seconds calling a function. It appears that after about 8 hours of running the timer stops calling the function even though the program continues to run.
I define the timer in my main class like this:
private System.Timers.Timer timer = new System.Timers.Timer();
Then in one of my init functions I set it like this:
timer.Interval = 15000;
timer.Enabled = true;
timer.Elapsed += new ElapsedEventHandler(UpdateStats);
timer.Start();
The function UpdateStats is called for about the first 8 hours. Then it isn't called anymore. To confirm that it isn't called anymore I have a a file being written to in the function that appends the current to the file. After 8 hours the file isnt written to anymore.
Any help would be appreciated! Thanks.

Timer doesn't seem to last forever
Toni Greco
FcoLomas
exceptions
Possible thread locks
From my experience timer's do not fall asleep however Threads occassionaly do fall off the face of the earth after a couple of days.
Also how big is your File I've noticed issues that actually went away when I stopped logging or rotated the logs out because the file was getting so big and for some reason .NET was puking on it. This would happen after a few weeks however for me.
Bhupathi Venkatesh
I added a try/catch inside the function that the timer was calling and an out of memory exception was being thrown.
I was accidently allocating memory I didn't use.
Sorry to give MS a bad name due to my own ignorance!
The timer is working beautifully hours on end!
Thanks for the suggestions guys.
eTape
Yeah XML made an enemy of me recently when releasing StartMenuEx (which uses Xml to read/write themes, not a good idea)...
For some reason people couldn't read the files on their pc's. Must have been schema problems or something.
This post was pretty useless. Maybe I should hit Back
Mario_G
A timer might not fire because there are not available threads in the thread pool to call that method. You can find out how many worker threads are available using the ThreadPool class. You can also implement custom performance counters using this info. Then make a counter log in perfmon.msc.
http://msdn.microsoft.com/library/default.asp url=/library/en-us/dnpag/html/scalenethowto02.asp
puromtec
Is there any chance of something unexpected happening inside of UpdateStats
Is this timer being run from within a Windows Service I ask because some people have problems with System.Timers.Timer there and end up having to use System.Threading.Timer instead.
palmirag
Thanks for the replies. The code is not running from within a windows service.
It is possible something unexpected is happening, although there is very little code inside UpdateStats. The code loads a remote xml file:
xmlStats.Load(sUrl);
The only thing I can really see going wrong is if the url doesn't resolve. But, I pulled the network connection as the program was running... waited a few minutes, then connected it again and the UpdateStats function continues right on as it should. So it doesn't seem like there is a problem loading the xml file.
I don't think the file that is being written to is getting too large (it only gets around 4 meg). I only put the file writing into this function to troubleshoot. The stats in the function stopped getting updated after about 8 hours thats why I added I put the file writing stuff in there.
What about disposing of the timer at the end of each UpdateStats and recreating it again Any problem there Is it worth a shot
Thanks.
Ignatius V Ignatius