【发布时间】:2014-06-27 21:34:10
【问题描述】:
这就是我在 form1 中所做的:
void PopulateApplications()
{
DoubleBufferedd(dataGridView1, true);
int rcount = dataGridView1.Rows.Count;
int rcurIndex = 0;
foreach (Process p in Process.GetProcesses())
{
try
{
if (File.Exists(p.MainModule.FileName))
{
memoryUsage = Core.getallmemoryusage(p.ProcessName);
Core.getcpu(p.ProcessName);
cpuusage = Core.processes;
var icon = Icon.ExtractAssociatedIcon(p.MainModule.FileName);
Image ima = icon.ToBitmap();
ima = resizeImage(ima, new Size(25, 25));
ima = (Image)(new Bitmap(ima, new Size(25, 25)));
String status = p.Responding ? "Running" : "Not Responding";
if (rcurIndex < rcount - 1)
{
var currentRow = dataGridView1.Rows[rcurIndex];
currentRow.Cells[0].Value = ima;
currentRow.Cells[1].Value = p.ProcessName;
currentRow.Cells[2].Value = cpuusage;
currentRow.Cells[3].Value = memoryUsage;
currentRow.Cells[4].Value = status;
}
else
{
dataGridView1.Rows.Add(ima, p.ProcessName,cpuusage,memoryUsage, status);//false, ima, p.ProcessName, status);
}
rcurIndex++;
}
}
catch ( Exception e)
{
string t = "error";
}
}
if (rcurIndex < rcount - 1)
{
for (int i = rcurIndex; i < rcount - 1; i++)
{
dataGridView1.Rows.RemoveAt(rcurIndex);
}
}
}
现在 form1 PopulateApplications 中的方法,我每 5 秒从一个计时器滴答事件中调用它。
然后我每次循环处理进程并获取内存使用情况和 CPU 使用情况。
这是Core类中内存和CPU的方法。
使用记忆法没有问题。工作又好又快。
public static string getallmemoryusage(string processName)
{
var counter = new PerformanceCounter("Process", "Working Set - Private", processName);
privateMemeory = (counter.NextValue() / 1024 / 1024).ToString();
//string.Format("Private memory: {0}k", counter.NextValue() / 1024 / 1024);
return privateMemeory;
}
问题在于getcpu 方法。我需要让它每隔 1000 毫秒休眠几次以获取 CPU 使用率。如果我在这个方法上使用断点,我会得到最后的值。问题是,当我每 5 秒调用一次 form1 中的方法时,它也会每 5 秒调用一次 getcpu 并执行此操作,并且这些线程睡眠使其工作非常缓慢。如果我将线程睡眠设置为 10 毫秒,它会更快,但我会在大多数进程上使用 0% 或 100%。
public static string getcpu(string name)
{
var cpuload = new PerformanceCounter("Processor", "% Processor Time", "_Total");
processes = Convert.ToInt32(cpuload.NextValue()) + "%";
System.Threading.Thread.Sleep(1000);
processes = cpuload.NextValue() + "%";
System.Threading.Thread.Sleep(1000);
processes = cpuload.NextValue() + "%";
System.Threading.Thread.Sleep(1000);
processes = cpuload.NextValue() + "%";
System.Threading.Thread.Sleep(1000);
processes = cpuload.NextValue() + "%";
System.Threading.Thread.Sleep(1000);
return processes;
}
【问题讨论】:
-
Rotem 是的,我用更新的 getcpu 方法使用 name 变量改变了我的问题,并且我没有在 form1 中再次迭代所有进程。我仍然在每个过程中得到 0%。例如,在测试列表中,我看到 1 个索引和:chorme CPU0% 并且每个进程都相同。
-
在 getcpu 方法中我没有得到异常,我添加了 try and catch now no exceptions。但是在 form1 中,它有时会在某些进程访问被拒绝时抛出异常,但这些是系统进程,所以我不介意他们被拒绝访问。
-
我刚刚再次更新了我的问题,显示内存使用部分没有问题。仍然 cpu 使用不工作。
-
用我现在在问题底部尝试的内容再次更新了我的问题。尝试了另一种解决方案,但它也不起作用。尝试了这个解决方案:stackoverflow.com/questions/2181828/… 但是我如何使用 getcpu 方法中的进程名称以及现在在哪里使用? (变量名)
-
除了您需要将结果除以处理器数量(即 System.Environment.ProcessorCount)这一事实之外,我认为您的代码没有任何问题,它对我有用。如果您尝试获取 % 空闲时间(即传入“Idle”作为第三个参数而不是处理器名称或“_Total”)会发生什么