Improved GPU restart

This commit is contained in:
Serge
2023-05-12 15:51:57 +02:00
parent 7484253007
commit 51cd700e25
7 changed files with 108 additions and 23 deletions

View File

@@ -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'">

View File

@@ -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());

View 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);
}
}
}
}

View File

@@ -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)

View File

@@ -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 {

View File

@@ -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>

View File

@@ -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);
}