private static HashSet <string> GetNowRunning() { IList <TaskListWrapper.AppProcess> table; try { var wrapper = new TaskListWrapper(); table = wrapper.GetBasicAppProcesses("running").ConfigureAwait(false).GetAwaiter().GetResult(); } catch (Exception e) { Logger.Warn(e, "Could not get the list of running processes."); return(null); } var nowRunning = new HashSet <string>(); foreach (var item in table) { Logger.Debug($"PID = {item.ProcessId}, Name = {item.PackageName}, Memory = {item.MemoryUsage}, Image = {item.ImageName}"); nowRunning.Add(item.PackageName); } return(nowRunning); }
private async Task DoCheck() { if (!this.timer.Enabled) { return; } if (this.consideredPackages == null) { return; } Logger.Debug("Checking for running apps."); try { this.timer.Stop(); var wrapper = new TaskListWrapper(); IList <TaskListWrapper.AppProcess> table; try { table = await wrapper.GetBasicAppProcesses("running").ConfigureAwait(false); } catch (Exception e) { Logger.Warn(e, "Could not get the list of running processes."); return; } var nowRunning = new HashSet <string>(); foreach (var item in table) { Logger.Debug($"PID = {item.ProcessId}, Name = {item.PackageName}, Memory = {item.MemoryUsage}, Image = {item.ImageName}"); nowRunning.Add(item.PackageName); } if (this.previouslyRunningAppIds != null && nowRunning.SequenceEqual(this.previouslyRunningAppIds)) { Logger.Debug("The list of running apps has not changed."); return; } Logger.Info("Notifying about updated app state."); this.previouslyRunningAppIds = nowRunning; this.Publish(new ActivePackageFullNames(nowRunning)); } catch (Exception e) { Logger.Error(e); throw; } finally { this.timer.Start(); } }
public async Task Stop(string packageFullName, CancellationToken cancellationToken = default) { Logger.Info("Stopping package " + packageFullName); var taskListWrapper = new TaskListWrapper(); IList <TaskListWrapper.AppProcess> processes; try { processes = await taskListWrapper.GetBasicAppProcesses(null, cancellationToken).ConfigureAwait(false); } catch (Exception e) { throw new ApplicationException("Could not get the list of running processes.", e); } var processIds = processes.Where(p => p.PackageName == packageFullName).Select(p => p.ProcessId).ToArray(); Logger.Info("The package appears to have currently {0} associated running processes with the following PIDs: {1}.", processIds.Length, string.Join(", ", processIds)); foreach (var pid in processIds) { Logger.Info("Killing process PID = " + pid + " and its children..."); try { var p = Process.GetProcessById(pid); p.Kill(true); } catch (ArgumentException) { Logger.Warn("Could not find the process PID = " + pid + ". Most likely, the process has been already killed or stopped."); } catch (Exception e) { Logger.Warn("Could not kill process PID = " + pid + ".", e); } } }