diff --git a/app/Gpu/AmdGpuControl.cs b/app/Gpu/AmdGpuControl.cs index 3cd18a1e..2083d5c1 100644 --- a/app/Gpu/AmdGpuControl.cs +++ b/app/Gpu/AmdGpuControl.cs @@ -135,19 +135,7 @@ public class AmdGpuControl : IGpuControl } } - foreach (string kill in appNames) - foreach (var process in Process.GetProcessesByName(kill)) - { - try - { - process.Kill(); - Logger.WriteLine($"Stopped: {process.ProcessName}"); - } - catch (Exception ex) - { - Logger.WriteLine($"Failed to stop: {process.ProcessName} {ex.Message}"); - } - } + foreach (string kill in appNames) ProcessHelper.KillByName(kill); } catch (Exception ex) diff --git a/app/Gpu/IGpuControl.cs b/app/Gpu/IGpuControl.cs index b9cedcd1..db8ceb70 100644 --- a/app/Gpu/IGpuControl.cs +++ b/app/Gpu/IGpuControl.cs @@ -6,7 +6,6 @@ public interface IGpuControl : IDisposable { public string FullName { get; } int? GetCurrentTemperature(); int? GetGpuUse(); - void KillGPUApps(); } diff --git a/app/Gpu/NvidiaGpuControl.cs b/app/Gpu/NvidiaGpuControl.cs index 42db69d4..c60b9948 100644 --- a/app/Gpu/NvidiaGpuControl.cs +++ b/app/Gpu/NvidiaGpuControl.cs @@ -55,26 +55,13 @@ public class NvidiaGpuControl : IGpuControl 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); - } - } + foreach (Process process in processes) ProcessHelper.KillByProcess(process); } catch (Exception ex) { Logger.WriteLine(ex.Message); } - - //NVIDIA.RestartDisplayDriver(); } diff --git a/app/HardwareControl.cs b/app/HardwareControl.cs index 092151e8..aa0e2358 100644 --- a/app/HardwareControl.cs +++ b/app/HardwareControl.cs @@ -187,7 +187,7 @@ public static class HardwareControl public static void KillGPUApps() { - List tokill = new() { "EADesktop", "RadeonSoftware", "epicgameslauncher" }; + List tokill = new() { "EADesktop", "RadeonSoftware", "epicgameslauncher", "ASUSSmartDisplayControl" }; if (AppConfig.isConfig("kill_gpu_apps")) { @@ -196,19 +196,7 @@ public static class HardwareControl tokill.Add("nvcplui"); } - foreach (string kill in tokill) - foreach (var process in Process.GetProcessesByName(kill)) - { - try - { - process.Kill(); - Logger.WriteLine($"Stopped: {process.ProcessName}"); - } - catch (Exception ex) - { - Logger.WriteLine($"Failed to stop: {process.ProcessName} {ex.Message}"); - } - } + foreach (string kill in tokill) ProcessHelper.KillByName(kill); if (AppConfig.isConfig("kill_gpu_apps") && GpuControl is not null) { diff --git a/app/InputDispatcher.cs b/app/InputDispatcher.cs index 16a8c02b..d7e9aacf 100644 --- a/app/InputDispatcher.cs +++ b/app/InputDispatcher.cs @@ -429,7 +429,8 @@ namespace GHelper public static void SetBacklightAuto(bool init = false) { if (init) AsusUSB.Init(); - AsusUSB.ApplyBrightness(GetBacklight(), "Auto"); + + if (!OptimizationService.IsRunning()) AsusUSB.ApplyBrightness(GetBacklight(), "Auto"); } public static void SetBacklight(int delta) diff --git a/app/Logger.cs b/app/Logger.cs index 76e31c1e..622f7f33 100644 --- a/app/Logger.cs +++ b/app/Logger.cs @@ -30,7 +30,7 @@ public static class Logger try { var file = File.ReadAllLines(logFile); - int skip = Math.Max(0, file.Count() - 500); + int skip = Math.Max(0, file.Count() - 1000); File.WriteAllLines(logFile, file.Skip(skip).ToArray()); } catch { } diff --git a/app/ProcessHelper.cs b/app/ProcessHelper.cs new file mode 100644 index 00000000..9293fe7b --- /dev/null +++ b/app/ProcessHelper.cs @@ -0,0 +1,97 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Security.Principal; +using System.Text; +using System.Threading.Tasks; + +namespace GHelper +{ + public static class ProcessHelper + { + private static long lastAdmin; + + public static void CheckAlreadyRunning() + { + Process currentProcess = Process.GetCurrentProcess(); + Process[] processes = Process.GetProcessesByName(currentProcess.ProcessName); + + if (processes.Length > 1) + { + foreach (Process process in processes) + if (process.Id != currentProcess.Id) + try + { + process.Kill(); + } + catch (Exception ex) + { + Logger.WriteLine(ex.ToString()); + MessageBox.Show(Properties.Strings.AppAlreadyRunningText, Properties.Strings.AppAlreadyRunning, MessageBoxButtons.OK); + Application.Exit(); + return; + } + } + } + + public static bool IsUserAdministrator() + { + WindowsIdentity identity = WindowsIdentity.GetCurrent(); + WindowsPrincipal principal = new WindowsPrincipal(identity); + return principal.IsInRole(WindowsBuiltInRole.Administrator); + } + + public static void RunAsAdmin(string? param = null) + { + + if (Math.Abs(DateTimeOffset.Now.ToUnixTimeMilliseconds() - lastAdmin) < 2000) return; + lastAdmin = DateTimeOffset.Now.ToUnixTimeMilliseconds(); + + // Check if the current user is an administrator + if (!IsUserAdministrator()) + { + ProcessStartInfo startInfo = new ProcessStartInfo(); + startInfo.UseShellExecute = true; + startInfo.WorkingDirectory = Environment.CurrentDirectory; + startInfo.FileName = Application.ExecutablePath; + startInfo.Arguments = param; + startInfo.Verb = "runas"; + Process.Start(startInfo); + Application.Exit(); + } + } + + + public static void KillByName(string name) + { + foreach (var process in Process.GetProcessesByName(name)) + { + try + { + process.Kill(); + Logger.WriteLine($"Stopped: {process.ProcessName}"); + } + catch (Exception ex) + { + Logger.WriteLine($"Failed to stop: {process.ProcessName} {ex.Message}"); + } + } + } + + public static void KillByProcess(Process process) + { + try + { + process.Kill(); + Logger.WriteLine($"Stopped: {process.ProcessName}"); + } + catch (Exception ex) + { + Logger.WriteLine($"Failed to stop: {process.ProcessName} {ex.Message}"); + } + } + + + } +} diff --git a/app/Program.cs b/app/Program.cs index 9b48ec85..6adf399c 100644 --- a/app/Program.cs +++ b/app/Program.cs @@ -25,7 +25,6 @@ namespace GHelper private static long lastAuto; private static long lastTheme; - private static long lastAdmin; public static InputDispatcher inputDispatcher; @@ -48,7 +47,7 @@ namespace GHelper Debug.WriteLine(CultureInfo.CurrentUICulture); //Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture("fr"); - CheckProcesses(); + ProcessHelper.CheckAlreadyRunning(); try { @@ -67,7 +66,7 @@ namespace GHelper } Logger.WriteLine("------------"); - Logger.WriteLine("App launched: " + AppConfig.GetModel() + " :" + Assembly.GetExecutingAssembly().GetName().Version.ToString() + CultureInfo.CurrentUICulture + (IsUserAdministrator() ? "." : "")); + Logger.WriteLine("App launched: " + AppConfig.GetModel() + " :" + Assembly.GetExecutingAssembly().GetName().Version.ToString() + CultureInfo.CurrentUICulture + (ProcessHelper.IsUserAdministrator() ? "." : "")); Application.EnableVisualStyles(); @@ -211,55 +210,6 @@ namespace GHelper } - static void CheckProcesses() - { - Process currentProcess = Process.GetCurrentProcess(); - Process[] processes = Process.GetProcessesByName(currentProcess.ProcessName); - - if (processes.Length > 1) - { - foreach (Process process in processes) - if (process.Id != currentProcess.Id) - try - { - process.Kill(); - } - catch (Exception ex) - { - Logger.WriteLine(ex.ToString()); - MessageBox.Show(Properties.Strings.AppAlreadyRunningText, Properties.Strings.AppAlreadyRunning, MessageBoxButtons.OK); - Application.Exit(); - return; - } - } - } - - public static bool IsUserAdministrator() - { - WindowsIdentity identity = WindowsIdentity.GetCurrent(); - WindowsPrincipal principal = new WindowsPrincipal(identity); - return principal.IsInRole(WindowsBuiltInRole.Administrator); - } - - public static void RunAsAdmin(string? param = null) - { - - if (Math.Abs(DateTimeOffset.Now.ToUnixTimeMilliseconds() - lastAdmin) < 2000) return; - lastAdmin = DateTimeOffset.Now.ToUnixTimeMilliseconds(); - - // Check if the current user is an administrator - if (!IsUserAdministrator()) - { - ProcessStartInfo startInfo = new ProcessStartInfo(); - startInfo.UseShellExecute = true; - startInfo.WorkingDirectory = Environment.CurrentDirectory; - startInfo.FileName = Application.ExecutablePath; - startInfo.Arguments = param; - startInfo.Verb = "runas"; - Process.Start(startInfo); - Application.Exit(); - } - } } } \ No newline at end of file diff --git a/app/Settings.cs b/app/Settings.cs index 98a1735d..f282e133 100644 --- a/app/Settings.cs +++ b/app/Settings.cs @@ -168,7 +168,7 @@ namespace GHelper int trim = model.LastIndexOf("_"); if (trim > 0) model = model.Substring(0, trim); - labelModel.Text = model + (Program.IsUserAdministrator() ? "." : ""); + labelModel.Text = model + (ProcessHelper.IsUserAdministrator() ? "." : ""); TopMost = AppConfig.getConfig("topmost") == 1; @@ -1075,7 +1075,7 @@ namespace GHelper } int setStatus = nvControl.SetClocks(gpu_core, gpu_memory); - if (launchAsAdmin && setStatus == -1) Program.RunAsAdmin("gpu"); + if (launchAsAdmin && setStatus == -1) ProcessHelper.RunAsAdmin("gpu"); } catch (Exception ex) @@ -1502,9 +1502,9 @@ namespace GHelper if (dialogResult == DialogResult.No) return; } - Program.RunAsAdmin("gpurestart"); + ProcessHelper.RunAsAdmin("gpurestart"); - if (!Program.IsUserAdministrator()) return; + if (!ProcessHelper.IsUserAdministrator()) return; Logger.WriteLine("Trying to restart dGPU"); diff --git a/app/Startup.cs b/app/Startup.cs index abbaaa4f..f8da4539 100644 --- a/app/Startup.cs +++ b/app/Startup.cs @@ -16,7 +16,7 @@ public class Startup public static void ReScheduleAdmin() { - if (Program.IsUserAdministrator() && IsScheduled()) + if (ProcessHelper.IsUserAdministrator() && IsScheduled()) { UnSchedule(); Schedule(); @@ -38,7 +38,7 @@ public class Startup td.Triggers.Add(new LogonTrigger { UserId = userId }); td.Actions.Add(strExeFilePath); - if (Program.IsUserAdministrator()) + if (ProcessHelper.IsUserAdministrator()) td.Principal.RunLevel = TaskRunLevel.Highest; td.Settings.StopIfGoingOnBatteries = false; @@ -54,10 +54,10 @@ public class Startup } catch (Exception e) { - if (Program.IsUserAdministrator()) + if (ProcessHelper.IsUserAdministrator()) MessageBox.Show("Can't create a start up task. Try running Task Scheduler by hand and manually deleting GHelper task if it exists there.", "Scheduler Error", MessageBoxButtons.OK); - else - Program.RunAsAdmin(); + else + ProcessHelper.RunAsAdmin(); } } @@ -73,10 +73,10 @@ public class Startup } catch (Exception e) { - if (Program.IsUserAdministrator()) + if (ProcessHelper.IsUserAdministrator()) MessageBox.Show("Can't remove task. Try running Task Scheduler by hand and manually deleting GHelper task if it exists there.", "Scheduler Error", MessageBoxButtons.OK); else - Program.RunAsAdmin(); + ProcessHelper.RunAsAdmin(); } } }