Skip to content

Commit

Permalink
Misc fixes
Browse files Browse the repository at this point in the history
Get rid of weird .Wait and make it async, fix window titles having <error>, fix deadlock.
  • Loading branch information
Andrew Sampson committed Mar 31, 2018
1 parent b54c383 commit abbed21
Show file tree
Hide file tree
Showing 10 changed files with 1,369 additions and 265 deletions.
28 changes: 14 additions & 14 deletions BorderlessGaming.Logic/Core/ProcessWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ private async void Watch()
{
while (!_watcherToken.IsCancellationRequested)
{
UpdateProcesses();
await UpdateProcesses();
if (AutoHandleFavorites)
{
// check favorites against the cache
Expand All @@ -73,7 +73,7 @@ private async void Watch()
{
favProcess.IsRunning = true;
favProcess.RunningId = pd.Proc.Id;
RemoveBorder(pd, favProcess);
await RemoveBorder(pd, favProcess);
}
}
}
Expand All @@ -90,56 +90,56 @@ private async void Watch()
/// <summary>
/// remove the menu, resize the window, remove border, and maximize
/// </summary>
public void RemoveBorder(ProcessDetails pd, Favorite favDetails = null, bool overrideTimeout = false)
public async Task RemoveBorder(ProcessDetails pd, Favorite favDetails = null, bool overrideTimeout = false)
{
if (favDetails != null && favDetails.DelayBorderless && overrideTimeout == false)
{
//Wait 10 seconds before removing the border.
var task = new Task(() => RemoveBorder(pd, favDetails, true));
var task = new Task(async () => await RemoveBorder(pd, favDetails, true));
task.Wait(TimeSpan.FromSeconds(10));
}

// If a Favorite screen exists, use the Rect from that, instead
if (favDetails?.FavScreen != null)
{
RemoveBorder_ToSpecificRect(pd, PRectangle.ToRectangle(favDetails.FavScreen), favDetails,
await RemoveBorder_ToSpecificRect(pd, PRectangle.ToRectangle(favDetails.FavScreen), favDetails,
overrideTimeout);
return;
}
Manipulation.MakeWindowBorderless(pd, _form, pd.WindowHandle, new Rectangle(), favDetails ?? Favorite.FromWindow(pd));
await Manipulation.MakeWindowBorderless(pd, _form, pd.WindowHandle, new Rectangle(), favDetails ?? Favorite.FromWindow(pd));
}

/// <summary>
/// remove the menu, resize the window, remove border, and maximize
/// </summary>
public void RemoveBorder_ToSpecificScreen(IntPtr hWnd, Screen screen, Favorite favDetails = null,
public async Task RemoveBorder_ToSpecificScreen(IntPtr hWnd, Screen screen, Favorite favDetails = null,
bool overrideTimeout = false)
{
if (favDetails != null && favDetails.DelayBorderless && overrideTimeout == false)
{
//Wait 10 seconds before removing the border.
var task = new Task(() => RemoveBorder_ToSpecificScreen(hWnd, screen, favDetails, true));
var task = new Task(async () => await RemoveBorder_ToSpecificScreen(hWnd, screen, favDetails, true));
task.Wait(TimeSpan.FromSeconds(10));
}

var pd = FromHandle(hWnd);
Manipulation.MakeWindowBorderless(pd, _form, hWnd, screen.Bounds, favDetails ?? Favorite.FromWindow(pd));
await Manipulation.MakeWindowBorderless(pd, _form, hWnd, screen.Bounds, favDetails ?? Favorite.FromWindow(pd));
}

/// <summary>
/// remove the menu, resize the window, remove border, and maximize
/// </summary>
public void RemoveBorder_ToSpecificRect(IntPtr hWnd, Rectangle targetFrame, Favorite favDetails = null,
public async Task RemoveBorder_ToSpecificRect(IntPtr hWnd, Rectangle targetFrame, Favorite favDetails = null,
bool overrideTimeout = false)
{
if (favDetails != null && favDetails.DelayBorderless && overrideTimeout == false)
{
//Wait 10 seconds before removing the border.
var task = new Task(() => RemoveBorder_ToSpecificRect(hWnd, targetFrame, favDetails, true));
var task = new Task(async () => await RemoveBorder_ToSpecificRect(hWnd, targetFrame, favDetails, true));
task.Wait(TimeSpan.FromSeconds(10));
}
var pd = FromHandle(hWnd);
Manipulation.MakeWindowBorderless(pd, _form, hWnd, targetFrame, favDetails ?? Favorite.FromWindow(pd));
await Manipulation.MakeWindowBorderless(pd, _form, hWnd, targetFrame, favDetails ?? Favorite.FromWindow(pd));
}

/// <summary>
Expand Down Expand Up @@ -167,7 +167,7 @@ private void HandlePrunedProcess(ProcessDetails pd)
}
}

private void UpdateProcesses()
private async Task UpdateProcesses()
{
if (!AutoHandleFavorites)
{
Expand All @@ -189,7 +189,7 @@ private void UpdateProcesses()

if (!process.NoAccess)
{
TaskUtilities.StartTaskAndWait(() => { currentTitle = Native.GetWindowTitle(process.WindowHandle); },
await TaskUtilities.StartTaskAndWait(() => { currentTitle = Native.GetWindowTitle(process.WindowHandle); },
Config.Instance.AppSettings.SlowWindowDetection ? 10 : 2); shouldBePruned = process.WindowTitle != currentTitle;
}
}
Expand Down
39 changes: 20 additions & 19 deletions BorderlessGaming.Logic/Models/ProcessDetails.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Threading.Tasks;
using BorderlessGaming.Logic.System.Utilities;
using BorderlessGaming.Logic.Windows;

Expand All @@ -28,12 +29,18 @@ public ProcessDetails(Process p, IntPtr hWnd)
Proc = p;

WindowHandle = hWnd;
WindowTitle = "<error>";
TaskUtilities.StartTaskAndWait(() => { WindowTitle = Native.GetWindowTitle(WindowHandle); },
Config.Instance.AppSettings.SlowWindowDetection ? 10 : 2);
WindowTitle = Native.GetWindowTitle(WindowHandle);
// GetWindowTitle();

//this.WindowClass = WindowsAPI.Native.GetWindowClassName(this.WindowHandle); // note: this isn't used, currently
}

private async void GetWindowTitle()
{
await TaskUtilities.StartTaskAndWait(() => { WindowTitle = Native.GetWindowTitle(WindowHandle); },
Config.Instance.AppSettings.SlowWindowDetection ? 10 : 2);
}

// Automatically detects changes to the window handle
public IntPtr WindowHandle
{
Expand All @@ -48,7 +55,7 @@ public IntPtr WindowHandle

if (!Native.IsWindow(_windowHandle))
{
_windowHandle = Native.GetMainWindowForProcess(Proc);
_windowHandle = Native.GetMainWindowForProcess(Proc).GetAwaiter().GetResult();
}
}
catch
Expand Down Expand Up @@ -112,22 +119,16 @@ public string BinaryName
private string BinaryNameForComparison => BinaryName.Trim().ToLower().Replace(" ", "").Replace("_", "");

// Detect whether or not the window needs border changes
public bool WindowHasTargetableStyles
public async Task<bool> WindowHasTargetableStyles()
{
get
{
var targetable = false;
TaskUtilities.StartTaskAndWait(() =>
{
var styleCurrentWindowStandard = Native.GetWindowLong(WindowHandle, WindowLongIndex.Style);
var styleCurrentWindowExtended = Native.GetWindowLong(WindowHandle, WindowLongIndex.ExtendedStyle);
targetable = styleCurrentWindowStandard.HasTargetStyles() ||
styleCurrentWindowExtended.HasExtendedStyles();
}, Config.Instance.AppSettings.SlowWindowDetection
? 10
: 2);
return targetable;
}
var targetable = false;
await TaskUtilities.StartTaskAndWait(() =>
{
var styleCurrentWindowStandard = Native.GetWindowLong(WindowHandle, WindowLongIndex.Style);
var styleCurrentWindowExtended = Native.GetWindowLong(WindowHandle, WindowLongIndex.ExtendedStyle);
targetable = styleCurrentWindowStandard.HasTargetStyles() || styleCurrentWindowExtended.HasExtendedStyles();
}, Config.Instance.AppSettings.SlowWindowDetection ? 10 : 2);
return targetable;
}

public override string ToString() // so that the ListView control knows how to display this object to the user
Expand Down
19 changes: 11 additions & 8 deletions BorderlessGaming.Logic/System/Utilities/TaskUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,27 @@ namespace BorderlessGaming.Logic.System.Utilities
{
public static class TaskUtilities
{
public static void StartTaskAndWait(Action target)
public static async Task StartTaskAndWait(Action target)
{
StartTaskAndWait(target, 0);
await StartTaskAndWait(target, 0);
}

public static void WaitAndStartTask(Action target, int iHowLongToWait)
public static async Task WaitAndStartTaskAsync(Action target, int iHowLongToWait)
{
var ts = new CancellationTokenSource();
var ct = ts.Token;
Task.Run(async () =>
await Task.Run(async () =>
{
await Task.Delay(TimeSpan.FromSeconds(iHowLongToWait), ct);
target();
}, ct).Wait(ct);
}, ct);
}

public static void StartTaskAndWait(Action target, int iHowLongToWait)
public static async Task StartTaskAndWait(Action target, int iHowLongToWait)
{
try
{
Task.Run(async () =>
await Task.Run(async () =>
{
var ts = new CancellationTokenSource();
var ct = ts.Token;
Expand All @@ -41,6 +41,7 @@ public static void StartTaskAndWait(Action target, int iHowLongToWait)
{
break;
}

if (iHowLongToWait > 0)
{
if ((DateTime.Now - dtStartTime).TotalSeconds > iHowLongToWait)
Expand All @@ -53,13 +54,15 @@ public static void StartTaskAndWait(Action target, int iHowLongToWait)
{
// ignored
}

break;
}
}

await Task.Delay(15, ct);
//MainWindow.DoEvents();
}
}).Wait();
});
}
catch (Exception)
{
Expand Down
13 changes: 7 additions & 6 deletions BorderlessGaming.Logic/Windows/Manipulation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Drawing;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using BorderlessGaming.Logic.Models;
using BorderlessGaming.Logic.Properties;
Expand Down Expand Up @@ -30,12 +31,12 @@ public static class Manipulation
/// <summary>
/// remove the menu, resize the window, remove border, and maximize
/// </summary>
public static void MakeWindowBorderless(ProcessDetails processDetails, Form frmMain, IntPtr targetWindow,
public static async Task MakeWindowBorderless(ProcessDetails processDetails, Form frmMain, IntPtr targetWindow,
Rectangle targetFrame, Favorite favDetails)
{
if (NeedsDelay(targetWindow))
{
MakeWindowBorderlessDelayed(processDetails, frmMain, targetWindow, targetFrame, favDetails);
await MakeWindowBorderlessDelayed(processDetails, frmMain, targetWindow, targetFrame, favDetails);
}
else
{
Expand All @@ -49,7 +50,7 @@ public static void MakeWindowBorderless(ProcessDetails processDetails, Form frmM
{
if (processDetails.MadeBorderless)
{
if (processDetails.MadeBorderlessAttempts > 3 || !processDetails.WindowHasTargetableStyles)
if (processDetails.MadeBorderlessAttempts > 3 || ! await processDetails.WindowHasTargetableStyles())
{
return;
}
Expand Down Expand Up @@ -218,7 +219,7 @@ public static void MakeWindowBorderless(ProcessDetails processDetails, Form frmM
}
}

private static void MakeWindowBorderlessDelayed(ProcessDetails processDetails, Form frmMain,
private static async Task MakeWindowBorderlessDelayed(ProcessDetails processDetails, Form frmMain,
IntPtr targetWindow, Rectangle targetFrame, Favorite favDetails)
{
// Automatically match a window to favorite details, if that information is available.
Expand All @@ -231,7 +232,7 @@ private static void MakeWindowBorderlessDelayed(ProcessDetails processDetails, F
{
if (processDetails.MadeBorderless)
{
if (processDetails.MadeBorderlessAttempts > 3 || !processDetails.WindowHasTargetableStyles)
if (processDetails.MadeBorderlessAttempts > 3 || ! await processDetails.WindowHasTargetableStyles())
{
return;
}
Expand Down Expand Up @@ -374,7 +375,7 @@ private static void MakeWindowBorderlessDelayed(ProcessDetails processDetails, F
);
}
//wait before applying styles
TaskUtilities.WaitAndStartTask(() =>
await TaskUtilities.WaitAndStartTaskAsync(() =>
{
Native.SetWindowLong(targetWindow, WindowLongIndex.Style, styleNewWindowStandard);
Native.SetWindowLong(targetWindow, WindowLongIndex.ExtendedStyle, styleNewWindowExtended);
Expand Down
30 changes: 14 additions & 16 deletions BorderlessGaming.Logic/Windows/Native.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using BorderlessGaming.Logic.Models;
using BorderlessGaming.Logic.System.Utilities;
using BorderlessGaming.Logic.Windows.Audio;
Expand Down Expand Up @@ -219,6 +220,7 @@ public static string GetWindowTitle(IntPtr hWnd)
var length = (int) SendMessage(hWnd, WM_GETTEXTLENGTH, IntPtr.Zero, IntPtr.Zero);
var sbWindowTitle = new StringBuilder(length + 1);
SendMessage(hWnd, WM_GETTEXT, (IntPtr) sbWindowTitle.Capacity, sbWindowTitle);
Console.WriteLine(sbWindowTitle.ToString());
return sbWindowTitle.ToString();
}

Expand Down Expand Up @@ -350,35 +352,31 @@ private static bool GetMainWindowForProcess_EnumWindows(IntPtr hWndEnumerated, u
/// </summary>
/// <param name="process"></param>
/// <returns></returns>
public static IntPtr GetMainWindowForProcess(Process process)
public static async Task<IntPtr> GetMainWindowForProcess(Process process)
{
if (Config.Instance.AppSettings.SlowWindowDetection)
{
try
{
var hMainWindow = IntPtr.Zero;

lock (GetMainWindowForProcess_Locker)
GetMainWindowForProcess_Value = IntPtr.Zero;
await TaskUtilities.StartTaskAndWait(() =>
{
GetMainWindowForProcess_Value = IntPtr.Zero;
TaskUtilities.StartTaskAndWait(() =>
for (uint i = 0; i <= 1; i++)
{
for (uint i = 0; i <= 1; i++)
foreach (ProcessThread thread in process.Threads)
{
foreach (ProcessThread thread in process.Threads)
if (GetMainWindowForProcess_Value != IntPtr.Zero)
{
if (GetMainWindowForProcess_Value != IntPtr.Zero)
{
break;
}

EnumThreadWindows(thread.Id, GetMainWindowForProcess_EnumWindows, i);
break;
}
}
});
hMainWindow = GetMainWindowForProcess_Value;
}

EnumThreadWindows(thread.Id, GetMainWindowForProcess_EnumWindows, i);
}
}
});
hMainWindow = GetMainWindowForProcess_Value;
if (hMainWindow != IntPtr.Zero)
{
return hMainWindow;
Expand Down
3 changes: 3 additions & 0 deletions BorderlessGaming/BorderlessGaming.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@
<DependentUpon>AboutForm.cs</DependentUpon>
<SubType>Designer</SubType>
</EmbeddedResource>
<EmbeddedResource Include="Forms\AboutForm.en-US.resx">
<DependentUpon>AboutForm.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Forms\AboutForm.en.resx">
<DependentUpon>AboutForm.cs</DependentUpon>
<SubType>Designer</SubType>
Expand Down
Loading

0 comments on commit abbed21

Please sign in to comment.