|
-
Apr 17th, 2003, 04:02 PM
#1
Thread Starter
Addicted Member
cpu & memory usage (solved)
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
Last edited by theonetrueace; Apr 18th, 2003 at 02:15 AM.
-
Apr 17th, 2003, 04:12 PM
#2
Fanatic Member
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.
-
Apr 17th, 2003, 04:14 PM
#3
Thread Starter
Addicted Member
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
-
Apr 17th, 2003, 05:16 PM
#4
Thread Starter
Addicted Member
process.workingset gives you the total memory usage....im still looking for cpu...
AcE
-
Apr 17th, 2003, 05:29 PM
#5
Fanatic Member
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.
Last edited by Slow_Learner; Apr 17th, 2003 at 05:33 PM.
-
Apr 17th, 2003, 05:36 PM
#6
Thread Starter
Addicted Member
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
-
Apr 17th, 2003, 05:57 PM
#7
I wonder how many charact
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
Last edited by nemaroller; Apr 17th, 2003 at 06:13 PM.
-
Apr 17th, 2003, 06:10 PM
#8
Thread Starter
Addicted Member
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
-
Apr 17th, 2003, 06:15 PM
#9
I wonder how many charact
In this case its returning a value between 0 and 100% ....
because the category name is % Processor Time
-
Apr 17th, 2003, 06:18 PM
#10
Thread Starter
Addicted Member
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
-
Apr 17th, 2003, 06:22 PM
#11
I wonder how many charact
Yea I know, for some reason, the first value (the first sampling of the value ) is always some messed up number...
-
Apr 17th, 2003, 06:31 PM
#12
I wonder how many charact
Maybe its a bug.... just read it once when after its initialized and throw away that value...
-
Apr 17th, 2003, 06:33 PM
#13
Thread Starter
Addicted Member
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
Last edited by theonetrueace; Apr 17th, 2003 at 06:42 PM.
-
Apr 17th, 2003, 06:45 PM
#14
Thread Starter
Addicted Member
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
-
Apr 17th, 2003, 06:49 PM
#15
I wonder how many charact
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.
-
Apr 17th, 2003, 06:51 PM
#16
Thread Starter
Addicted Member
nope that aint it....already tryed it.....and just tryed again...it gives me 0 all the time
-
Apr 17th, 2003, 07:17 PM
#17
I wonder how many charact
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
-
Apr 17th, 2003, 07:23 PM
#18
Thread Starter
Addicted Member
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
-
Apr 17th, 2003, 07:26 PM
#19
Thread Starter
Addicted Member
An unhandled exception of the type'System.OverflowException' occurred in system.windows.forms.dll
Additional information: Arithmetic operation resulted in an overflow.
-
Apr 17th, 2003, 07:26 PM
#20
I wonder how many charact
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...
-
Apr 17th, 2003, 07:32 PM
#21
Thread Starter
Addicted Member
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
-
Apr 17th, 2003, 07:37 PM
#22
I wonder how many charact
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...
Last edited by nemaroller; Apr 17th, 2003 at 07:41 PM.
-
Apr 17th, 2003, 07:42 PM
#23
Thread Starter
Addicted Member
i commented the cpu calls out then used the convert call on the memory usage and still got the same error @ 10%
-
Apr 17th, 2003, 09:47 PM
#24
I wonder how many charact
-
Apr 18th, 2003, 01:50 AM
#25
Thread Starter
Addicted Member
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!
Last edited by theonetrueace; Apr 18th, 2003 at 02:16 AM.
-
Apr 18th, 2003, 03:00 PM
#26
I wonder how many charact
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 %...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|