private void ProgramWatcherThread() { Thread.CurrentThread.IsBackground = true; while (!_shouldShutdown) { List <string> allPaths = new List <string>(); // This method for getting all processes comes from https://stackoverflow.com/a/5497319 var wmiQueryString = "SELECT ProcessId, ExecutablePath, CommandLine FROM Win32_Process"; using (var searcher = new ManagementObjectSearcher(wmiQueryString)) using (var results = searcher.Get()) { var query = Process.GetProcesses() .Join(results.Cast <ManagementObject>(), p => p.Id, mo => (int)(uint)mo["ProcessId"], (p, mo) => new ProcessInfo { process = p, path = (string)mo["ExecutablePath"], }); foreach (var item in query) { if (item.path != null) { allPaths.Add(item.path); } } } allPaths.Sort(); bool anyNewInstances = false; lock (_processStateLock) { var targetCopy = new Dictionary <string, bool>(_targetProcessStates); foreach (var watching in targetCopy) { int index = allPaths.BinarySearch(watching.Key); if (index >= 0) // Process is running { if (watching.Value == false) // it wasn't running last time { _targetProcessStates[watching.Key] = true; anyNewInstances = true; } } else // Process is not running { _targetProcessStates[watching.Key] = false; } } } if (anyNewInstances) { _iconRef.PopupMessage("Don't forget to watch your important files...", 4); } Thread.Sleep(2000); } }
private void OnMainWindowClose(object sender, CancelEventArgs e) { if (sender is MainWindow win && !_exiting) { if (Settings.RunInBackground) { e.Cancel = true; win.Hide(); if (!Settings.RunInBackgroundPopShown) { _icon.PopupMessage("Sidesaver will continue running in the background...", 2); Settings.RunInBackgroundPopShown = true; } } } }