Results 1 to 6 of 6

Thread: VB6 gets the CPU utilization of the specified process.

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2020
    Posts
    4,180

    VB6 gets the CPU utilization of the specified process.

    Dim C As Currency
    C = CPUUSage(GetPidByHwnd(Me.hWnd))
    MSGBOX C & "%"

    Code:
     Function GetPidByHwnd(hWnd As Long, Optional lThread As Long)
         lThread = GetWindowThreadProcessId(hWnd, GetPidByHwnd)
     End Function
    
    Function CPUUSage(ProcID As Long) As Currency
     On Error Resume Next
     Dim n1, d1
     Dim n2, d2, nd, dd
        Dim objService, objInstance1
        Dim perf_instance2
        Dim PercentProcessorTime
     Set objService = GetObject("Winmgmts:{impersonationlevel=impersonate}!\Root\Cimv2")
        
     For Each objInstance1 In objService.ExecQuery("Select * from Win32_PerfRawData_PerfProc_Process where IDProcess = '" & ProcID & "'")
      n1 = objInstance1.PercentProcessorTime
      d1 = objInstance1.TimeStamp_Sys100NS
      Exit For
     Next
    
     For Each perf_instance2 In objService.ExecQuery("Select * from Win32_PerfRawData_PerfProc_Process where IDProcess = '" & ProcID & "'")
      n2 = perf_instance2.PercentProcessorTime
      d2 = perf_instance2.TimeStamp_Sys100NS
      Exit For
     Next
     ' CounterType - PERF_100NSEC_TIMER_INV
     ' Formula - (1- ((N2 - N1) / (D2 - D1))) x 100
     nd = (n2 - n1)
     dd = (d2 - d1)
     PercentProcessorTime = ((nd / dd)) * 100
           
     CPUUSage = Round(PercentProcessorTime, 2)
    End Function

  2. #2

    Thread Starter
    PowerPoster
    Join Date
    Jan 2020
    Posts
    4,180

    Re: VB6 gets the CPU utilization of the specified process.

    Using my software to detect the cpu utilization rate of this process is 60% on average. The task manager shows that this process only occupies 10% of the cpu, and the local cpu is idle 85%. But the total cpu utilization rate is 90%.
    My computer has turned on hyper-v virtual machine, so maybe it's more accurate to turn off virtualization, and turn on virtualization, and the whole process is in a mess?

    I am a 6-core cpu, and I have four threads running in an endless loop to perform a long operation.

    code from here:https://www.tek-tips.com/viewthread.cfm?qid=395765
    Last edited by xiaoyao; Jun 27th, 2024 at 12:57 AM.

  3. #3
    PowerPoster yereverluvinuncleber's Avatar
    Join Date
    Feb 2014
    Location
    Norfolk UK (inbred)
    Posts
    2,470

    Re: VB6 gets the CPU utilization of the specified process.

    WMI is notoriously unreliable at providing accurate and current CPU usage levels.

    Code:
    Private Declare Function GetSystemTimes Lib "kernel32" (lpIdleTime As Any, lpKernelTime As Any, lpUserTime As Any) As Long
    
    Private Function CPU_Usage_Percent() As Currency
        Dim idleTimeLive As Currency: idleTimeLive = 0
        Dim kernelTimeLive As Currency: kernelTimeLive = 0
        Dim usermodeTimeLive As Currency: usermodeTimeLive = 0
        Static idleTimeStored As Currency
        Static kernelTimeStored As Currency
        Static usermodeTimeStored As Currency
        
        On Error GoTo CPU_Usage_Percent_Error
    
        GetSystemTimes idleTimeLive, kernelTimeLive, usermodeTimeLive
        
        idleTimeStored = idleTimeLive - idleTimeStored
        kernelTimeStored = kernelTimeLive - kernelTimeStored
        usermodeTimeStored = usermodeTimeLive - usermodeTimeStored
        
        CPU_Usage_Percent = ((kernelTimeStored + usermodeTimeStored - idleTimeStored) / (kernelTimeStored + usermodeTimeStored)) * 100
        
        idleTimeStored = idleTimeLive
        kernelTimeStored = kernelTimeLive
        usermodeTimeStored = usermodeTimeLive
        
        On Error GoTo 0
        Exit Function
    
    CPU_Usage_Percent_Error:
    
        MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure CPU_Usage_Percent, line " & Erl & "."
    
    End Function
    https://github.com/yereverluvinunclebert

    Skillset: VMS,DOS,Windows Sysadmin from 1985, fault-tolerance, VaxCluster, Alpha,Sparc. DCL,QB,VBDOS- VB6,.NET, PHP,NODE.JS, Graphic Design, Project Manager, CMS, Quad Electronics. classic cars & m'bikes. Artist in water & oils. Historian.

    By the power invested in me, all the threads I start are battle free zones - no arguing about the benefits of VB6 over .NET here please. Happiness must reign.

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Jan 2020
    Posts
    4,180

    Re: VB6 gets the CPU utilization of the specified process.

    powershell,Python,They always have a lot of quick ways to get accurate numbers.

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Jan 2020
    Posts
    4,180

    Re: VB6 gets the CPU utilization of the specified process.

    Quote Originally Posted by yereverluvinuncleber View Post
    WMI is notoriously unreliable at providing accurate and current CPU usage levels.

    Code:
    Private Declare Function GetSystemTimes Lib "kernel32" (lpIdleTime As Any, lpKernelTime As Any, lpUserTime As Any) As Long
    
    Private Function CPU_Usage_Percent() As Currency
        Dim idleTimeLive As Currency: idleTimeLive = 0
        Dim kernelTimeLive As Currency: kernelTimeLive = 0
        Dim usermodeTimeLive As Currency: usermodeTimeLive = 0
        Static idleTimeStored As Currency
        Static kernelTimeStored As Currency
        Static usermodeTimeStored As Currency
        
        On Error GoTo CPU_Usage_Percent_Error
    
        GetSystemTimes idleTimeLive, kernelTimeLive, usermodeTimeLive
        
        idleTimeStored = idleTimeLive - idleTimeStored
        kernelTimeStored = kernelTimeLive - kernelTimeStored
        usermodeTimeStored = usermodeTimeLive - usermodeTimeStored
        
        CPU_Usage_Percent = ((kernelTimeStored + usermodeTimeStored - idleTimeStored) / (kernelTimeStored + usermodeTimeStored)) * 100
        
        idleTimeStored = idleTimeLive
        kernelTimeStored = kernelTimeLive
        usermodeTimeStored = usermodeTimeLive
        
        On Error GoTo 0
        Exit Function
    
    CPU_Usage_Percent_Error:
    
        MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure CPU_Usage_Percent, line " & Erl & "."
    
    End Function
    How do I get the exact CPU footprint of each process?

  6. #6
    PowerPoster yereverluvinuncleber's Avatar
    Join Date
    Feb 2014
    Location
    Norfolk UK (inbred)
    Posts
    2,470

    Re: VB6 gets the CPU utilization of the specified process.

    Not yet tried to do it. There is a task manager app in planet source code:

    https://github.com/Planet-Source-Cod...nager__1-70771

    But it does not look at CPU per process.
    Last edited by yereverluvinuncleber; Jun 27th, 2024 at 01:02 PM.
    https://github.com/yereverluvinunclebert

    Skillset: VMS,DOS,Windows Sysadmin from 1985, fault-tolerance, VaxCluster, Alpha,Sparc. DCL,QB,VBDOS- VB6,.NET, PHP,NODE.JS, Graphic Design, Project Manager, CMS, Quad Electronics. classic cars & m'bikes. Artist in water & oils. Historian.

    By the power invested in me, all the threads I start are battle free zones - no arguing about the benefits of VB6 over .NET here please. Happiness must reign.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width