C # Windows Service - Multiple Timers

I am developing a Windows service that should perform several tasks at different periods.

Currently, I have two timers, a full timer and a stock timer, working at various intervals, as defined below.

 fullTimer = new System.Timers.Timer();
 fullTimer.Elapsed += new ElapsedEventHandler(OnElapsedTime);
 fullTimer.Interval = Convert.ToDouble(interval * 1000);
 fullTimer.AutoReset = true;
 fullTimer.Start(); // or fullTimer.Enabled = true;
 GC.KeepAlive(fullTimer);

   stockTimer = new System.Timers.Timer();
   stockTimer.Elapsed += new ElapsedEventHandler(StockOnElapsedTime);
   stockTimer.Interval = Convert.ToDouble(stockInterval * 1000);
   stockTimer.AutoReset = true;
   stockTimer.Start();
   GC.KeepAlive(stockTimer);

Can anyone understand why the timers will not shoot. I get really weird behavior. If I start the stock handler manually

StockOnElapsedTime(null,null);

The timer seems to continue to fire correctly.

+3
source share
3 answers

GC.KeepAlive , , , , , . , KeepAlive. , ?

+2

Timer .

GC.KeepAlive .

0

, , , , GC.KeepAlive ( , ). OnStart?

0

Source: https://habr.com/ru/post/1755229/


All Articles