could some1 show me an example of a way to monitor a process' cpu and memory usages?...btw im gonna put the values in 2 progressbars for users to view in my app....thx in advance
AcE
Printable View
could some1 show me an example of a way to monitor a process' cpu and memory usages?...btw im gonna put the values in 2 progressbars for users to view in my app....thx in advance
AcE
You probably want to look into the Process component:
ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfsystemdiagnosticsprocessclasstopic.htm
Many examples of code are in the help.
yea i have....i been reading threw that, lookin on vbcity, lookin on planet-source-code, and lookin on here all day....but i cant find the exact way to get the values......but knowing me i have overlooked them....thats why i posted :)
AcE
process.workingset gives you the total memory usage....im still looking for cpu...
AcE
Hmm well actually the perfmon COM object is probably much more useful for what you seem to be looking to do, although it's kind of overkill.
ms-help://MS.VSCC/MS.MSDNVS/vbref/html/vbsamwindowsformsperformancemonitorsample.htm
complete sample app included.
edit: woops perfmon actually has a .NET version according to the docs. and duh, the component is in the toolbox under Components.
yea i looked at that earlyer....but i couldnt find a single command to return a number of process cycles or percentage of processor usage....
AcE
The following I lifted off a forum:
To get the CPU load and other information about system, you may use the
System.Diagnostics.PerformanceCounter class.The PerformanceCounter
component can be used for both reading existing predefined or custom
counters and publishing (writing) performance data to custom counters.
To read from a performance counter, create an instance of the
PerformanceCounter class, set the CategoryName, CounterName, and,
optionally, the InstanceName or MachineName properties, and then call the
NextValue method to take a performance counter reading.
VB Code:
Imports System.Diagnostics Dim myCounter as PerformanceCounter = new PerformanceCounter() myCounter.CategoryName = "Processor" myCounter.CounterName = "% Processor Time" myCounter.InstanceName = "_Total" msgbox("The Processor Time: " & myCounter.RawValue) 'or Label1.Text = myCounter.RawValue
The value returned is the reading at that time... what you can do to illustrate it better is declare myCounter as Private at the Form LEvel, then run the rest of the code underneath a button click...
InstanceName has to do with how many processors or which processor you want to monitor, in this case, its set to Total to measure all processors on the machine.. which is most likely just 1.
The machine name determines which computer should be monitored.
The category name is the category the counter is under, such as Processor, Paging File, and PhysicalDisk.
The counter name indicates the specific counter, such as % Processor Time.
The instance name determines the instance of the counter. For example, a four-processor server has four instances of the % Processor Time counter, plus a total counter.
-ZDNET australia
http://www.zdnet.com.au/builder/prog...0272791,00.htm
yup yup....i saw that....it wuz in the sample code.....but i dont really understand the concept here.....its returning the processor "time" ... wut does that mean in this case?
AcE
In this case its returning a value between 0 and 100% ....
because the category name is % Processor Time
so heres wut i got when i put it in a textbox
The Processor Time: 210510499232
..... how u get that number to a number from 1-100???
im sorry if i seem blind.....i usually have no problem with these type things....
AcE
Yea I know, for some reason, the first value (the first sampling of the value ) is always some messed up number...
Maybe its a bug.... just read it once when after its initialized and throw away that value...
heres something else thats interesting to me......the workingset property can only be accessed 1 time per start of the process.....
well it is definity a time based number bc it gets larger everytime i hit my button.....
edit: nm got that figured out...u got to run the process.refresh call before u call process.workingset....still dont no about this time number tho
ok.....so % Processor Time isnt the right thing then......we need a Load,Usage, or something to that affect but i havent seen one of these all mornin.....no wait...its afternoon now...lol
AcE
Well, if you use .NextValue, you won't get that weird number... supposedly NextValue does some calculation that RawValue does not.... in any event, .NextValue solves the problem.
nope that aint it....already tryed it.....and just tryed again...it gives me 0 all the time
Well, it worked for me... the counter must persist outside the sub, so it must be declared at the form level, and then when you click on a button, run code to read the NextValue....
Anyway, I wrote something that will allow you to find out what categories are available ... its rather crude... there is a Process counter in there....
VB Code:
Dim f As PerformanceCounterCategory Dim g As Int32 Dim t As String Dim r As Array r = System.Diagnostics.PerformanceCounterCategory.GetCategories() For g = 0 To r.GetUpperBound(0) f = (r.GetValue(g)) Console.WriteLine(f.CategoryName()) Next
k that wuz the prob....i didnt have it declared outside the sub.....but now in the mist of all this i got another problem....i got an over flow ever when my progressbar value for my memory goes over 10%....wuts gonna be the best way to trouble shoot this?....bc it dont tell me where the problem is.....
AcE
An unhandled exception of the type'System.OverflowException' occurred in system.windows.forms.dll
Additional information: Arithmetic operation resulted in an overflow.
The progressbar probably takes a Int32, a type that the value returned by .NextValue doesn't fit into... you'll have to do truncate the numbers after the decimal... 5.3423423423 ....
actually, performancecounter returns an Int64.... so it obviously won't fit...
naw this is seperate.....im doing
cprogressbar1.refresh
cprogressbar1.value = newprocess.workset
and it works fine in a textbox so i put it in the progressbar and on 9% everytime.....boom...error....
AcE
Yea well a textbox automatically converts the int64 into a string...and a string can hold as much data as your computer can... but an int32 which is the data type used by the progressbar.Value property can only use a 4-byte value... which an Int64 is not...
use the convert class ... and make sure to have Option Strict checked in your IDE configuration so the compiler will warn you about these things...
i commented the cpu calls out then used the convert call on the memory usage and still got the same error @ 10%
post ur code
ok.....finally got that fixed....im using a custom progressbar control and the problem wuz with the drawing of it....i just set it up to figure the percent before it got to the progressbar so it didnt get bombarded with crazy numbers.....
next thing....do u think there is a _Average instancename or something to that affect....i tryed _Average but that doesnt work....reason im asking is because my processbar is jumping back and forth between 0 & 100 ... when the windows taskmanager says like 4 - 8 %.....
AcE
edit: nm.....changed some settings on the progressbar and all is well....this darn thing has been giving me fits all day....thx for ur help!
The taskmanager only polls from 1 second to every 5 seconds...(depending on the polling speed you set)
Whereas a PerformanceCounter in a loop could be high as 100% CPU usage, but by the time the taskmanager gets around to reading the CPU load, it could have gone down to 4 or 1 %...