mirror of
https://github.com/jkocon/g-helper.git
synced 2026-02-23 13:00:52 +01:00
Improved GPU restart
This commit is contained in:
@@ -16,7 +16,7 @@
|
||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
<ProduceReferenceAssembly>False</ProduceReferenceAssembly>
|
||||
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
|
||||
<AssemblyVersion>0.60</AssemblyVersion>
|
||||
<AssemblyVersion>0.61</AssemblyVersion>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
|
||||
|
||||
@@ -90,7 +90,7 @@ public class NvidiaGpuControl : IGpuControl
|
||||
}
|
||||
|
||||
|
||||
public bool RestartGPU()
|
||||
public bool RestartGPUPnP()
|
||||
{
|
||||
|
||||
if (!IsValid) return false;
|
||||
@@ -99,13 +99,30 @@ public class NvidiaGpuControl : IGpuControl
|
||||
{
|
||||
PhysicalGPU internalGpu = _internalGpu!;
|
||||
var pnpDeviceId = internalGpu.BusInformation.PCIIdentifiers.ToString();
|
||||
Logger.WriteLine("Device ID:"+ pnpDeviceId);
|
||||
Logger.WriteLine("Device ID:" + pnpDeviceId);
|
||||
RunCMD("pnputil", $"/disable-device /deviceid \"{pnpDeviceId}\"");
|
||||
Thread.Sleep(3000);
|
||||
RunCMD("pnputil", $"/enable-device /deviceid \"{pnpDeviceId}\"");
|
||||
Thread.Sleep(2000);
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.WriteLine(ex.ToString());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public bool RestartGPU()
|
||||
{
|
||||
try
|
||||
{
|
||||
string script = @"$device = Get-PnpDevice | Where-Object { $_.FriendlyName -imatch 'NVIDIA' -and $_.Class -eq 'Display' }; Disable-PnpDevice $device.InstanceId -Confirm:$false; Start-Sleep -Seconds 2; Enable-PnpDevice $device.InstanceId -Confirm:$false";
|
||||
Logger.WriteLine(script);
|
||||
RunCMD("powershell", script);
|
||||
Thread.Sleep(2000);
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex )
|
||||
{
|
||||
Logger.WriteLine(ex.ToString());
|
||||
|
||||
38
app/OptimizationService.cs
Normal file
38
app/OptimizationService.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Text;
|
||||
|
||||
namespace GHelper
|
||||
{
|
||||
public static class OptimizationService
|
||||
{
|
||||
public static void SetChargeLimit (int newValue)
|
||||
{
|
||||
// Set the path to the .ini file
|
||||
string path = @"C:\ProgramData\ASUS\ASUS System Control Interface\ASUSOptimization\Customization.ini";
|
||||
|
||||
|
||||
// Make a backup copy of the INI file
|
||||
string backupPath = path + ".bak";
|
||||
File.Copy(path, backupPath, true);
|
||||
|
||||
string fileContents = File.ReadAllText(path, Encoding.Unicode);
|
||||
|
||||
// Find the section [BatteryHealthCharging]
|
||||
string sectionPattern = @"\[BatteryHealthCharging\]\s*(version=\d+)?\s+value=(\d+)";
|
||||
Match sectionMatch = Regex.Match(fileContents, sectionPattern);
|
||||
if (sectionMatch.Success)
|
||||
{
|
||||
// Replace the value with the new value
|
||||
string oldValueString = sectionMatch.Groups[2].Value;
|
||||
int oldValue = int.Parse(oldValueString);
|
||||
string newSection = sectionMatch.Value.Replace($"value={oldValue}", $"value={newValue}");
|
||||
|
||||
// Replace the section in the file contents
|
||||
fileContents = fileContents.Replace(sectionMatch.Value, newSection);
|
||||
|
||||
File.WriteAllText(path, fileContents, Encoding.Unicode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -291,16 +291,19 @@ namespace GHelper
|
||||
{
|
||||
settingsForm.Show();
|
||||
settingsForm.Activate();
|
||||
settingsForm.VisualiseGPUMode();
|
||||
|
||||
if (action == "gpu")
|
||||
switch (action)
|
||||
{
|
||||
Startup.ReScheduleAdmin();
|
||||
settingsForm.FansToggle();
|
||||
case "gpu":
|
||||
Startup.ReScheduleAdmin();
|
||||
settingsForm.FansToggle();
|
||||
break;
|
||||
case "gpurestart":
|
||||
settingsForm.RestartGPU(false);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
settingsForm.VisualiseGPUMode();
|
||||
|
||||
}
|
||||
|
||||
static void TrayIcon_MouseClick(object? sender, MouseEventArgs e)
|
||||
|
||||
2
app/Properties/Strings.Designer.cs
generated
2
app/Properties/Strings.Designer.cs
generated
@@ -826,7 +826,7 @@ namespace GHelper.Properties {
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Something is using dGPU and blocking Eco mode. Restart dGPU in device manager and try to set Eco again?.
|
||||
/// Looks up a localized string similar to Something is using dGPU and preventing Eco mode. Restart dGPU in device manager? * Please proceed on your own risk..
|
||||
/// </summary>
|
||||
internal static string RestartGPU {
|
||||
get {
|
||||
|
||||
@@ -373,7 +373,7 @@
|
||||
<value>Quit</value>
|
||||
</data>
|
||||
<data name="RestartGPU" xml:space="preserve">
|
||||
<value>Something is using dGPU and blocking Eco mode. Restart dGPU in device manager and try to set Eco again?</value>
|
||||
<value>Something is using dGPU and preventing Eco mode. Restart dGPU in device manager? * Please proceed on your own risk.</value>
|
||||
</data>
|
||||
<data name="RPM" xml:space="preserve">
|
||||
<value>RPM</value>
|
||||
|
||||
@@ -1454,19 +1454,43 @@ namespace GHelper
|
||||
|
||||
}
|
||||
|
||||
public bool RestartGPU()
|
||||
public void RestartGPU(bool confirm = true)
|
||||
{
|
||||
if (HardwareControl.GpuControl is null) return false;
|
||||
if (!HardwareControl.GpuControl!.IsNvidia) return false;
|
||||
if (HardwareControl.GpuControl is null) return;
|
||||
if (!HardwareControl.GpuControl!.IsNvidia) return;
|
||||
|
||||
DialogResult dialogResult = MessageBox.Show(Properties.Strings.RestartGPU, Properties.Strings.EcoMode, MessageBoxButtons.YesNo);
|
||||
if (dialogResult == DialogResult.No) return false;
|
||||
if (confirm)
|
||||
{
|
||||
DialogResult dialogResult = MessageBox.Show(Properties.Strings.RestartGPU, Properties.Strings.EcoMode, MessageBoxButtons.YesNo);
|
||||
if (dialogResult == DialogResult.No) return;
|
||||
}
|
||||
|
||||
Program.RunAsAdmin();
|
||||
Program.RunAsAdmin("gpurestart");
|
||||
|
||||
Logger.WriteLine("Trying to restart GPU");
|
||||
var nvControl = (NvidiaGpuControl)HardwareControl.GpuControl;
|
||||
return nvControl.RestartGPU();
|
||||
if (!Program.IsUserAdministrator()) return;
|
||||
|
||||
Logger.WriteLine("Trying to restart dGPU");
|
||||
|
||||
Task.Run(async () =>
|
||||
{
|
||||
Program.settingsForm.BeginInvoke(delegate
|
||||
{
|
||||
labelTipGPU.Text = "Restarting GPU ...";
|
||||
ButtonEnabled(buttonOptimized, false);
|
||||
ButtonEnabled(buttonEco, false);
|
||||
ButtonEnabled(buttonStandard, false);
|
||||
ButtonEnabled(buttonUltimate, false);
|
||||
});
|
||||
|
||||
var nvControl = (NvidiaGpuControl)HardwareControl.GpuControl;
|
||||
bool status = nvControl.RestartGPU();
|
||||
|
||||
Program.settingsForm.BeginInvoke(delegate
|
||||
{
|
||||
labelTipGPU.Text = status ? "GPU Restarted, you can try Eco mode again" : "Failed to restart GPU";
|
||||
InitGPUMode();
|
||||
});
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
@@ -1490,13 +1514,14 @@ namespace GHelper
|
||||
string[] tokill = { "EADesktop", "RadeonSoftware" };
|
||||
foreach (string kill in tokill)
|
||||
foreach (var process in Process.GetProcessesByName(kill)) process.Kill();
|
||||
}
|
||||
}
|
||||
|
||||
//if (eco == 1) status = 0; else
|
||||
status = Program.wmi.SetGPUEco(eco);
|
||||
|
||||
if (status == 0 && eco == 1 && hardWay)
|
||||
{
|
||||
if (RestartGPU()) Program.wmi.SetGPUEco(1);
|
||||
RestartGPU();
|
||||
}
|
||||
|
||||
await Task.Delay(TimeSpan.FromMilliseconds(500));
|
||||
@@ -1508,7 +1533,7 @@ namespace GHelper
|
||||
|
||||
if (eco == 0)
|
||||
{
|
||||
await Task.Delay(TimeSpan.FromMilliseconds(1000));
|
||||
await Task.Delay(TimeSpan.FromMilliseconds(3000));
|
||||
HardwareControl.RecreateGpuControl();
|
||||
SetGPUClocks(false);
|
||||
}
|
||||
@@ -1669,6 +1694,8 @@ namespace GHelper
|
||||
sliderBattery.Value = limit;
|
||||
|
||||
Program.wmi.DeviceSet(ASUSWmi.BatteryLimit, limit, "BatteryLimit");
|
||||
OptimizationService.SetChargeLimit(limit);
|
||||
|
||||
Program.config.setConfig("charge_limit", limit);
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user