From 42cc1bdb98c5f13de95ec8f9bcb521597c0061e2 Mon Sep 17 00:00:00 2001 From: Serge <5920850+seerge@users.noreply.github.com> Date: Mon, 29 May 2023 12:16:19 +0200 Subject: [PATCH] Added option to stop all GPU apps before setting Eco --- app/Extra.Designer.cs | 59 ++++++++++++++++++------------ app/Extra.cs | 10 +++++ app/Gpu/NvidiaGpuControl.cs | 38 +++++++++++++++++-- app/Properties/Strings.Designer.cs | 9 +++++ app/Properties/Strings.resx | 3 ++ app/Settings.cs | 7 ++++ 6 files changed, 100 insertions(+), 26 deletions(-) diff --git a/app/Extra.Designer.cs b/app/Extra.Designer.cs index afaa3655..1ce3bdf8 100644 --- a/app/Extra.Designer.cs +++ b/app/Extra.Designer.cs @@ -51,6 +51,8 @@ namespace GHelper pictureHelp = new PictureBox(); groupLight = new GroupBox(); panelBacklightExtra = new Panel(); + numericBacklightPluggedTime = new NumericUpDown(); + labelBacklightTimeoutPlugged = new Label(); numericBacklightTime = new NumericUpDown(); labelBacklightTimeout = new Label(); labelBrightness = new Label(); @@ -81,24 +83,23 @@ namespace GHelper checkSleepLid = new CheckBox(); checkShutdownLid = new CheckBox(); groupOther = new GroupBox(); + checkGpuApps = new CheckBox(); checkAutoApplyWindowsPowerMode = new CheckBox(); checkKeyboardAuto = new CheckBox(); checkUSBC = new CheckBox(); checkNoOverdrive = new CheckBox(); checkTopmost = new CheckBox(); - numericBacklightPluggedTime = new NumericUpDown(); - labelBacklightTimeoutPlugged = new Label(); groupBindings.SuspendLayout(); tableKeys.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)pictureHelp).BeginInit(); groupLight.SuspendLayout(); panelBacklightExtra.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)numericBacklightPluggedTime).BeginInit(); ((System.ComponentModel.ISupportInitialize)numericBacklightTime).BeginInit(); ((System.ComponentModel.ISupportInitialize)trackBrightness).BeginInit(); panelXMG.SuspendLayout(); tableBacklight.SuspendLayout(); groupOther.SuspendLayout(); - ((System.ComponentModel.ISupportInitialize)numericBacklightPluggedTime).BeginInit(); SuspendLayout(); // // groupBindings @@ -326,6 +327,22 @@ namespace GHelper panelBacklightExtra.Size = new Size(948, 241); panelBacklightExtra.TabIndex = 43; // + // numericBacklightPluggedTime + // + numericBacklightPluggedTime.Location = new Point(655, 181); + numericBacklightPluggedTime.Maximum = new decimal(new int[] { 3600, 0, 0, 0 }); + numericBacklightPluggedTime.Name = "numericBacklightPluggedTime"; + numericBacklightPluggedTime.Size = new Size(240, 39); + numericBacklightPluggedTime.TabIndex = 49; + // + // labelBacklightTimeoutPlugged + // + labelBacklightTimeoutPlugged.Location = new Point(13, 183); + labelBacklightTimeoutPlugged.Name = "labelBacklightTimeoutPlugged"; + labelBacklightTimeoutPlugged.Size = new Size(636, 45); + labelBacklightTimeoutPlugged.TabIndex = 48; + labelBacklightTimeoutPlugged.Text = "Seconds to turn off backlight when plugged"; + // // numericBacklightTime // numericBacklightTime.Location = new Point(655, 133); @@ -667,6 +684,7 @@ namespace GHelper // // groupOther // + groupOther.Controls.Add(checkGpuApps); groupOther.Controls.Add(checkAutoApplyWindowsPowerMode); groupOther.Controls.Add(checkKeyboardAuto); groupOther.Controls.Add(checkUSBC); @@ -675,15 +693,25 @@ namespace GHelper groupOther.Dock = DockStyle.Top; groupOther.Location = new Point(10, 897); groupOther.Name = "groupOther"; - groupOther.Size = new Size(954, 276); + groupOther.Size = new Size(954, 310); groupOther.TabIndex = 2; groupOther.TabStop = false; groupOther.Text = "Other"; // + // checkGpuApps + // + checkGpuApps.AutoSize = true; + checkGpuApps.Location = new Point(25, 220); + checkGpuApps.Name = "checkGpuApps"; + checkGpuApps.Size = new Size(544, 36); + checkGpuApps.TabIndex = 48; + checkGpuApps.Text = "Stop all apps using GPU when switching to Eco"; + checkGpuApps.UseVisualStyleBackColor = true; + // // checkAutoApplyWindowsPowerMode // checkAutoApplyWindowsPowerMode.AutoSize = true; - checkAutoApplyWindowsPowerMode.Location = new Point(25, 220); + checkAutoApplyWindowsPowerMode.Location = new Point(25, 268); checkAutoApplyWindowsPowerMode.Name = "checkAutoApplyWindowsPowerMode"; checkAutoApplyWindowsPowerMode.Size = new Size(416, 36); checkAutoApplyWindowsPowerMode.TabIndex = 47; @@ -731,29 +759,13 @@ namespace GHelper checkTopmost.Text = Strings.WindowTop; checkTopmost.UseVisualStyleBackColor = true; // - // numericBacklightPluggedTime - // - numericBacklightPluggedTime.Location = new Point(655, 181); - numericBacklightPluggedTime.Maximum = new decimal(new int[] { 3600, 0, 0, 0 }); - numericBacklightPluggedTime.Name = "numericBacklightPluggedTime"; - numericBacklightPluggedTime.Size = new Size(240, 39); - numericBacklightPluggedTime.TabIndex = 49; - // - // labelBacklightTimeoutPlugged - // - labelBacklightTimeoutPlugged.Location = new Point(13, 183); - labelBacklightTimeoutPlugged.Name = "labelBacklightTimeoutPlugged"; - labelBacklightTimeoutPlugged.Size = new Size(636, 45); - labelBacklightTimeoutPlugged.TabIndex = 48; - labelBacklightTimeoutPlugged.Text = "Seconds to turn off backlight when plugged"; - // // Extra // AutoScaleDimensions = new SizeF(13F, 32F); AutoScaleMode = AutoScaleMode.Font; AutoSize = true; AutoSizeMode = AutoSizeMode.GrowAndShrink; - ClientSize = new Size(974, 1131); + ClientSize = new Size(974, 1220); Controls.Add(groupOther); Controls.Add(groupLight); Controls.Add(groupBindings); @@ -775,6 +787,7 @@ namespace GHelper groupLight.PerformLayout(); panelBacklightExtra.ResumeLayout(false); panelBacklightExtra.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)numericBacklightPluggedTime).EndInit(); ((System.ComponentModel.ISupportInitialize)numericBacklightTime).EndInit(); ((System.ComponentModel.ISupportInitialize)trackBrightness).EndInit(); panelXMG.ResumeLayout(false); @@ -782,7 +795,6 @@ namespace GHelper tableBacklight.ResumeLayout(false); groupOther.ResumeLayout(false); groupOther.PerformLayout(); - ((System.ComponentModel.ISupportInitialize)numericBacklightPluggedTime).EndInit(); ResumeLayout(false); PerformLayout(); } @@ -846,5 +858,6 @@ namespace GHelper private TextBox textM1; private NumericUpDown numericBacklightPluggedTime; private Label labelBacklightTimeoutPlugged; + private CheckBox checkGpuApps; } } \ No newline at end of file diff --git a/app/Extra.cs b/app/Extra.cs index 1fa9053d..d6d6dac9 100644 --- a/app/Extra.cs +++ b/app/Extra.cs @@ -99,6 +99,8 @@ namespace GHelper labelBacklightLid.Text = Properties.Strings.Lid; labelBacklightLogo.Text = Properties.Strings.Logo; + checkGpuApps.Text = Properties.Strings.KillGpuApps; + Text = Properties.Strings.ExtraSettings; InitTheme(); @@ -217,6 +219,14 @@ namespace GHelper numericBacklightTime.ValueChanged += NumericBacklightTime_ValueChanged; numericBacklightPluggedTime.ValueChanged += NumericBacklightTime_ValueChanged; + checkGpuApps.Checked = AppConfig.isConfig("kill_gpu_apps"); + checkGpuApps.CheckedChanged += CheckGpuApps_CheckedChanged; + + } + + private void CheckGpuApps_CheckedChanged(object? sender, EventArgs e) + { + AppConfig.setConfig("kill_gpu_apps", (checkGpuApps.Checked ? 1 : 0)); } private void NumericBacklightTime_ValueChanged(object? sender, EventArgs e) diff --git a/app/Gpu/NvidiaGpuControl.cs b/app/Gpu/NvidiaGpuControl.cs index 57114f62..e789ca62 100644 --- a/app/Gpu/NvidiaGpuControl.cs +++ b/app/Gpu/NvidiaGpuControl.cs @@ -1,4 +1,5 @@ -using NvAPIWrapper.GPU; +using NvAPIWrapper; +using NvAPIWrapper.GPU; using NvAPIWrapper.Native; using NvAPIWrapper.Native.Delegates; using NvAPIWrapper.Native.GPU; @@ -35,8 +36,7 @@ public class NvidiaGpuControl : IGpuControl public int? GetCurrentTemperature() { - if (!IsValid) - return null; + if (!IsValid) return null; PhysicalGPU internalGpu = _internalGpu!; IThermalSensor? gpuSensor = @@ -50,6 +50,38 @@ public class NvidiaGpuControl : IGpuControl { } + public void KillGPUApps() + { + + if (!IsValid) return; + PhysicalGPU internalGpu = _internalGpu!; + + try + { + Process[] processes = internalGpu.GetActiveApplications(); + foreach (Process process in processes) + { + try + { + process?.Kill(); + Logger.WriteLine("Stopped: " + process.ProcessName); + } + catch (Exception ex) + { + Logger.WriteLine(ex.Message); + } + } + } + catch (Exception ex) + { + Logger.WriteLine(ex.Message); + } + + + + //NVIDIA.RestartDisplayDriver(); + } + public int GetClocks(out int core, out int memory) { diff --git a/app/Properties/Strings.Designer.cs b/app/Properties/Strings.Designer.cs index 4620c5c0..0a5125c4 100644 --- a/app/Properties/Strings.Designer.cs +++ b/app/Properties/Strings.Designer.cs @@ -627,6 +627,15 @@ namespace GHelper.Properties { } } + /// + /// Looks up a localized string similar to Stop all apps using GPU when switching to Eco. + /// + internal static string KillGpuApps { + get { + return ResourceManager.GetString("KillGpuApps", resourceCulture); + } + } + /// /// Looks up a localized string similar to Laptop Backlight. /// diff --git a/app/Properties/Strings.resx b/app/Properties/Strings.resx index 67eb4212..063d5d3e 100644 --- a/app/Properties/Strings.resx +++ b/app/Properties/Strings.resx @@ -306,6 +306,9 @@ Lower backlight brightness on battery and back when plugged + + Stop all apps using GPU when switching to Eco + Laptop Backlight diff --git a/app/Settings.cs b/app/Settings.cs index 6fd002d8..259e367a 100644 --- a/app/Settings.cs +++ b/app/Settings.cs @@ -6,6 +6,7 @@ using System.Net; using System.Reflection; using System.Text.Json; using System.Timers; +using System.Windows.Forms; using Tools; namespace GHelper @@ -1478,9 +1479,15 @@ namespace GHelper protected static void KillGPUApps() { string[] tokill = { "EADesktop", "RadeonSoftware", "epicgameslauncher" }; + foreach (string kill in tokill) foreach (var process in Process.GetProcessesByName(kill)) process.Kill(); + if (AppConfig.isConfig("kill_gpu_apps") && HardwareControl.GpuControl is not null && HardwareControl.GpuControl.IsNvidia) + { + NvidiaGpuControl nvControl = (NvidiaGpuControl)HardwareControl.GpuControl; + nvControl.KillGPUApps(); + } } public void SetGPUEco(int eco, bool hardWay = false)