public void WatchCpuAndMemory()
{
var pc = new PerformanceCounter("Processor Information", "% Processor Time");
var cat = new PerformanceCounterCategory("Processor Information");
var cpuInstances = cat.GetInstanceNames();
var cpus = new Dictionary<string, CounterSample>();
var memoryCounter = new PerformanceCounter("Memory", "Available MBytes");
foreach (var s in cpuInstances)
{
pc.InstanceName = s;
cpus.Add(s, pc.NextSample());
}
var t = DateTime.Now;
while (t.AddMinutes(1) > DateTime.Now)
{
Trace.WriteLine(string.Format("Memory:{0}MB", memoryCounter.NextValue()));
foreach (var s in cpuInstances)
{
pc.InstanceName = s;
Trace.WriteLine(string.Format("CPU:{0} - {1:f}", s, Calculate(cpus[s], pc.NextSample())));
cpus[s] = pc.NextSample();
}
//Trace.Flush();
System.Threading.Thread.Sleep(1000);
}
}