private static void StartPreventSleepTimer() { if (_preventSleepTimer?.IsActive ?? false) { return; } // sleep time setting is minimal 1 minute _preventSleepTimer = new AppTimer((s, e) => { PInvokeHelpers.PreventSleep(); }, 20 * 1000);// leave this interval, it works _preventSleepTimer.Start(); }
private static async Task MiningManagerMainLoop(CancellationToken stop) { try { var checkWaitTime = TimeSpan.FromMilliseconds(50); Func <bool> isActive = () => !stop.IsCancellationRequested; // sleep time setting is minimal 1 minute, 19-20s interval var preventSleepIntervalElapsedTimeChecker = new ElapsedTimeChecker(TimeSpan.FromSeconds(19), true); Logger.Info(Tag, "Starting MiningManagerMainLoop"); while (isActive()) { if (isActive()) { await TaskHelpers.TryDelay(checkWaitTime, stop); } // prevent sleep check if (isActive() && preventSleepIntervalElapsedTimeChecker.CheckAndMarkElapsedTime()) { var isMining = IsMiningEnabled; if (isMining) { PInvokeHelpers.PreventSleep(); } else { PInvokeHelpers.AllowMonitorPowerdownAndSleep(); } } // TODO should we check internet interval here??? } } catch (TaskCanceledException e) { Logger.Info(Tag, $"MiningManagerMainLoop TaskCanceledException: {e.Message}"); } catch (Exception e) { Logger.Error(Tag, $"TaskCanceledException Exception: {e.Message}"); } finally { Logger.Info(Tag, "Exiting MiningManagerMainLoop run cleanup"); // cleanup PInvokeHelpers.AllowMonitorPowerdownAndSleep(); } }