.NET Windows Service with timer stops responding

前端 未结 7 961
猫巷女王i
猫巷女王i 2020-11-30 05:17

I have a windows service written in c#. It has a timer inside, which fires some functions on a regular basis. So the skeleton of my service:

public partial cl         


        
7条回答
  •  隐瞒了意图╮
    2020-11-30 05:33

    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:

        void TimeLoop(object arg)
        {
            stopTimer();
    
            //Do some stuff
    
            startTimer();
        }
    

    You could also lock(...) your main loop to protect against reentrancy.

提交回复
热议问题