Timers in windows service failing to trigger

Hello all,

I have a windows service that uses two static timers and several dynamically created timers (System.Timers.Timer).

The issue is that the service runs fine, then suddenly, the timers no longer fire so nothing happens but the service still shows as running.

We write to a log when a timer tick begins processing and when it ends, and after checking the logs, we see that a process begins and ends but is never called again.

Also, the timers don't all fail at the same time.

On one of the static timers I disable at the start of processing then enable at the end, this is in a try catch finally statement so the timer should always be re-enabled.

One last thought, the server where this is being tested has high memory usage, could it be possible that the server is trying to reclaim memory and as a result, the service is faltering? Would making the process high priority make a difference?

Also, on another server where this has been tested, the service has run without incident for at least two weeks. This is why I feel that the issue could be to do with the server more than the service itself.

Any help/suggestions are very much appreciated!

Thanks.

resourcesys
resourcesysAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AndyAinscowFreelance programmer / ConsultantCommented:
Your question seems to be a bit contradictory.
You appear to say the service works (timers firing....) and then fails (after seconds, minutes, hours, days ?) BUT also say a process starts but the log shows no timer is firing.  

Do timers work, events fired and handled and then stop OR never fire (or don't get handled which isn't the same thing as not firing) ?

Is there a reason you require so many timers rather than just the one timer and based on an elapsed time perform the required action ?

Have you tried running this service on another PC.
AndyAinscowFreelance programmer / ConsultantCommented:
Sorry - ignore the final line, I just reread that you have already tried that.
resourcesysAuthor Commented:
Hi Andy,

Thanks for your reply.

Yes the service does function correctly with timers firing, then after hours/days, they are not firing anymore.

When the timers fire, at the start of processing we write a log message and at the end we write another. So we can tell that timers are not firing as the log messages are not being created and not data transfer is taking place.

The reason for the amount of timers is that we transfer data between one database and another, but there are several database servers and they can be polled at different time intervals.

Thanks.

Brett.
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

AndyAinscowFreelance programmer / ConsultantCommented:
Some years ago (10, 15, 20+) one was advised to use timers sparingly as there were only a few available to the system.  Whether that still is the case I don't really know but it certainly looks like your problem system is killing timers off for some reason.

Based on what you say you have something like:
Timer1_Event:  ProcessDatabase1
Timer2_Event:  ProcessDatabase2
Timer3_Event:  ProcessDatabase3
...

How much work would it be to re-arrange the logic a bit to have something like (I code mainly in C++ and C# but it should be clear what I mean - and this is pseudo code anyway to demonstrate the logic)

initialise one counter and just set one timer, for sake of example to fire every minute
long lTimerCount = 0;

Timer_Event
{
  lTimerCount = lTimerCount + 1;

  if (lTimerCount mod 5) == 0 then  ProcessDatabase1;    //DB1 is run every 5 minutes
  if (lTimerCount mod 7) == 0 then  ProcessDatabase2;    //DB2 is run every 2 minutes
  if (lTimerCount mod 4) == 0 then  ProcessDatabase3;    //DB3 is run every 4 minutes

}
AndyAinscowFreelance programmer / ConsultantCommented:
oops:  DB2 is run every 2 minutes should of course be DB2 is run every 7 minutes
resourcesysAuthor Commented:
Hi Andrew,

I'll consider changing the design of the service later.

It turns out that the issue was system memory, the server where the service was running had several instances of SQL Server running but where not actually in use. Once they were stopped memory usage went down from 95% to 50 %.

We are monitoring to make sure all is okay.

Thank you for your responses, they are much appreciated.

resourcesys.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
resourcesysAuthor Commented:
No code solution was necessary, simply the disabling of unused services.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.