Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

get the usage of the cpu? 3

Status
Not open for further replies.

Hannes214

IS-IT--Management
Jan 30, 2006
45
DE
Hi :)

I have an other problem too...
I need to know, how many the CPU is in use...
I don't want to enumerate the running processes.... only the usage of the CPU ... can someona help ???



 

This doesn't with "." for lcComputer... i tryed it als a simple *.prg file.. like that sample...and get that error...

IT-Management
( an FoxPro-Newbi :eek:) )
 
Hi Hannes,

This is rather a windows issue, than a foxpro issue, as it's about WMI. I'd guess you have unsufficient rights to get that WMI-Service object, but I know too little about it.

Perhaps ask about this error in some other tek-tips-group, too...

Bye, Olaf.
 
Hannes214

Rather than going trough gymnastics you figure out why WMI does does work, have you looked at the application, and figure out why it would take up 100% of the CPU.

Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
ReFox XI (www.mcrgsoftware.com)
 
Hannes,

If the VB DLL is right, that your Foxpro.exe uses 100% cpu time, the reason may be, that you run an infinite loop without ever issuing DOEVENTS, which let's the OS do some other tasks too.

For example this simple loop results in a very high cpu usage:
Code:
do while .t.
enddo
And vfp is not responsive anymore, during that loop.

High cpu usage also results from long running sql queries. You may issue the DOEVENTS command between queries, to make the system more responsive.

Bye, Olaf.
 
unsufficient rights for WMI-Service object can't be the problem.. i test it with an admin-acc...

and the vb-dll...
even if i want to use such code, i test it as a single prg-file.. no loop or events!... and no sql queries...

i test both codes on a win2k system with admin rights and at both time the pure code..

(even if the code works, i integrate it into my programs...

IT-Management
( an FoxPro-Newbi :eek:) )
 
Hi Hannes,

I thought as you said
Hannes214 said:
..and the programm always shows 100 % ...
you were talking about measuring cpu usage with the help of the VB dll. Measuring the cpu usage of your App.exe, which may have several long running queries or loops within.
So, what took up 100% cpu usage, then?

Bye, Olaf.
 
nothing... thats the problem ...

the task manager shows 99% at System Idle Process.....

IT-Management
( an FoxPro-Newbi :eek:) )
 
Hi Hannes,

If The System Idle Process has 99% cpu usage, then everything's fine, isn't it?

I thought your original problem was, that your EXE takes up 100% cpu usage for long periods of time and causes trouble for other processes. I thought you wanted to monitor cpu usage to see when and why that happens and with my example of an endless loop I just wanted to give hints what might cause unexpected high cpu usage. I didn't thought you put some endless loop around our code snippets.

Besides WMI not working for you, for which you might find a solution in another forum, I wonder what your initial problem really is?

Bye, Olaf.
 
VFP 6.0 can not use monikers.
Below the working code for VFP 6.0

LOCAL lcRemoteComputer, lcAdminUserName, lcAdminPassword, ;
loSWbemLocator, loSWbemServices
*lcAdminUserName="sysadmin_name"
*lcAdminPassword="password_sysadmina"
lcAdminUserName=""
lcAdminPassword=""
*lcRemoteComputer = "remote_computer_name"
lcRemoteComputer = "."

loSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
loSWbemServices = loSWbemLocator.ConnectServer(lcRemoteComputer, "root\cimv2",lcAdminUserName,lcAdminPassword)
lcolSWbemObjectSet = loSWbemServices.InstancesOf("Win32_Process")
FOR EACH loProcess In lcolSWbemObjectSet
? "Name "+ TRANSFORM(loProcess.Name)
? "Process ID: " + TRANS(loProcess.ProcessID)
? "CPU usage to date: " + ;
TRANSFORM((VAL(loProcess.KernelModeTime)+;
VAL(loProcess.UserModeTime)) / 10000000)
NEXT



Juri Shutenko
Municipality of Maardu, Estonia
 
Nice followup Juri. Code is forward compatible too.

Brian
 
