Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsVB SyntaxEnterprise DevelopmentDatabase AccessControlsCOMWin APICrystal ReportDeploymentGeneralGeneral 2
Related Topics
VB.NET / ASP.NETMS SQL ServerMS AccessOther Database ProductsMore Topics ...

VB Forum / Win API / August 2007



Tip: Looking for answers? Try searching our database.

CPU usage from process id

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
JohnnyC - 20 Aug 2007 20:43 GMT
VB6
.
Is there a very straightforward way to get the cpu usage value if I know the
ProcessID?  I have the PID, but need to CPU usage value.
.
I have seen multiple ways to do this, but am looking for a very direct
method.
Thank you.
Ed - 20 Aug 2007 23:20 GMT
>VB6
>.
[quoted text clipped - 4 lines]
>method.
>Thank you.

Heres one,
http://files.codes-sources.com/fichier.aspx?id=42392&f=ProcessCPU%5cclsProcessCp
uUsage.cls

JohnnyC - 21 Aug 2007 01:35 GMT
This is perfect, Ed.  Exactly what I was looking for.  Thank you very
much.-John

>>VB6
>>.
[quoted text clipped - 8 lines]
> Heres one,
> http://files.codes-sources.com/fichier.aspx?id=42392&f=ProcessCPU%5cclsProcessCp
uUsage.cls
Ed - 21 Aug 2007 22:20 GMT
>This is perfect, Ed.  Exactly what I was looking for.  Thank you very
>much.-John

One thing I noticed with this class (under XP Pro) is the Function
GetCpuUsage can sometimes return an overflow error, so what I do is just
re-initalize the counters in the error trap.

ErrExit:
GetCpuUsage = -1
InitialiseCounter

hth,
Ed
JohnnyC - 22 Aug 2007 14:52 GMT
I have been running this code on both an XP pro and 2003 server.  Both
machines are giving me a zero cpu usage value for every process.
.
I haven't been getting the overflow error though.
Thanks.

>>This is perfect, Ed.  Exactly what I was looking for.  Thank you very
>>much.-John
[quoted text clipped - 9 lines]
> hth,
> Ed
JohnnyC - 22 Aug 2007 17:49 GMT
OK, i got it.  I had to download the entire project from the site you
recommended Ed.
.
The trick is keeping the cpuprocess class in memory for each process you
want to track.  You do the initialize of the class, pass it a processid, but
do not destroy the object.  When you want the CPU % value, just ask for it
using the proper instantiated class for the unique PID.
.
Works great.  Thanks again.

>I have been running this code on both an XP pro and 2003 server.  Both
>machines are giving me a zero cpu usage value for every process.
[quoted text clipped - 15 lines]
>> hth,
>> Ed
Ed - 23 Aug 2007 08:41 GMT
>OK, i got it.  I had to download the entire project from the site you
>recommended Ed.
[quoted text clipped - 5 lines]
>.
>Works great.  Thanks again.

Ya it seems to work really well with my dual core unlike most routines
I've tried. Here's sort of the way I use it for a list of running
programs,...

'Gen. Declare
Dim CpuUseList(100) As ClsProcCpuUsage ' 100 files max for testing

'Form Load
For I = 1 To 100
Set CpuUseList(I) = New ClsProcCpuUsage
Next I

' Sub Get PIDs
' PID = .ProcessID
If i < 101 Then
CpuUseList(i).pid = PID
Cpu_Usage_For_This_PID = Format(CpuUseList(i).GetCpuUsage, "##0.00")
End If

' my update for the CLASS
Public Property Let pid(ByVal ID As Long)
'only initialize if new PID
If ProcessID = ID Then Exit Property
ProcessID = ID
InitialiseCounter
End Property

' Unload/Kill
For i = 1 To 100
Set CpuUseList(i) = Nothing
Next i
Erase CpuUseList()

Cheers,
Ed
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.