Codeusa / Borderless-Gaming

Play your favorite games in a borderless window; no more time consuming alt-tabs.
GNU General Public License v2.0
5.5k stars 476 forks source link

Application freezes after returning from Lock screen #680

Open usagirei opened 3 months ago

usagirei commented 3 months ago

Requirements

Summary

Borderless Gaming will freeze when you lock the computer screen, possibly in other scenarios that fit the same criteria.

Steps to reproduce

  1. Run Application
  2. Go to lock screen windows (Win+L), and wait a few moments (10~15 seconds should do it, if not lock it again and retry)
  3. Come back to frozen application

Technical details

  1. GetForegroundWindow may return NULL under certain conditions, one of which, is when the lock screen is active.

  2. Then GetWindowThreadProcessId will return NULL too (Which happens to be a valid PID, the System Idle process) ,

  3. Then when it tries to create a ProcessDetails class, for an invalid process, which tries to get the window title for an invalid window. https://github.com/Codeusa/Borderless-Gaming/blob/3cc4dc6bd580b263287be45981f1e36036daf4eb/BorderlessGaming.Logic/Windows/ForegroundManager.cs#L33-L35

  4. It will then hang indefinitely trying to do so here: https://github.com/Codeusa/Borderless-Gaming/blob/3cc4dc6bd580b263287be45981f1e36036daf4eb/BorderlessGaming.Logic/Models/ProcessDetails.cs#L56-L59


Checks for NULL/Invalid handles need to be introduced:

An early return here: https://github.com/Codeusa/Borderless-Gaming/blob/3cc4dc6bd580b263287be45981f1e36036daf4eb/BorderlessGaming.Logic/Windows/ForegroundManager.cs#L33

var handle = Native.GetForegroundWindow(); 
if(handle == IntPtr.Zero) 
    return;

And maybe a short circuit here: https://github.com/Codeusa/Borderless-Gaming/blob/3cc4dc6bd580b263287be45981f1e36036daf4eb/BorderlessGaming.Logic/Models/ProcessDetails.cs#L56-L59

if ((_windowHandle != IntPtr.Zero) && !Native.IsWindow(_windowHandle))
{
    _windowHandle = Native.GetMainWindowForProcess(Proc).GetAwaiter().GetResult();
}

Or ensure a ProcessDetails class can't be created for an invalid process/window

version

9.5.6.1328 - git 3cc4dc6