Hi Juri,

I didn't know, that VFP6 can't use monikers. To be honest, I'm not even having a clear concept of what a moniker is.
This should help Hannes.

Nevertheless I also tested the VB.dll CPU Monitor mentioned by JimWWinter. It works okay, if used in a timer.

So here is some simple code, that works with that DLL:

Code:
Local loCPUMonitorTimer

loCPUMonitorTimer = Createobject("CpuMonitorTimer")
loCPUMonitorTimer.Enabled = .t.
On Shutdown Clear Events
Read Events
loCPUMonitorTimer = .null. 

Define Class CpuMonitorTimer As Timer
   Interval = 2000
   nCountDown = 5 && work for 5 intervals

   Procedure Init()
      This.AddProperty("oCPUMon",CreateObject("cpumon.cpuuse"))
   Endproc

   Procedure Timer()
      ? This.oCPUMon.cpuuse()
      This.nCountDown = This.nCountDown -1
      If This.nCountDown = 0
         Clear Events 
      EndIf 
   Endproc
Enddefine

The only disadvantage is, you can't monitor if a certain process uses more than 90% of the cpu time, you only get the overall cpu usage.

Bye, Olaf.
 
A Big Thanks to Juris

.. now i know, why the other codes don't work...

now i can realize my programm .. Thanks

IT-Management
( an FoxPro-Newbi :eek:) )
 
Ähm...yes..
and i know ...

but i'm an IT-Management an no Programmer, so i realy don't need to be a realy good specialist in VFP..

I only use the possibility that we use VFP in our company to program little applications for my work ...

IT-Management
( FoxPro-Newbi :eek:) )
 
Finally, here you are: A Performance monitoring timer, monitoring cpu usage of a single definable process, that should also work with VFP6:

Code:
#Define ccMonitoredProcess "vfp6"
#Define ccMonitoredComputer "."
#Define ccAdminUsername ""
#Define ccAdminPassword ""

Local loCPUMonitorTimer
loCPUMonitorTimer = Createobject("CpuMonitorTimer",;
   ccMonitoredProcess,ccMonitoredComputer,ccAdminUsername,ccAdminPassword)
On Shutdown Clear Events
Read Events
On Shutdown
loCPUMonitorTimer = .Null. 


Define Class CpuMonitorTimer As Timer
   Interval = 1000

   *#Define cnIntervals 5
   #IfDef cnIntervals
      nCountDown = cnIntervals && work for several intervals only
   #Endif

   Procedure Init()
      Lparameters tcMonitoredProcess, tcMonitoredComputer, tcAdminUsername, tcAdminPassword

      If Vartype(tcMonitoredProcess)#"C"
         Return .f.
      EndIf 
      tcMonitoredComputer = IIf(Empty(tcMonitoredComputer), ".", tcMonitoredComputer)
      tcAdminUsername     = IIf(Empty(tcAdminUsername)    , "" , tcAdminUsername)
      tcAdminPassword     = IIf(Empty(tcAdminPassword)    , "" , tcAdminPassword)

      Local loSWbemLocator, loSWbemServices

      loSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
      loSWbemServices = loSWbemLocator.ConnectServer(tcMonitoredComputer,"root\cimv2",;
         tcAdminUsername, tcAdminPassword)
      
      With This
         .AddProperty([oPerfProcess],loSWbemServices.Get(;
         [Win32_PerfFormattedData_PerfProc_Process.Name=']+tcMonitoredProcess+[']))
         .oPerfProcess.Refresh_
      EndWith
   Endproc

   Procedure Timer()
      With This
         .oPerfProcess.Refresh_     
         Set Message To "CPU usage: "+Transform(.oPerfProcess.PercentProcessorTime)+"%"

         #IfDef cnIntervals
            .nCountDown = .nCountDown -1
            If .nCountDown = 0
               Clear Events 
            EndIf 
         #Endif
      Endwith
   Endproc

   Procedure Destroy()
      This.oPerfProcess = .null.
   Endproc 
Enddefine

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top