C# My windows Service dies after 3min RRS feed

  • Question

  • Hi,

    I have developed a small windows service in C# and the idea is to run and implement an action every few minutes depending on the time delay setting. 

    protected override void OnStart(string[] args)
                this.timer.Elapsed +=  new System.Timers.ElapsedEventHandler(onTimedEvent);
                this.timer.Interval = (config.prop_ApplicationRunningDelay * 1000); // e.g. 120 * 1000 = 3min
                this.timer.AutoReset = true;
                this.timer.Enabled = true;
                createRunningLog(); //does some action..

    It works perfectly when it is set for 2min or less, but when it is set for greater than 2min, the application dies and doesn't triger any action. I want my service run every 15min 24/7. 

    If I keepAlive(timer), wouldn't I use too much memory after a few hours or min? because it keeps creating memory and the garbage collector keeps them alive, what do you think?

    Any help will be appreciated.

    Tuesday, June 30, 2009 11:33 AM


  • Your KeepAlive() call doesn't accomplish anything, the OnStart() method will quickly exit.  It sounds like the kind of problem that would be caused by the garbage collector releasing the timer.  But there is no evidence for that in your code snippet.  The timer object is a member of your service class so there should always be a reference to it.  And there should be no other code running in the service so the garbage collector should never run.

    The only possible explanation is that there's other code that cancels the timer.  Or that somehow createRunningLog() takes more than 30 seconds when the timer is set to 3 minutes, causing the service manager to believe the service is dead.  Just wild guesses.

    Hans Passant.
    • Marked as answer by Zhi-Xin Ye Monday, July 6, 2009 11:45 AM
    Tuesday, June 30, 2009 1:02 PM