Getting CPU Usage
-
I'm trying to get the cpu usage of the running processes and I thought I had found the answer the PerformanceCounter but I can't really get it to work. I start of by filling a ListView with the running processes and then update mem-usage and cpu-usage every second with a Timer but the cpu-usage is always reported to be 0. Could this has something to do with that I'm using a dual-core processor or is something else wrong? This is what I've done...
public void GetProcesses()
{
pThread = new Thread(FillProcessList);
pThread.Start();
}private void FillProcessList()
{
Process[] myProcesses = Process.GetProcesses();for (int i = 0; i < myProcesses.Length; i++) { int pId = myProcesses\[i\].Id; string pName = myProcesses\[i\].ProcessName; string pUser = GetProcessOwner(pId); ListViewItem item = new ListViewItem(new string\[\] { pId.ToString(), pName, pUser, string.Empty, string.Empty }); item.Tag = myProcesses\[i\]; lvProcesses.Items.Add(item); }
}
private void TrackProcesses()
{
if (pThread != null)
if (!pThread.IsAlive)
{
updateTimer.Stop();
foreach (ListViewItem item in lvProcesses.Items)
{
Process p = Process.GetProcessById(((Process)item.Tag).Id);item.SubItems\[3\].Text = (p.WorkingSet64 / 1024.0M).ToString(); PerformanceCounter counter = new PerformanceCounter(); counter.CategoryName = "Process"; counter.CounterName = "% Processor Time"; counter.InstanceName = p.ProcessName; item.SubItems\[4\].Text = counter.NextValue().ToString(); } updateTimer.Start(); }
}
public void StopPThread()
{
if (pThread != null)
pThread.Abort();
}private void updateTimer_Tick(object sender, EventArgs e)
{
TrackProcesses();
}http://johanmartensson.se - Home of MPEG4Watcher
-
I'm trying to get the cpu usage of the running processes and I thought I had found the answer the PerformanceCounter but I can't really get it to work. I start of by filling a ListView with the running processes and then update mem-usage and cpu-usage every second with a Timer but the cpu-usage is always reported to be 0. Could this has something to do with that I'm using a dual-core processor or is something else wrong? This is what I've done...
public void GetProcesses()
{
pThread = new Thread(FillProcessList);
pThread.Start();
}private void FillProcessList()
{
Process[] myProcesses = Process.GetProcesses();for (int i = 0; i < myProcesses.Length; i++) { int pId = myProcesses\[i\].Id; string pName = myProcesses\[i\].ProcessName; string pUser = GetProcessOwner(pId); ListViewItem item = new ListViewItem(new string\[\] { pId.ToString(), pName, pUser, string.Empty, string.Empty }); item.Tag = myProcesses\[i\]; lvProcesses.Items.Add(item); }
}
private void TrackProcesses()
{
if (pThread != null)
if (!pThread.IsAlive)
{
updateTimer.Stop();
foreach (ListViewItem item in lvProcesses.Items)
{
Process p = Process.GetProcessById(((Process)item.Tag).Id);item.SubItems\[3\].Text = (p.WorkingSet64 / 1024.0M).ToString(); PerformanceCounter counter = new PerformanceCounter(); counter.CategoryName = "Process"; counter.CounterName = "% Processor Time"; counter.InstanceName = p.ProcessName; item.SubItems\[4\].Text = counter.NextValue().ToString(); } updateTimer.Start(); }
}
public void StopPThread()
{
if (pThread != null)
pThread.Abort();
}private void updateTimer_Tick(object sender, EventArgs e)
{
TrackProcesses();
}http://johanmartensson.se - Home of MPEG4Watcher
The MSDN[^] documentation for
NextValue
says that invoking the property on a newly createdPerformanceCounter
instance is like resetting the counter - you'll have to call it twice, with a (recommended) interval of one second. Alternatively, you could make the counter a member variable of the class and callNextValue
once in the constructor. This way, it gets created only once and successive calls will return the correct values.Regards Senthil [MVP - Visual C#] _____________________________ My Blog | My Articles | My Flickr | WinMacro
-
The MSDN[^] documentation for
NextValue
says that invoking the property on a newly createdPerformanceCounter
instance is like resetting the counter - you'll have to call it twice, with a (recommended) interval of one second. Alternatively, you could make the counter a member variable of the class and callNextValue
once in the constructor. This way, it gets created only once and successive calls will return the correct values.Regards Senthil [MVP - Visual C#] _____________________________ My Blog | My Articles | My Flickr | WinMacro
Thanks, I'll look into that
http://johanmartensson.se - Home of MPEG4Watcher