Skip to content

Conversation

@MadoScientist97
Copy link
Contributor

Hi @dankrusi I know this is like after months, but there were some race issues with how the window focus switching was maintained and handled, I have some potential fixes for that, should help improve stability.

@dankrusi dankrusi requested a review from Copilot August 18, 2025 07:29
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR addresses data race issues in the window focus switching mechanism to prevent desktop focus from landing on unintended windows. The changes improve thread safety and reduce monitoring overhead by replacing the continuous focus monitoring thread with targeted focus tracking during desktop switches.

  • Removes continuous background thread that monitored focused windows every 200ms
  • Replaces thread-unsafe Dictionary with ConcurrentDictionary for storing window focus state
  • Adds explicit focus storage calls before desktop switching operations

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
Source/Forms/AppForm.cs Removes the call to start continuous focus monitoring thread
Source/App/App.cs Implements thread-safe focus tracking with ConcurrentDictionary and targeted focus storage

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
You can also share your feedback on Copilot code review for a chance to win a $100 gift card. Take the survey.

VDSwitchedSafe();
} else {
//storeLastWinFocused();
this.AppForm.BeginInvoke((Action)(() => {
Copy link

Copilot AI Aug 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BeginInvoke is asynchronous and may cause race conditions. The VDSwitchedSafe() call inside the invoke block could execute after subsequent desktop switches, leading to incorrect state updates. Consider using Invoke for synchronous execution or adding proper synchronization.

Suggested change
this.AppForm.BeginInvoke((Action)(() => {
this.AppForm.Invoke((Action)(() => {

Copilot uses AI. Check for mistakes.
if(Util.OS.IsWindow(lastWindowHandle)) {
IntPtr lastWindowHandle;
if (VDDToLastFocusedWin.TryGetValue(displayNumber, out lastWindowHandle)) {
System.Threading.Thread.Sleep(50);
Copy link

Copilot AI Aug 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hard-coded sleep duration (50ms) is a magic number that makes the timing behavior unclear. Consider defining this as a named constant or making it configurable to improve maintainability.

Copilot uses AI. Check for mistakes.
@dankrusi
Copy link
Owner

@MadoScientist97 ignore the bot review - I just wanted to test this feature.

Thanks for the PR - will test it this week.

Best,

-dan

@MadoScientist97
Copy link
Contributor Author

I do seem to be running into an issue in my work laptop where the focus gets lost, when switching back but it doesn't jump around to random workspaces anymore. Might work on this in the weekend

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants