问题描述
- C#写任务管理器,怎么能在listview中实现windows下任务管理器那种动态的效果
-
我是在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 = 直接赋值
}
}
时间: 2025-01-21 10:46:40