public void NewProcessAdded(ProcessTime pt) { if (InvokeRequired) { Invoke(new Action(() => { if (this.IsDisposed) { return; } NewProcessAdded(pt); })); return; } if (trackedProcessList.ContainsKey(pt.ProcessName)) { return; } ProcessTracked tracked = new ProcessTracked(); tracked.ProcessName = pt.ProcessName; tracked.ProcessTitle = pt.ProcessTitle; tracked.Timestamp = pt.GetTimeFormatted(); tracked.SetChecked(pt.GetTracked()); trackedProcessList.Add(pt.ProcessName, tracked); trackerList.Controls.Add(tracked); }
private static void FindProcessLoop() { Program.findProcessTimer = new System.Timers.Timer(2000); findProcessTimer.Elapsed += (o, o2) => { findProcessTimer.Interval = secondsPerLoop * 1000; Process[] processes = Process.GetProcesses(); foreach (Process p in processes) { if (processTimes.ContainsKey(p.ProcessName) || string.IsNullOrEmpty(p.MainWindowTitle)) { continue; } else { try { p.EnableRaisingEvents = true; if (savedProcessTime.ContainsKey(p.ProcessName)) { ProcessTime pt = savedProcessTime[p.ProcessName]; ProcessTime newPT = new ProcessTime(p.ProcessName, p.MainWindowTitle, pt.GetTime()); newPT.SetTracked(pt.GetTracked()); processTimes.Add(p.ProcessName, newPT); if (mainForm != null && mainForm.Visible) { mainForm.NewProcessAdded(pt); } } else { processTimes.Add(p.ProcessName, new ProcessTime(p.ProcessName, p.MainWindowTitle)); if (mainForm != null && mainForm.Visible) { mainForm.NewProcessAdded(processTimes[p.ProcessName]); } } p.Exited += Process_Exited; } catch (Win32Exception) { } } } }; Program.findProcessTimer.AutoReset = true; Program.findProcessTimer.Start(); }
private static void SaveProgramTimes() { Debug.WriteLine("Saving data"); StringCollection collection = new StringCollection(); List <string> addedKeys = new List <string>(); foreach (string key in processTimes.Keys) { if (addedKeys.Contains(key)) { continue; } ProcessTime pt = processTimes[key]; if (pt.GetTime() == 0) { continue; } collection.Add($"{key}?{pt.ProcessName}`{pt.ProcessTitle}`{pt.GetTime()}`{pt.GetTracked()}"); addedKeys.Add(key); } foreach (string key in savedProcessTime.Keys) { if (addedKeys.Contains(key)) { continue; } ProcessTime pt = savedProcessTime[key]; if (pt.GetTime() == 0) { continue; } collection.Add($"{key}?{pt.ProcessName}`{pt.ProcessTitle}`{pt.GetTime()}`{pt.GetTracked()}"); addedKeys.Add(key); } Settings.Default.ProgramTrackData = collection; Settings.Default.Save(); }