private void BgWorker1_DoWork(object sender, DoWorkEventArgs e) { do { // InitPerformanceCounters method sends events to the backgroundWorker1_ProgressChanged method TaskManagerLib.GetProcesses(out Process[] procArray); int procsAdded = TaskManagerLib.AddNewProcesses(ref procList, procArray, excludedProcesses); if (procsAdded > 0) { bgWorker1.ReportProgress(1); } Console.WriteLine($"Processes added : {procsAdded}"); int valsUpdated = TaskManagerLib.UpdateValues(ref procList); if (valsUpdated > 0) { bgWorker1.ReportProgress(1); } Console.WriteLine($"Processes Updated : {valsUpdated}"); int procsRemoved = TaskManagerLib.RemoveDeadProcesses(ref procList, procArray); if (procsRemoved > 0) { bgWorker1.ReportProgress(1); } Console.WriteLine($"Processes removed : {procsRemoved}"); // use Task.Delay to avoid blocking Task.Delay(1200).Wait(); }while (true); }
public ProcInfo(Process process, string name, int processid) { try { this.Name = name; this.Pid = processid; this.RamUsage = 0; this.CpuUsage = 0; // time consuming part is creating the performance counters this.CpuCounter = TaskManagerLib.GetPerfCounterForProcessId(this.Pid, "% Processor Time"); this.RamCounter = TaskManagerLib.GetPerfCounterForProcessId(this.Pid, "Working Set - Private"); this.RamUsage = TaskManagerLib.GetNextValue(this.RamCounter, "ram"); this.CpuUsage = TaskManagerLib.GetNextValue(this.CpuCounter, "cpu"); Console.WriteLine($"ProcInfo object created (Pid): {this.Pid}"); } catch (Exception ex) { Console.WriteLine($"Exception thrown {ex.Message}"); } }
/* ********************************************************************************** * Summary: Record position of scrollbar. Unbind and rebind procList to the * Listbox. Set old scrollbar position back so user doesn't get bumped * back to the top of the list while they are scrolling. * Return: void. Listbox control updates. ***********************************************************************************/ private void ShowData() { // update the listbox UI when background worker reports progress procListView.Clear(); System.StringComparison stringComparison = System.StringComparison.CurrentCulture; for (int i = 0; i < procList.Count; i++) { procListView.Add(new ProcView(procList[i])); } switch (sortState) { case SortedBy.NONE: break; case SortedBy.NAME: procListView.Sort((x, y) => string.Compare(x.Name, y.Name, stringComparison)); break; case SortedBy.CPU: procListView.Sort((x, y) => y.CpuUsage.CompareTo(x.CpuUsage)); break; case SortedBy.RAM: procListView.Sort((x, y) => y.RamUsage.CompareTo(x.RamUsage)); break; } // record old position of scrollbar var offset = this.listBox1.TopIndex; this.listBox1.DataSource = null; TaskManagerLib.FilterByText(ref procListView, this.textBoxFilter.Text); this.listBox1.DataSource = procListView; // set scrollbar back to original offset so user can scroll while its updating // without the scrollbar going back up to the top this.listBox1.TopIndex = offset; }