It works for some time (like 10-15 days) then it stops. I mean the service shows as running, but it does not do anything. I make some logging and the problem can be the timer, because after the interval it does not call the tickTack_Elapsed function.

I was thinking about rewrite it without a timer, using an endless loop, which stops the processing for the amount of time I set up. This is also not an elegant solution and I think it can have some side effects regarding memory.

The Timer is used from the System.Timers namespace, the environment is Windows 2003. I used this approach in two different services on different servers, but both is producing this behavior (this is why I thought that it is somehow connected to my code or the framework itself).

Does somebody experienced this behavior? What can be wrong?

Edit:

I edited both services. One got a nice try-catch everywhere and more logging. The second got a timer-recreation on a regular basis. None of them stopped since them, so if this situation remains for another week, I will close this question. Thank you for everyone so far.

Edit:

I close this question because nothing happened. I mean I made some changes, but those changes are not really relevant in this matter and both services are running without any problem since then. Please mark it as "Closed for not relevant anymore".

Is there anything in the event log after the timer stops running?
–
brienDec 29 '08 at 14:00

Nothing at all. I can catch the tickTack_Elapsed function, but it is not called anymore. So the log shows that from starting the service I got events every 10 minutes but after a while it just stops without any error message or anything else. Can I log somehow more details?
–
BiriDec 29 '08 at 14:05

don't close the question, because others may find the information useful; accept an answer instead
–
Steven A. LoweMay 12 '10 at 18:45

7 Answers
7

Like many respondents have pointed out exceptions are swallowed by timer. In my windows services I use System.Threading.Timer. It has Change(...) method which allows you to start/stop that timer. Possible place for exception could be reentrancy problem - in case when tickTack_Elapsed executes longer than timer period. Usually I write timer loop like this:

from my experience this problem is pretty much always due to an unhandled exception of the timer elasped delegate thread, ie "what Steven said!"
–
RhysCFeb 9 '10 at 7:25

That seems like terrible default behavior, but I've seen this before too.
–
SqlRyanMay 12 '10 at 15:30

I cannot believe this is still the case. I have been hitting my head against a wall trying to figure out why my program was stopping suddenly with no message though I had a tremendous amount of log writing.
–
spinonMay 21 '11 at 21:59

1

@spinon: one of my pet .net peeves - this has been a design flaw since .net 1.0, yet it remains unfixed even after 12 years!
–
Steven A. LoweMay 21 '11 at 23:06

I have seen this before with both timer, and looped services. Usually the case is that an exception is caught that stops the timer or looping thread, but does not restart it as part of the exception recovery.

To your other points...
I dont think that there is anything "elegant" about the timer. For me its more straight forward to see a looping operation in code than timer methods. But Elegance is subjective.

Memory issue? Not if you write it properly. Maybe a processor burden if your Thread.Sleep() isn't set right.

My last idea was that I will connect to the service with VS to debug it. Hopefully I could get the exception whereever it happens. I will also rewrite one of them to a loop. It worth a try.
–
BiriDec 29 '08 at 14:12

You may want to have a loop that periodically checks the timer to see if its running, or to check the other loop. I use this to automatically restart a stopped timer or loop thread.
–
StingyJackDec 29 '08 at 14:18

Sorry, I forgot to mention that this is a .NET 2.0 application. But I also don't stop and restart the timer, however there are some points to consider (like using System.Threading instead of System.Timers). I add to my list to try, thanks.
–
BiriDec 30 '08 at 9:09

I had the same effect as you -- I never stopped/restarted the timer, either. Nonetheless, through detailed logging I was able to ascertain that the timer simply stopped firing events.
–
jroDec 30 '08 at 16:14

Unfortunately no. The tickTack_Elapsed is working right, I log when it starts and ends. Every time I see a start-stop event pair before it stops responding. Furhermore I also check the running state in there, so it cannot run over to a previously started instance.
–
BiriDec 29 '08 at 14:10