views:

51

answers:

2

I have a .NET Windows Service (.NET 3.5) with a timer (System.Timers.Timer). The OnElapsed method looks like this:

    private void OnTimerElapsed(object source, ElapsedEventArgs e)
    {
        lock (this)
        {
            timer.Stop();
            //process some stuff here..
            ProcessStuff();
            timer.Interval = GetTimerInterval();
            timer.Start();
        }
    }

It works fine until it mysteriously stops working. This happens every x days and although the service has a status of started, it does not kick off the ProcessStuff() method. I use log4net and nothing is logged there or in the Windows Event logs. The ProcessStuff() spawns multiple threads to do some work.

How do I resolve this?

+1  A: 

Is it possible that ProcessStuff() or GetTimerInterval() throws an exception, so that timer.Start() is not executed?

Then you should maybe wrap that part in a try..catch and/or add some logging, e.g:

timer.Stop();
try
{
  //process some stuff here..
  ProcessStuff();
}
catch (Exception ex)
{
  // log the exception?
}
timer.Interval = GetTimerInterval();
timer.Start();
M4N
Highly likely theory.
Nick
It's possible but why would it happen only after x days. I do see unhandled exceptions in ProcessStuff log and the timer still works
DotnetDude
to be sure you might also add a global exception handle that logs to system event viewer "AppDomain.CurrentDomain.UnhandledException"
sadboy
@DotnetDude: it's difficult to answer without knowing what these methods do.
M4N
@Martin - I will add the try catch block. Are you suggesting that in the catch block, i try to restart the timer?
DotnetDude
@DotnetDude: no you'll have to restart it after the catch block. Otherwise it will only be started if an exception was thrown. I'll update my answer...
M4N
A: 

I have two ideas for you. One create a dump and see what is up or use the remote debugger and see what you are locking on.

rerun
How would I create a dump? Can you please explain
DotnetDude