Skip to content

Commit

Permalink
fix(Foreground): Fix unhandled crash in foreground window detection
Browse files Browse the repository at this point in the history
  • Loading branch information
Belphemur committed May 8, 2021
1 parent 30d021f commit b61c347
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions SoundSwitch.Audio.Manager/WindowMonitor.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Serilog;
using SoundSwitch.Audio.Manager.Interop.Com.Threading;
using SoundSwitch.Audio.Manager.Interop.Com.User;

Expand Down Expand Up @@ -67,11 +68,21 @@ public WindowMonitor()
return;
var (processId, windowText, windowClass) = ProcessWindowInformation(hwnd);

//Couldn't find the processId of the window
if (processId == 0) return;

Task.Factory.StartNew(() =>
{
var process = Process.GetProcessById((int) processId);
var processName = process.MainModule?.FileName ?? "N/A";
ForegroundChanged?.Invoke(this, new Event(processId, processName, windowText, windowClass, hwnd));
try
{
var process = Process.GetProcessById((int) processId);
var processName = process.MainModule?.FileName ?? "N/A";
ForegroundChanged?.Invoke(this, new Event(processId, processName, windowText, windowClass, hwnd));
}
catch (Exception e)
{
Log.Warning(e, "Couldn't get info about foreground process");
}
});
};

Expand Down

0 comments on commit b61c347

Please sign in to comment.