Sleep timer, ideally reactive

Under normal conditions, the following code is well colored every two minutes:

Observable.Interval(TimeSpan.FromSeconds(120)).Subscribe( l => { var now = DateTime.Now; Console.WriteLine( "Tick at " + now.ToString("hh:mm:ss") + " Count " + l.ToString()); }); 

Behavior after the device sleeps for a while and then resumes

 Tick at 11:30:00 Count 0 (11:31:55 put computer to sleep) (no tick at 11:32:00 because PC is suspended, which is fine) (no tick at 11:34:00 because PC is suspended) (11:34:30 wake computer up) (no tick as soon as possible to indicate that ticks were missed) Tick at 11:36:30 Count 1 

It seems that the type of observed timer interval resets itself and starts the interval again from the moment the device resumes working. (I observed similar behavior with an observable timer)

Sleep timer

I need a timer that does not restart the interval when resuming, but ticks once if any ticks are skipped during sleep

  • more than once for each missing tick, but once in total if any number of ticks is missing
  • this is especially true for TimeSpans hours or days, which in some cases will cause the timer to stop extinguishing if the laptop goes to bed even once a day.
  • would it be nice to have an overload for the observed interval and timer defining such misfire instructions?
  • If you cannot make it work in the Rx observation space, are there other timers, regardless of sleep mode? [/ li>
  • Quartz has not been tested for this yet, but ideally would like to not include more dependencies, so the Observable solution or managed .NET is preferable.
  • the same should work for other deeper sleep modes, for example. sleep mode
+5
source share

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


All Articles