From 508f9658317c68792fd8365151424f5c81dd967f Mon Sep 17 00:00:00 2001 From: "Imani, Sahil" Date: Sat, 16 Aug 2025 21:31:24 +0530 Subject: [PATCH 1/2] added some guards and fixed some race issues --- Source/App/App.cs | 35 ++++++++++------------------------- Source/Forms/AppForm.cs | 1 - 2 files changed, 10 insertions(+), 26 deletions(-) diff --git a/Source/App/App.cs b/Source/App/App.cs index 9f2b4af..824ee16 100644 --- a/Source/App/App.cs +++ b/Source/App/App.cs @@ -97,7 +97,6 @@ public App() { } #endregion - #region Virtual Desktop Methods public void LoadVDAPI() { @@ -171,12 +170,11 @@ private void _MonitorVDSwitch() { try { var newVDDisplayNumber = this.GetVDDisplayNumber(false); if(newVDDisplayNumber != this.CurrentVDDisplayNumber) { - this.CurrentVDDisplayName = this.GetVDDisplayName(false); - this.CurrentVDDisplayNumber = newVDDisplayNumber; - //Util.Logging.WriteLine("Switched to " + this.CurrentVDDisplayNumber); - VDSwitchedSafe(); - } else { - //storeLastWinFocused(); + this.AppForm.BeginInvoke((Action)(() => { + this.CurrentVDDisplayName = this.GetVDDisplayName(false); + this.CurrentVDDisplayNumber = newVDDisplayNumber; + VDSwitchedSafe(); + })); } System.Threading.Thread.Sleep(100); } catch(Exception e) { @@ -236,6 +234,7 @@ public void VDSwitchedSafe() { public void SwitchDesktopBackward() { // We try the virtual desktop implementation API, but fallback to shortcut keys if it fails... try { + _storeLastWinFocused(); VDAPI.SwitchBackward(); } catch(Exception e) { Util.Logging.WriteLine("App: Error: SwitchDesktopBackward (VDAPI.SwitchBackward()): " + e.Message); @@ -246,6 +245,7 @@ public void SwitchDesktopBackward() { public void SwitchDesktopForward() { // We try the virtual desktop implementation API, but fallback to shortcut keys if it fails... try { + _storeLastWinFocused(); VDAPI.SwitchForward(); } catch(Exception e) { Util.Logging.WriteLine("App: Error: SwitchDesktopForward (VDAPI.SwitchForward()): " + e.Message); @@ -309,23 +309,6 @@ private void _MonitorFGWindowName() { } - public void MonitorFocusedWindow() { - var thread = new Thread(new ThreadStart(_monitorFocusedWindow)); - thread.Start(); - } - - private void _monitorFocusedWindow() { - while(true) { - try { - _storeLastWinFocused(); - System.Threading.Thread.Sleep(200); - } catch(Exception e) { - Util.Logging.WriteLine("App: Error: _monitorFocusedWindow: " + e.Message); - System.Threading.Thread.Sleep(1000); - } - } - } - private void _storeLastWinFocused() { IntPtr hWnd = Util.OS.GetForegroundWindow(); if(hWnd != IntPtr.Zero) { @@ -346,7 +329,9 @@ private void _restorePrevWinFocus() { var displayNumber = (int)this.GetVDDisplayNumber(false); if(VDDToLastFocusedWin.ContainsKey(displayNumber)) { IntPtr lastWindowHandle = VDDToLastFocusedWin[displayNumber]; - if(Util.OS.IsWindow(lastWindowHandle)) { + // Add a small delay to give the desktop time to load + System.Threading.Thread.Sleep(50); + if (Util.OS.IsWindow(lastWindowHandle)) { Util.OS.SetForegroundWindow(lastWindowHandle); //Console.WriteLine("restore: "+ displayNumber + " "+ lastWindowHandle); } diff --git a/Source/Forms/AppForm.cs b/Source/Forms/AppForm.cs index 58d2795..f3867f5 100644 --- a/Source/Forms/AppForm.cs +++ b/Source/Forms/AppForm.cs @@ -38,7 +38,6 @@ private void AppForm_Load(object sender, EventArgs e) { App.Instance.MonitorSystemThemeSwitch(); App.Instance.MonitorVDisplayCount(); App.Instance.MonitorFGWindowName(); - App.Instance.MonitorFocusedWindow(); App.Instance.UIUpdate(); } From c35b879176966829976c39c5891a8b6fea016f1f Mon Sep 17 00:00:00 2001 From: "Imani, Sahil" Date: Sat, 16 Aug 2025 22:16:29 +0530 Subject: [PATCH 2/2] updated the dict type --- Source/App/App.cs | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/Source/App/App.cs b/Source/App/App.cs index 824ee16..2bc3e46 100644 --- a/Source/App/App.cs +++ b/Source/App/App.cs @@ -13,6 +13,7 @@ using System.Globalization; using System.Linq; using System.Security.Policy; +using System.Collections.Concurrent; namespace WindowsVirtualDesktopHelper { @@ -49,7 +50,7 @@ internal class HotKeyAction { private KeyboardHook KeyboardHooksJumpToDesktop = null; private KeyboardHook _keyboardHooks = null; private List _keyboardHooksHotKeysAndActions = new List(); // the registered hotkey actions - private Dictionary VDDToLastFocusedWin = new Dictionary(); + private ConcurrentDictionary VDDToLastFocusedWin = new ConcurrentDictionary(); public IntPtr LastForegroundhWnd = IntPtr.Zero; //TODO: this should be private public List FGWindowHistory = new List(); //TODO: this should be private // needed to detect if Task View was open private List _desktopNumberHistory = new List(); // stores a list of most recent desktop numbers used @@ -316,24 +317,18 @@ private void _storeLastWinFocused() { var fgWindowType = Util.OS.GetHandleWndType(hWnd); if(fgWindowType == "Shell_TrayWnd") return; // we ignore the icon tray, since this takes the focus away when we click the prev/next arrows var displayNumber = (int)this.GetVDDisplayNumber(false); - if(VDDToLastFocusedWin.ContainsKey(displayNumber)) { - VDDToLastFocusedWin[displayNumber] = hWnd; - } else { - VDDToLastFocusedWin.Add(displayNumber, hWnd); - } + VDDToLastFocusedWin.AddOrUpdate(displayNumber, hWnd, (key, existingValue) => hWnd); //Console.WriteLine($"store: display {displayNumber} hwnd {hWnd} ({fgWindowType})"); } } private void _restorePrevWinFocus() { var displayNumber = (int)this.GetVDDisplayNumber(false); - if(VDDToLastFocusedWin.ContainsKey(displayNumber)) { - IntPtr lastWindowHandle = VDDToLastFocusedWin[displayNumber]; - // Add a small delay to give the desktop time to load + IntPtr lastWindowHandle; + if (VDDToLastFocusedWin.TryGetValue(displayNumber, out lastWindowHandle)) { System.Threading.Thread.Sleep(50); if (Util.OS.IsWindow(lastWindowHandle)) { Util.OS.SetForegroundWindow(lastWindowHandle); - //Console.WriteLine("restore: "+ displayNumber + " "+ lastWindowHandle); } } }