static void RemoveWindow(WindowInstance instance) { Task.Run(async() => { Logger.LogInfo("Removing Window Process -> [" + instance.ProcessID + "]"); if (SelectedWindowInstance != null) { if (SelectedWindowInstance.InstanceProcess.Id.Equals(instance.ProcessID)) { await Common.Window.Dispatcher.InvokeAsync(() => { Common.Window.SetCurrentView(NavigationButtons.Window_Selection); }); SelectedWindowInstance = null; } } if (Common.Window != null) { await Common.Window.Dispatcher.InvokeAsync(() => { if (Common.Window.ContentFrame.Content is WindowSelectionPage) { (Common.Window.ContentFrame.Content as WindowSelectionPage).OpenWindowsList.Children.Remove(instance.ListItem); } }, DispatcherPriority.Background); } Windows.Remove(instance); }); }
internal static void BeginUpdater() { foreach (Process p in Process.GetProcesses()) { AddWindow(p); } addWatcher = new ManagementEventWatcher(); addWatcher.Query = new WqlEventQuery("SELECT * FROM Win32_ProcessStartTrace"); addWatcher.Start(); addWatcher.EventArrived += ((object sender, EventArrivedEventArgs args) => { DoEvent(); void DoEvent(bool isRetry = false) { try { AddWindow(Process.GetProcessById(int.Parse(args.NewEvent.Properties["ProcessId"].Value.ToString())), true); } catch (ArgumentException) { Logger.LogWarn("Picked up process that closed immediatly [RETRY]"); Retry(); } void Retry() { if (RetriesLeft != 0) { if (RetriesLeft == MaxRetries || isRetry) { RetriesLeft--; } DoEvent(true); } else { RetriesLeft = MaxRetries; } } } }); removeWatcher = new ManagementEventWatcher(); removeWatcher.Query = new WqlEventQuery("SELECT * FROM Win32_ProcessStopTrace"); removeWatcher.Start(); removeWatcher.EventArrived += ((object sender, EventArrivedEventArgs args) => { WindowInstance instance = Windows.FirstOrDefault(x => x.ProcessID.Equals(int.Parse(args.NewEvent.Properties["ProcessId"].Value.ToString()))); if (instance != null) { if (!instance.Equals(default(WindowInstance))) { RemoveWindow(instance); } } }); IsUpdaterInitialized = true; }