我是在Timer控件裡,每隔一個周期清空一遍再刷新,但是這個效果太挫了
private void timer1_Tick(object sender, EventArgs e)
{
this.listView1.Items.Clear();
foreach (Process ps in Process.GetProcesses())
{
PerformanceCounter cpuload = new PerformanceCounter();
cpuload.CategoryName = "Process";
cpuload.CounterName = "% Processor Time";
cpuload.InstanceName = ps.ProcessName;
PerformanceCounter pribyte = new PerformanceCounter();
pribyte.CategoryName = "Process";
pribyte.CounterName = "Private Bytes";
pribyte.InstanceName = ps.ProcessName;
PerformanceCounter workbyte = new PerformanceCounter();
workbyte.CategoryName = "Process";
workbyte.CounterName = "Working Set";
workbyte.InstanceName = ps.ProcessName;
PerformanceCounter virbyte = new PerformanceCounter();
virbyte.CategoryName = "Process";
virbyte.CounterName = "Virtual Bytes";
virbyte.InstanceName = ps.ProcessName;
ListViewItem lv = new ListViewItem();
lv.ImageIndex = 0;
lv.Text = ps.ProcessName;
float cpu = cpuload.NextValue();
lv.SubItems.Add(cpuload.NextValue().ToString() + "%");
lv.SubItems.Add(Convert.ToString(pribyte.NextValue() / 1024) + "KB");
lv.SubItems.Add(Convert.ToString(workbyte.NextValue() / 1024) + "KB");
lv.SubItems.Add(Convert.ToString(virbyte.NextValue() / 1024) + "KB");
lv.SubItems.Add(Convert.ToString(ps.Id));
listView1.Items.Add(lv);
}
}
遍歷直接修改。
foreach (ListViewItem lvi in listView1.Items)
{
if (lvi.SubItems[代表進程id的那一列] == 你的進程id)
{
lvi.SubItems[xxx].Text = 直接賦值
}
}