From 83b8938e1adb261f0eeeaa7bacb4c94651aa94b9 Mon Sep 17 00:00:00 2001 From: Terry MacDonald Date: Sat, 2 Jul 2022 15:08:54 +1200 Subject: [PATCH] Reduces config scanning delay as much as possible WinLibrary currently waits 5 seconds if it can't read the taskbar registry, and then it tries again. This is because based on my testing, if a screen layout changes, windows takes up to 20 seconds to update registry to record this fact. We have to wait until windows has finished 4 times before we are sure to have passed the 20 second window. This is likely the delay you have mentioned. I *think* that I can slightly speed this up. We only MUST to do this delay when we are recording the config (i.e. creating a new display profile), and other times it's kind of a nice to have. So I've attempted to speed this up using a 'fastScan' option for the WinLibrary GetActiveConfig function. This will enable it to only query once for the general scans of the active config, and if there is a problem getting the data it will just accept that fact and will still return quickly. But it will still take up to 20 seconds when creating a new display profile as it is REALLY important we get that data correctly. Fixes #129 --- DisplayMagician/Properties/AssemblyInfo.cs | 4 +- DisplayMagician/UIForms/DisplayProfileForm.cs | 2 +- DisplayMagicianShared/ProfileItem.cs | 8 +-- DisplayMagicianShared/ProfileRepository.cs | 4 +- DisplayMagicianShared/Windows/WinLibrary.cs | 51 +++++++++++-------- 5 files changed, 38 insertions(+), 31 deletions(-) diff --git a/DisplayMagician/Properties/AssemblyInfo.cs b/DisplayMagician/Properties/AssemblyInfo.cs index 3864e53..f58418a 100644 --- a/DisplayMagician/Properties/AssemblyInfo.cs +++ b/DisplayMagician/Properties/AssemblyInfo.cs @@ -26,8 +26,8 @@ using System.Resources; [assembly: Guid("e4ceaf5e-ad01-4695-b179-31168eb74c48")] // Version information -[assembly: AssemblyVersion("2.4.1.1")] -[assembly: AssemblyFileVersion("2.4.1.1")] +[assembly: AssemblyVersion("2.4.1.2")] +[assembly: AssemblyFileVersion("2.4.1.2")] [assembly: NeutralResourcesLanguageAttribute( "en" )] [assembly: CLSCompliant(true)] diff --git a/DisplayMagician/UIForms/DisplayProfileForm.cs b/DisplayMagician/UIForms/DisplayProfileForm.cs index addfdad..0f7bef6 100644 --- a/DisplayMagician/UIForms/DisplayProfileForm.cs +++ b/DisplayMagician/UIForms/DisplayProfileForm.cs @@ -548,7 +548,7 @@ namespace DisplayMagician.UIForms // Refresh the profiles to see whats valid ProfileRepository.IsPossibleRefresh(); // Reload the profiles in case we swapped to another program to change it - ProfileRepository.UpdateActiveProfile(); + ProfileRepository.UpdateActiveProfile(false); // Change to the current selected Profile ChangeSelectedProfile(ProfileRepository.GetActiveProfile()); // Refresh the Profile UI diff --git a/DisplayMagicianShared/ProfileItem.cs b/DisplayMagicianShared/ProfileItem.cs index c3fc2cb..608a904 100644 --- a/DisplayMagicianShared/ProfileItem.cs +++ b/DisplayMagicianShared/ProfileItem.cs @@ -478,7 +478,7 @@ namespace DisplayMagicianShared } - public bool CreateProfileFromCurrentDisplaySettings() + public bool CreateProfileFromCurrentDisplaySettings(bool fastScan = true) { // Calling the 3 different libraries automatically gets the different configs from each of the 3 video libraries. // If the video library isn't in use then it also fills in the defaults so that the JSON file can save properly @@ -495,16 +495,16 @@ namespace DisplayMagicianShared if (VideoMode == VIDEO_MODE.NVIDIA && nvidiaLibrary.IsInstalled) { nvidiaLibrary.UpdateActiveConfig(); - winLibrary.UpdateActiveConfig(); + winLibrary.UpdateActiveConfig(fastScan); } else if (VideoMode == VIDEO_MODE.AMD && amdLibrary.IsInstalled) { amdLibrary.UpdateActiveConfig(); - winLibrary.UpdateActiveConfig(); + winLibrary.UpdateActiveConfig(fastScan); } else { - winLibrary.UpdateActiveConfig(); + winLibrary.UpdateActiveConfig(fastScan); } // Grab the profile data from the current stored config (that we just updated) diff --git a/DisplayMagicianShared/ProfileRepository.cs b/DisplayMagicianShared/ProfileRepository.cs index 62f90f1..16e2c99 100644 --- a/DisplayMagicianShared/ProfileRepository.cs +++ b/DisplayMagicianShared/ProfileRepository.cs @@ -696,7 +696,7 @@ namespace DisplayMagicianShared } } - public static void UpdateActiveProfile() + public static void UpdateActiveProfile(bool fastScan = true) { SharedLogger.logger.Debug($"ProfileRepository/UpdateActiveProfile: Updating the profile currently active (in use now)."); @@ -751,7 +751,7 @@ namespace DisplayMagicianShared //ShellHelper.TellShellToWriteSettings(); //WinLibrary.RefreshTaskBars(); - profile.CreateProfileFromCurrentDisplaySettings(); + profile.CreateProfileFromCurrentDisplaySettings(fastScan); if (_profilesLoaded && _allProfiles.Count > 0) { diff --git a/DisplayMagicianShared/Windows/WinLibrary.cs b/DisplayMagicianShared/Windows/WinLibrary.cs index 40feb1d..043c205 100644 --- a/DisplayMagicianShared/Windows/WinLibrary.cs +++ b/DisplayMagicianShared/Windows/WinLibrary.cs @@ -473,12 +473,12 @@ namespace DisplayMagicianShared.Windows } - public bool UpdateActiveConfig() + public bool UpdateActiveConfig(bool fastScan = true) { SharedLogger.logger.Trace($"WinLibrary/UpdateActiveConfig: Updating the currently active config"); try { - _activeDisplayConfig = GetActiveConfig(); + _activeDisplayConfig = GetActiveConfig(fastScan); _allConnectedDisplayIdentifiers = GetAllConnectedDisplayIdentifiers(); } catch (Exception ex) @@ -490,14 +490,14 @@ namespace DisplayMagicianShared.Windows return true; } - public WINDOWS_DISPLAY_CONFIG GetActiveConfig() + public WINDOWS_DISPLAY_CONFIG GetActiveConfig(bool fastScan = true) { SharedLogger.logger.Trace($"WinLibrary/GetActiveConfig: Getting the currently active config"); // We'll leave virtual refresh rate aware until we can reliably detect Windows 11 versions. - return GetWindowsDisplayConfig(QDC.QDC_ONLY_ACTIVE_PATHS); + return GetWindowsDisplayConfig(QDC.QDC_ONLY_ACTIVE_PATHS, fastScan); } - private WINDOWS_DISPLAY_CONFIG GetWindowsDisplayConfig(QDC selector = QDC.QDC_ONLY_ACTIVE_PATHS) + private WINDOWS_DISPLAY_CONFIG GetWindowsDisplayConfig(QDC selector = QDC.QDC_ONLY_ACTIVE_PATHS, bool fastScan = true) { // Prepare the empty windows display config @@ -909,25 +909,32 @@ namespace DisplayMagicianShared.Windows // Check whether Windows has actually added the registry keys that outline the taskbar position if (retryNeeded) { - // We wait until the reg key is populated - for (int count = 1; count <= 4; count++) + if (fastScan) { - SharedLogger.logger.Trace($"WinLibrary/GetWindowsDisplayConfig: We were unable to get all the Windows Taskbar Layouts! So we need to try again. Attempt {count} of 4."); - - // Wait 5 seconds - System.Threading.Thread.Sleep(5000); - // then try again - retryNeeded = false; - taskBarStuckRectangles = TaskBarLayout.GetAllCurrentTaskBarLayouts(windowsDisplayConfig.DisplaySources, out retryNeeded); - - if (!retryNeeded) - { - SharedLogger.logger.Trace($"WinLibrary/GetWindowsDisplayConfig: We successfully got the Windows Taskbar Layouts on attempt {count}! So we can stop trying to get them"); - break; - } - + SharedLogger.logger.Trace($"WinLibrary/GetWindowsDisplayConfig: Skipping retrying Windows Taskbar Layouts and just accepting the first locations"); } - } + else + { + // We wait until the reg key is populated + for (int count = 1; count <= 4; count++) + { + SharedLogger.logger.Trace($"WinLibrary/GetWindowsDisplayConfig: We were unable to get all the Windows Taskbar Layouts! So we need to try again. Attempt {count} of 4."); + + // Wait 5 seconds + System.Threading.Thread.Sleep(5000); + // then try again + retryNeeded = false; + taskBarStuckRectangles = TaskBarLayout.GetAllCurrentTaskBarLayouts(windowsDisplayConfig.DisplaySources, out retryNeeded); + + if (!retryNeeded) + { + SharedLogger.logger.Trace($"WinLibrary/GetWindowsDisplayConfig: We successfully got the Windows Taskbar Layouts on attempt {count}! So we can stop trying to get them"); + break; + } + + } + } + } // Now we try to get the taskbar settings too SharedLogger.logger.Trace($"WinLibrary/GetWindowsDisplayConfig: Attempting to get the Windows Taskbar Settings.");