在form1的顶部,我做了:
private System.Timers.Timer _refreshTimer;
private int _thisProcess;
然后在Form1 Load事件中:
_thisProcess = Process.GetCurrentProcess().Id;
InitializeRefreshTimer();
PopulateApplications();
然后是计时器init方法:
void InitializeRefreshTimer()
{
_refreshTimer = new System.Timers.Timer(5000);
_refreshTimer.SynchronizingObject = this;
_refreshTimer.Elapsed += new System.Timers.ElapsedEventHandler(TimerToUpdate_Elapsed);
_refreshTimer.Start();
}
然后计时器已经过了事件:
void TimerToUpdate_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
PopulateApplications();
}
最后是Populate方法:
void PopulateApplications()
{
dataGridView1.Rows.Clear();
foreach (Process p in Process.GetProcesses("."))
{
if (p.Id != _thisProcess)
{
try
{
if (p.MainWindowTitle.Length > 0)
{
String status = p.Responding ? "Running" : "Not Responding";
dataGridView1.Rows.Add( p.MainWindowTitle, status);
}
}
catch { }
}
}
}
变量状态显示在column2中但是我想说我希望在column5中显示每个进程/应用程序的状态?我该怎么办呢?
这是一张显示我正在运行程序的图像:
这是我使用的方法:
void PopulateApplications()
{
dataGridView1.Rows.Clear();
DataGridViewImageColumn img = new DataGridViewImageColumn();
img.HeaderText = "Icon";
img.Name = "ImageCol";
dataGridView1.Columns.Add(img);
dataGridView1.Columns.Add("AppName", "Application Name");
dataGridView1.Columns.Add("Status", "Status");
foreach (Process p in Process.GetProcesses())
{
if (p.MainWindowTitle.Length > 1)
{
var icon = Icon.ExtractAssociatedIcon(p.MainModule.FileName);
Image ima = icon.ToBitmap();
(dataGridView1.Columns["ImageCol"] as DataGridViewImageColumn).Image = ima;
String status = p.Responding ? "Running" : "Not Responding";
dataGridView1.Rows.Add(icon, p.MainWindowTitle, status);
}
}
}
答案 0 :(得分:1)
你能试试这段代码吗?
void PopulateApplications()
{
dataGridView1.Rows.Clear();
DataGridViewImageColumn img = new DataGridViewImageColumn();
img.HeaderText = "Icon";
img.Name = "ImageCol";
dataGridView1.Columns.Add(img);
dataGridView1.Columns.Add("AppName", "Application Name");
dataGridView1.Columns.Add("Status", "Status");
foreach (Process p in Process.GetProcesses())
{
if (p.MainWindowTitle.Length > 1)
{
var icon = Icon.ExtractAssociatedIcon(p.MainModule.FileName);
Image ima = icon.ToBitmap();
String status = p.Responding ? "Running" : "Not Responding";
dataGridView1.Rows.Add(ima, p.MainWindowTitle, status);
}
}
}