From 168c631d84feb928c7340b4cce5086aef251f64b Mon Sep 17 00:00:00 2001 From: alkl58 Date: Tue, 1 Mar 2022 19:16:42 +0100 Subject: [PATCH] (Add) Auto-detect when the machine is idle [Alpha State] #88 --- NotEnoughAV1Encodes/MainWindow.xaml.cs | 72 ++++++++++++++++++- .../Views/ProgramSettings.xaml | 6 +- .../Views/ProgramSettings.xaml.cs | 6 +- NotEnoughAV1Encodes/resources/Settings.cs | 2 + .../resources/lang/Strings.Designer.cs | 18 +++++ .../resources/lang/Strings.de.resx | 10 ++- .../resources/lang/Strings.resx | 10 ++- NotEnoughAV1Encodes/win32/IdleDetection.cs | 36 ++++++++++ 8 files changed, 151 insertions(+), 9 deletions(-) create mode 100644 NotEnoughAV1Encodes/win32/IdleDetection.cs diff --git a/NotEnoughAV1Encodes/MainWindow.xaml.cs b/NotEnoughAV1Encodes/MainWindow.xaml.cs index 34da7a6..326eaa3 100644 --- a/NotEnoughAV1Encodes/MainWindow.xaml.cs +++ b/NotEnoughAV1Encodes/MainWindow.xaml.cs @@ -23,11 +23,18 @@ namespace NotEnoughAV1Encodes { public partial class MainWindow : MetroWindow { + /// Prevents Race Conditions on Startup private bool startupLock = true; + + /// Encoding the Queue in Parallel or not private bool QueueParallel; + + /// State of the Program [0 = IDLE; 1 = Encoding; 2 = Paused] + private int ProgramState; + private Settings settingsDB = new(); private Video.VideoDB videoDB = new(); - private int ProgramState; + private string uid; private CancellationTokenSource cancellationTokenSource; public VideoSettings PresetSettings = new(); @@ -1069,6 +1076,54 @@ private void AddToQueue(string identifier, bool skipSubs) File.WriteAllText(Path.Combine(Global.AppData, "NEAV1E", "Queue", videoDB.InputFileName + "_" + identifier + ".json"), JsonConvert.SerializeObject(queueElement, Formatting.Indented)); } + private void AutoPauseResume() + { + TimeSpan idleTime = win32.IdleDetection.GetInputIdleTime(); + double time = idleTime.TotalSeconds; + + Debug.WriteLine("AutoPauseResume() => " + time.ToString() + " Seconds"); + if (ProgramState is 1) + { + // Pause + if (time < 40.0) + { + Dispatcher.Invoke(() => ImageStartStop.Source = new BitmapImage(new Uri(@"/NotEnoughAV1Encodes;component/resources/img/resume.png", UriKind.Relative))); + Dispatcher.Invoke(() => LabelStartPauseButton.Content = LocalizedStrings.Instance["Resume"]); + Dispatcher.Invoke(() => Title = "NEAV1E - " + LocalizedStrings.Instance["ToggleSwitchAutoPauseResume"] + " => Paused"); + + // Pause all PIDs + foreach (int pid in Global.LaunchedPIDs) + { + Suspend.SuspendProcessTree(pid); + } + + ProgramState = 2; + } + } + else if (ProgramState is 2) + { + Dispatcher.Invoke(() => Title = "NEAV1E - " + LocalizedStrings.Instance["ToggleSwitchAutoPauseResume"] + " => Paused - System IDLE since " + time.ToString() + " seconds"); + // Resume + if (time > 60.0) + { + Dispatcher.Invoke(() => ImageStartStop.Source = new BitmapImage(new Uri(@"/NotEnoughAV1Encodes;component/resources/img/pause.png", UriKind.Relative))); + Dispatcher.Invoke(() => LabelStartPauseButton.Content = LocalizedStrings.Instance["Pause"]); + Dispatcher.Invoke(() => Title = "NEAV1E - " + LocalizedStrings.Instance["ToggleSwitchAutoPauseResume"] + " => Encoding"); + + // Resume all PIDs + if (ProgramState is 2) + { + foreach (int pid in Global.LaunchedPIDs) + { + Resume.ResumeProcessTree(pid); + } + } + + ProgramState = 1; + } + } + } + private void Shutdown() { if (settingsDB.ShutdownAfterEncode) @@ -1764,6 +1819,15 @@ private async Task MainStartAsync(CancellationToken _cancelToken) taskBarTimer.Interval = 3000; // every 3s taskBarTimer.Start(); + // Starts Timer for Auto Pause Resume functionality + System.Timers.Timer pauseResumeTimer = new(); + if (settingsDB.AutoResumePause) + { + pauseResumeTimer.Elapsed += (sender, e) => { AutoPauseResume(); }; + pauseResumeTimer.Interval = 20000; // check every 10s + pauseResumeTimer.Start(); + } + using SemaphoreSlim concurrencySemaphore = new(WorkerCountQueue); // Creates a tasks list List tasks = new(); @@ -1891,6 +1955,12 @@ private async Task MainStartAsync(CancellationToken _cancelToken) ButtonRemoveSelectedQueueItem.IsEnabled = true; ButtonEditSelectedItem.IsEnabled = true; + // Stop Timer for Auto Pause Resume functionality + if (settingsDB.AutoResumePause) + { + pauseResumeTimer.Stop(); + } + // Stop TaskbarItem Progressbar taskBarTimer.Stop(); Dispatcher.Invoke(() => TaskbarItemInfo.ProgressValue = 1.0); diff --git a/NotEnoughAV1Encodes/Views/ProgramSettings.xaml b/NotEnoughAV1Encodes/Views/ProgramSettings.xaml index 43643c2..4231a54 100644 --- a/NotEnoughAV1Encodes/Views/ProgramSettings.xaml +++ b/NotEnoughAV1Encodes/Views/ProgramSettings.xaml @@ -50,18 +50,20 @@ - + + + @@ -88,7 +90,7 @@