From ed0bfff71c8b5c001df3f2344b48e07b54f15646 Mon Sep 17 00:00:00 2001 From: Lesueur Benjamin Date: Fri, 3 Jan 2025 08:19:12 +0100 Subject: [PATCH] Fixed an issue where reading steam settings changes could cause a crash --- HandheldCompanion/Platforms/Steam.cs | 29 ++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/HandheldCompanion/Platforms/Steam.cs b/HandheldCompanion/Platforms/Steam.cs index 2a8bef03e..1f84c5779 100644 --- a/HandheldCompanion/Platforms/Steam.cs +++ b/HandheldCompanion/Platforms/Steam.cs @@ -9,6 +9,7 @@ using System.Linq; using System.Text.RegularExpressions; using System.Threading.Tasks; +using System.Timers; namespace HandheldCompanion.Platforms; @@ -18,6 +19,7 @@ public class Steam : IPlatform private readonly RegistryWatcher SteamActiveUserWatcher = new(WatchedRegistry.CurrentUser, @"SOFTWARE\\Valve\\Steam\\ActiveProcess\\", "ActiveUser"); private FileSystemWatcher SteamActiveUserFileWatcher = new(); + private Timer debounceTimer; private readonly int SteamAppsId = 413080; @@ -60,6 +62,11 @@ public Steam() // check drivers IsControllerDriverInstalled = HasXboxDriversInstalled(); + + // Initialize debounce timer + debounceTimer = new Timer(1000); // 1 second delay + debounceTimer.AutoReset = false; // Trigger only once per activation + debounceTimer.Elapsed += DebounceTimer_Elapsed; } public override bool Start() @@ -91,13 +98,24 @@ private void ActiveUserWatcher_RegistryChanged(object? sender, RegistryChangedEv IncludeSubdirectories = true, }; - SteamActiveUserFileWatcher.Changed += (sender, e) => ActiveFileWatch_Changed(); + SteamActiveUserFileWatcher.Changed += FileWatcher_Changed; ActiveFileWatch_Changed(); } - private async void ActiveFileWatch_Changed() + private void FileWatcher_Changed(object sender, FileSystemEventArgs e) + { + // Restart the debounce timer whenever a change is detected + debounceTimer.Stop(); + debounceTimer.Start(); + } + + private void DebounceTimer_Elapsed(object? sender, ElapsedEventArgs e) + { + ActiveFileWatch_Changed(); + } + + private void ActiveFileWatch_Changed() { - await Task.Delay(1000).ConfigureAwait(false); // Avoid blocking the synchronization context int SteamInput = GetUseSteamControllerConfigValue(); base.SettingsValueChaned("UseSteamControllerConfig", SteamInput); } @@ -182,9 +200,12 @@ public override bool Stop(bool kill = false) { SteamActiveUserWatcher.StopWatching(); - SteamActiveUserFileWatcher.Changed -= (sender, e) => ActiveFileWatch_Changed(); + SteamActiveUserFileWatcher.Changed -= FileWatcher_Changed; SteamActiveUserFileWatcher.Dispose(); + debounceTimer.Stop(); + debounceTimer.Dispose(); + return base.Stop(); }