我是 C# 和 .NET 编程的新手,我正在尝试开发一个类似于 Windows TaskManager 的 Windows 窗体应用程序。我首先使用从 System.Diagnostics.Process 收集的信息填充 listView,然后使用 MethodInvoker 委托启动 System.Threading.Timer,该委托尝试每秒刷新列表视图数据。
问题是应用程序真的很慢,而且,可以肯定的是,我正在做一些不合适的事情。
public partial class Form1 : Form
{
private System.Threading.Timer t;
private Process[] procVett;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.loadAppInfo(null);
TimerCallback timerDelegate = new TimerCallback(this.refresh);
this.t = new System.Threading.Timer(timerDelegate, null, 1000, 3000);
}
private void refresh(Object stateInfo)
{
procVett = Process.GetProcesses();
this.BeginInvoke( (MethodInvoker)delegate
{
ListView.ListViewItemCollection ic = this.appList.Items;
ListView.ListViewItemCollection procIc = this.procList.Items;
time.Text = DateTime.Now.ToLongTimeString();
int count = 0;
procList.BeginUpdate();
foreach (Process p in procVett)
{
try
{
string cputime = p.TotalProcessorTime.TotalMinutes.ToString();
procIc[count].SubItems[1].Text = ("");
procIc[count].SubItems[2].Text = (cputime);
procIc[count].SubItems[3].Text =(p.WorkingSet64.ToString());
procIc[count].SubItems[4].Text = ("");
}
catch (System.Exception)
{
}
count++;
}
procList.EndUpdate();
}, null);
}
}