private void Suspending(object sender, HostSuspendingEventArgs args) { // // The host is telling us we're about to be suspended. At this point, time // computations will still be in a valid range (next <= _period), but after // we're woken up again, Tick would start to go on a crucade to catch up. // // This has caused problems in the past, where the flood of events caused // batteries to drain etc (see design rationale discussion higher up). // // In order to mitigate this problem, we force Tick to suspend before its // next computation of the next due time. Notice we can't afford to block // during the Suspending event handler; the host expects us to respond to // this event quickly, such that we're not keeping the application from // suspending promptly. // lock (_gate) { if (_runState == RUNNING) { _suspendedAt = _stopwatch.Elapsed; _runState = SUSPENDED; if (!Environment.HasShutdownStarted) { _resumeEvent.Reset(); } } } }
private static void OnSuspending(object sender, HostSuspendingEventArgs e) { var suspending = Suspending; if (suspending != null) suspending(sender, e); }