From 6459ba135ceced242be3fb3a78e983e03dd33124 Mon Sep 17 00:00:00 2001 From: Ace_Radom Date: Tue, 26 Nov 2024 19:20:08 +0100 Subject: [PATCH 1/2] check GPUController is started before getting GPU info --- LenovoLegionToolkit.Lib/Controllers/GPUController.cs | 3 +++ .../Controllers/Sensors/AbstractSensorsController.cs | 3 +++ 2 files changed, 6 insertions(+) diff --git a/LenovoLegionToolkit.Lib/Controllers/GPUController.cs b/LenovoLegionToolkit.Lib/Controllers/GPUController.cs index 002b1f753e..87f4172aff 100644 --- a/LenovoLegionToolkit.Lib/Controllers/GPUController.cs +++ b/LenovoLegionToolkit.Lib/Controllers/GPUController.cs @@ -25,6 +25,7 @@ public class GPUController private string? _performanceState; public event EventHandler? Refreshed; + public bool Started { get; private set; } = false; public bool IsSupported() { @@ -72,6 +73,7 @@ public async Task StartAsync(int delay = 1_000, int interval = 5_000) _refreshCancellationTokenSource = new CancellationTokenSource(); var token = _refreshCancellationTokenSource.Token; _refreshTask = Task.Run(() => RefreshLoopAsync(delay, interval, token), token); + Started = true; } public async Task StopAsync(bool waitForFinish = false) @@ -102,6 +104,7 @@ public async Task StopAsync(bool waitForFinish = false) _refreshCancellationTokenSource = null; _refreshTask = null; + Started = false; if (Log.Instance.IsTraceEnabled) Log.Instance.Trace($"Stopped"); diff --git a/LenovoLegionToolkit.Lib/Controllers/Sensors/AbstractSensorsController.cs b/LenovoLegionToolkit.Lib/Controllers/Sensors/AbstractSensorsController.cs index 6f5d2f2e65..657ea8a7a1 100644 --- a/LenovoLegionToolkit.Lib/Controllers/Sensors/AbstractSensorsController.cs +++ b/LenovoLegionToolkit.Lib/Controllers/Sensors/AbstractSensorsController.cs @@ -171,6 +171,9 @@ private static unsafe int GetCpuBaseClock() private async Task GetGPUInfoAsync() { + if (gpuController.IsSupported() && !gpuController.Started) + await gpuController.StartAsync(); + if (await gpuController.GetLastKnownStateAsync().ConfigureAwait(false) is GPUState.PoweredOff or GPUState.Unknown) return GPUInfo.Empty; From 5ab1468152ff8f1253b1d9e7d3d8ad704e34d149 Mon Sep 17 00:00:00 2001 From: Ace_Radom Date: Wed, 27 Nov 2024 14:48:07 +0100 Subject: [PATCH 2/2] code cleanup --- LenovoLegionToolkit.Lib/Controllers/GPUController.cs | 10 +++++----- .../Controllers/Sensors/AbstractSensorsController.cs | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/LenovoLegionToolkit.Lib/Controllers/GPUController.cs b/LenovoLegionToolkit.Lib/Controllers/GPUController.cs index 87f4172aff..1efc51ee44 100644 --- a/LenovoLegionToolkit.Lib/Controllers/GPUController.cs +++ b/LenovoLegionToolkit.Lib/Controllers/GPUController.cs @@ -25,7 +25,7 @@ public class GPUController private string? _performanceState; public event EventHandler? Refreshed; - public bool Started { get; private set; } = false; + public bool IsStarted { get => _refreshTask != null; } public bool IsSupported() { @@ -63,9 +63,10 @@ public async Task RefreshNowAsync() } } - public async Task StartAsync(int delay = 1_000, int interval = 5_000) + public Task StartAsync(int delay = 1_000, int interval = 5_000) { - await StopAsync(true).ConfigureAwait(false); + if (IsStarted) + return Task.CompletedTask; if (Log.Instance.IsTraceEnabled) Log.Instance.Trace($"Starting... [delay={delay}, interval={interval}]"); @@ -73,7 +74,7 @@ public async Task StartAsync(int delay = 1_000, int interval = 5_000) _refreshCancellationTokenSource = new CancellationTokenSource(); var token = _refreshCancellationTokenSource.Token; _refreshTask = Task.Run(() => RefreshLoopAsync(delay, interval, token), token); - Started = true; + return Task.CompletedTask; } public async Task StopAsync(bool waitForFinish = false) @@ -104,7 +105,6 @@ public async Task StopAsync(bool waitForFinish = false) _refreshCancellationTokenSource = null; _refreshTask = null; - Started = false; if (Log.Instance.IsTraceEnabled) Log.Instance.Trace($"Stopped"); diff --git a/LenovoLegionToolkit.Lib/Controllers/Sensors/AbstractSensorsController.cs b/LenovoLegionToolkit.Lib/Controllers/Sensors/AbstractSensorsController.cs index 657ea8a7a1..80772c114d 100644 --- a/LenovoLegionToolkit.Lib/Controllers/Sensors/AbstractSensorsController.cs +++ b/LenovoLegionToolkit.Lib/Controllers/Sensors/AbstractSensorsController.cs @@ -171,7 +171,7 @@ private static unsafe int GetCpuBaseClock() private async Task GetGPUInfoAsync() { - if (gpuController.IsSupported() && !gpuController.Started) + if (gpuController.IsSupported()) await gpuController.StartAsync(); if (await gpuController.GetLastKnownStateAsync().ConfigureAwait(false) is GPUState.PoweredOff or GPUState.Unknown)