0

I have a windows service that needs run a method every 1 min. I'm using a System.Threading.Timer and it works fine for 30-35 min and then the method stops running without any error

My code looks like this

Variables

private static int intervalSend = 60000;
private TimerCallback callback;
private static Timer sendTimer;

Init

   callback = new TimerCallback(SendAliveInquiry);
   sendTimer = new System.Threading.Timer(callback, null, 0, intervalSend);

Method

private void SendAliveInquiry(object obj)
{
  Method logic
}

Any idea why it stops running after a while?

Maverick
  • 1,396
  • 5
  • 22
  • 42
Birger
  • 349
  • 1
  • 4
  • 18
  • 5
    Exceptions? Bad temper? Weather? Tim lost his wallet? It's practically impossible to give a definitive answer. Do you log errors? Do you catch and handle exceptions? Does the service enter a stopped state, or just not respond? – J. Steen Jul 28 '16 at 07:48
  • 1
    Also, I think that you should have it as a scheduled task. Take a look at [Task Scheduler](https://technet.microsoft.com/en-us/library/cc748993(v=ws.11).aspx). – Timo Salomäki Jul 28 '16 at 07:51
  • 1
    This looks like a repeat of http://stackoverflow.com/questions/3635852/system-threading-timer-not-firing-after-some-time which suggests your sendTimer variable is being garbage collected. As in that answer; can you guarantee your static is not being disposed ? Can you post a small reproducible whole program we can test with ? – PhillipH Jul 28 '16 at 07:54
  • The service do not stop and still complete other task(send / receive on a message queue). I log error but nothing seem to crash. – Birger Jul 28 '16 at 08:02
  • Why is the timer static? Why is callback not static? In which class is the timer created? Is this instance garbage collected? – JeffRSon Jul 28 '16 at 09:47

0 Answers0