1 var processes = Process.GetProcesses(PCName); 2 processes.ToList<Process>().ForEach( 3 p => 4 { 5 if (!string.IsNullOrEmpty(p.ProcessName)) 6 processList.Add(p.ProcessName); 7 }); 8 9 processList.Sort(); 10 11 return processList;
Check是否能够成功进行监控.
1 var pcc = new PerformanceCounterCategory("Process", PCName); 2 3 return pcc.GetCounters(ProcessName) != null;
1 timer.Tick -= Record; 2 timer.Tick += Record; 3 timer.Interval = Intervalseconds * 1000; 4 timer.Enabled = true;
1 CPUCounter = new PerformanceCounter(); 2 MemoryCounter = new PerformanceCounter(); 3 4 CPUCounter.CategoryName = "Process"; 5 CPUCounter.CounterName = "% Processor Time"; 6 7 MemoryCounter.CategoryName = "Process"; 8 MemoryCounter.CounterName = "Working Set"; 9 10 CPUCounter.MachineName = setting.ComputerName; 11 CPUCounter.InstanceName = setting.ProcessName; 12 13 MemoryCounter.MachineName = setting.ComputerName; 14 MemoryCounter.InstanceName = setting.ProcessName; 15 16 CPUCounter.NextValue(); 17 MemoryCounter.NextValue();
1 datas.Append(DateTime.Now.ToString("hh:mm:ss") + ","); 2 if (setting.MonitorCPU) 3 { 4 datas.Append(CPUCounter.NextValue().ToString() + ","); 5 } 6 7 datas.Append(DateTime.Now.ToLongTimeString() + ","); 8 if (setting.MonitorMemory) 9 { 10 datas.Append((MemoryCounter.NextValue() / 1048576.0).ToString() + ","); 11 }
停止性能记录动作.
1 timer.Enabled = false; 2 Thread.Sleep(100);
为了方便大家理解,源代码附上.http://files.cnblogs.com/robyn/PerformanceMonitor.rar
原文:http://www.cnblogs.com/robyn/p/3783602.html