.NET Windows Service with timer stops responding

前端 未结 7 931
猫巷女王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:45

    Interesting issue. If it is truly just time related (i.e. not an exception), then I wonder if you can simply periodically recycle the timer - i.e.

    private void tickTack_Elapsed(object sender, ElapsedEventArgs e)
    {
        CheckForRecycle();
        // ... actual code
    }
    
    private void CheckForRecycle()
    {
        lock(someLock) {
            if(++tickCount > MAX_TICKS) {
                tickCount = 0;
                tickTack.Stop();
                // re-create timer
                tickTack = new Timer(...);
                tickTack.Elapsed += ...
                tickTack.Start();
            }
        }
    }
    

    You could probably merge chunks of this with the OnStart / OnStop etc to reduce duplication.

    0 讨论(0)
提交回复
热议问题