CPU usage

Hi,

I am looking for a simple way to diaplay CPU usage and (if possible) memory usage on a windows form.

Cameron.

[133 byte] By [CazaD] at [2007-12-24]
# 1

import the System.Diagnostics namespace.

then declare globally:

private PerformanceCounter theCPUCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");

then to get the CPU time, simply call the "NextValue()" method:

this.theCPUCounter.NextValue();

this will get you the CPU usage

as for mem usage, same thing applies I believe:

private PerformanceCounter theMemCounter = new PerformanceCounter("Memory", "Available MBytes");

..

..

this.theMemCounter.NextValue();

does this help?

overall I could make a solution which allows me to check both values on every second using a timer and setting its interval to 1 second (1000 milliseconds):



using System.Diagnostics;
namespace SomeProject
{
public partial class Form1 : Form
{
private PerformanceCounter theCPUCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");
private PerformanceCounter theMemCounter = new PerformanceCounter("Memory", "Available MBytes");
public Form1()
{
InitializeComponent();
}
private void Timer_Tick(object sender, EventArgs e)
{
this.theLabelInfo.Text = this.theCPUCounter.NextValue().ToString() + "%" + Environment.NewLine + this.theMemCounter.NextValue().ToString() + "MB";
}
}
}

http://msdn2.microsoft.com/en-us/library/system.diagnostics.performancecounter.aspx

ahmedilyas at 2007-10-8 > top of Msdn Tech,Visual C#,Visual C# General...
# 2
Yes, But does that display the information as well?
CazaD at 2007-10-8 > top of Msdn Tech,Visual C#,Visual C# General...
# 3
of course, thats what the NextValue() method returns :-) I was editing my post to give you a running example (almost running except you would have to place the components on the form etc...)
ahmedilyas at 2007-10-8 > top of Msdn Tech,Visual C#,Visual C# General...
# 4
OK Thanks.
CazaD at 2007-10-8 > top of Msdn Tech,Visual C#,Visual C# General...
# 5

The code came up with a few errors;

1. Error 1 'System.Diagnostics.PerformanceCounter.NextValue()' is a 'method', which is not valid in the given context.

2. Error 2 'System.Environment' does not contain a definition for 'Newline'.

3. Error 3 'System.Diagnostics.PerformanceCounter.NextValue()' is a 'method', which is not valid in the given context.

CazaD at 2007-10-8 > top of Msdn Tech,Visual C#,Visual C# General...
# 6

remember there maybe some typo errors in the syntax so be sure to correct it using the Intellisense - Newline should be NewLine

As for the rest - what you have done is incorrect. Please re read the code posted :-) - looks like you are doing NextValue.ToString() rather than NextValue().ToString(), since NextValue() is a method not a property

ahmedilyas at 2007-10-8 > top of Msdn Tech,Visual C#,Visual C# General...
# 7

That is all good now but now i have another error in this line of code:

privatePerformanceCounter theMemCounter = newPerformanceCounter("Memory", "AvailableMBytes");

Could not locate Performance Counter with specified category name 'Memory', counter name 'AvailableMBytes'.

CazaD at 2007-10-8 > top of Msdn Tech,Visual C#,Visual C# General...
# 8

should be:

privatePerformanceCounter theMemCounter = newPerformanceCounter("Memory", "Available MBytes");

ahmedilyas at 2007-10-8 > top of Msdn Tech,Visual C#,Visual C# General...
# 9

OK Thanks for that, it all works.

CazaD at 2007-10-8 > top of Msdn Tech,Visual C#,Visual C# General...
# 10

How know my proccess name?

Reading this genial thread, i'm monitoring the cpu and mem usage of my app, but i must put explicit name of my proccess in:

privatePerformanceCounter MEM = newPerformanceCounter("Proceso", "Espacio de trabajo","MyApp");

And when run like 'Release' runs Ok, but when run in 'Debug' crash because the proccess is called in another way.

Regards.

overpeer at 2007-10-8 > top of Msdn Tech,Visual C#,Visual C# General...
# 11

the reason it appears to crash in debug mode could be perhaps because its using Vshost to launch/run your actual application and would appear to run fine when in release mode - this could be one factor.

What happens when you run the exe file on its own in debug mode and release mode (not in the IDE itself)?

it should not just crash, well it may well do but are you sure it does not throw an exception instead?

http://msdn2.microsoft.com/en-us/library/xx7e9t8e.aspx

ahmedilyas at 2007-10-8 > top of Msdn Tech,Visual C#,Visual C# General...
# 12

I'm sorry, with 'crash' i want say that raise a exeption.

If i run the app in Debug mode INTO the IDE, throw a InvalidOperationException "Instance 'MyApp' does not exist in the specified Category.".

If i run the app from 'Debug/MyApp.exe' works ok, and if run the app from 'Release/MyApp.exe' runs ok too.

Thanks, i will catch the exeption but ... how to do that runs always? In VB there was a App.Name or something....

Regards.

overpeer at 2007-10-8 > top of Msdn Tech,Visual C#,Visual C# General...
# 13

I also get this, I actually wrote a fair amount of a reply and now deleted it! :-)

I'm wondering why it works fine outside the IDE as when I tried it didn't. the App.Name would be Application.ProductName perhaps but this may not resolve the issue

ahmedilyas at 2007-10-8 > top of Msdn Tech,Visual C#,Visual C# General...
# 14

:P

Thanks, i will follow search a way to do it (if exist, of course xD).

More interesting the PerformanceCounters, i like it :)

Regards.

overpeer at 2007-10-8 > top of Msdn Tech,Visual C#,Visual C# General...