CPU usage

Something like this:

VB.NET:
[COLOR=blue]Private[/COLOR] [COLOR=blue]Shared[/COLOR] cpu [COLOR=blue]As[/COLOR] [COLOR=blue]New[/COLOR] PerformanceCounter([COLOR=maroon]"Processor"[/COLOR], [COLOR=maroon]"% Processor Time"[/COLOR], [COLOR=maroon]"_Total"[/COLOR])
 
[COLOR=blue]Private[/COLOR] [COLOR=blue]Function[/COLOR] CpuUsage() [COLOR=blue]As[/COLOR] [COLOR=blue]Single[/COLOR]
[COLOR=blue]  Return[/COLOR] cpu.NextValue()
[COLOR=blue]End[/COLOR] [COLOR=blue]Function[/COLOR]
 
Thanks, I tried this, but I get 0 everytime, even when the task manager says that it is anything but 0. Did I leave something out?
 
Does Private Shared cpu As New PerformanceCounter("Processor", "% Processor Time", "_Total") give me the total percentage time that my paticular program has gotten? It seems like there should be something like :

Private Shared cpu As New PerformanceCounter("Processor", "% Processor Usage", "_Total") or something along those lines....

Is there anyway to find out what are the valid arguments that go in these parameters?
 
cBarry263 said:
Thanks, I tried this, but I get 0 everytime, even when the task manager says that it is anything but 0. Did I leave something out?
The first call will return 0 (that's why I made the Performance counter a shared member: make sure it does not go out of scope, or you'll instantiate a new Perf counter with every call).
 
cBarry263 said:
Does Private Shared cpu As New PerformanceCounter("Processor", "% Processor Time", "_Total") give me the total percentage time that my paticular program has gotten? It seems like there should be something like :

Private Shared cpu As New PerformanceCounter("Processor", "% Processor Usage", "_Total") or something along those lines....

Is there anyway to find out what are the valid arguments that go in these parameters?

Yes. For instance for Win 2003
 
Thanks for the link, % Processor Time, as I understand it, only gives me the the percentage of elapsed time for this thread. I want the total CPU Usage, as in if I open the Task Manager and look it says 25%, I want my program to do the same thing. The reason for this is that my program is supposed to go into sleep mode for a period of time if the CPU usage > 30% when the program first starts.
 
Back
Top