public static void Start() // TODO: Handle two registered processes running at the same time { cancelToken?.Cancel(); cancelToken = new CancellationTokenSource(); Task.Run(async() => { bool processChangedToARegisteredOne = false; bool atLeastARegisteredProcessIsRunning = false; for (; ;) { if (cancelToken.IsCancellationRequested) { return; } processChangedToARegisteredOne = false; atLeastARegisteredProcessIsRunning = false; foreach (var process in listenedProcesses) { Process[] pname = Process.GetProcessesByName(process); // TODO: Sometimes not firing on first boot? if (pname.Length == 0) { continue; } if (process != currentOpenedProcess) { currentOpenedProcess = process; ProcessInFocusChanged?.Invoke(process, pname[0].Id); processChangedToARegisteredOne = true; break; } atLeastARegisteredProcessIsRunning = true; } if (!processChangedToARegisteredOne) { if (!atLeastARegisteredProcessIsRunning) { ProcessInFocusChanged?.Invoke("", -1); // no process is running currentOpenedProcess = ""; } await Task.Delay(2000); } } }).CatchExceptions(); }
public static void Start() { Task.Run(async() => { bool processChangedToARegisteredOne = false; bool atLeastARegisteredProcessIsRunning = false; for (;;) { processChangedToARegisteredOne = false; atLeastARegisteredProcessIsRunning = false; foreach (var process in listenedProcesses) { Process[] pname = Process.GetProcessesByName(process); // TODO: Sometimes not firing on first boot? if (pname.Length == 0) { continue; } if (process != currentOpenedProcess) { currentOpenedProcess = process; ProcessInFocusChanged?.Invoke(process); processChangedToARegisteredOne = true; break; } atLeastARegisteredProcessIsRunning = true; } if (!processChangedToARegisteredOne) { if (!atLeastARegisteredProcessIsRunning) { ProcessInFocusChanged?.Invoke(""); // no process is running currentOpenedProcess = ""; } await Task.Delay(2000); } } }); }