Is it possible to get the CPU Usage of the own Program that you are running in VB.Net? I would like to add a CPU detection, in case the CPU of the Program is above for example 10% it increases a timer deleay to reduce the CPU.
            Asked
            
        
        
            Active
            
        
            Viewed 1,674 times
        
    1
            
            
        - 
                    Look at [this other question](https://stackoverflow.com/q/4679962/2557263). While it's for C# and for an arbitrary process, it's trivial to convert its answers to VB.NET and to your own process. – Alejandro Aug 28 '20 at 14:05
2 Answers
2
            You can use something like this.
There is a mix of informations had by Process, PerformanceCounter and Computer.
Putting together those informations you can get information by your process in relation con SO memory usage.
Also by Process you can get other informations about your app like max cpu usage, time usage etc.
    Using currentP As Process = Process.GetCurrentProcess
        Dim bFactor As Double = 1000 / 1024
        Dim cMemory As Long = New PerformanceCounter("Process", "Working Set - Private", currentP.ProcessName).RawValue
        Dim sbInfo As StringBuilder = New StringBuilder
        sbInfo.AppendLine("Current ProcessName    : " & currentP.ProcessName)
        sbInfo.AppendLine("Current WorkingSet     : " & (cMemory * bFactor * 0.000001).ToString & " MB ")
        sbInfo.AppendLine("In a total system PM   : " & (My.Computer.Info.TotalPhysicalMemory * bFactor * 0.000001).ToString & " MB ")
        sbInfo.AppendLine("Percentage of all      : " & ((cMemory / My.Computer.Info.TotalPhysicalMemory) * 0.01).ToString("N6"))
        'MsgBox(sbInfo.ToString)
        Console.WriteLine(sbInfo.ToString)
    End Using
 
    
    
        G3nt_M3caj
        
- 2,497
- 1
- 14
- 16
1
            
            
        First declare a PerformanceCounter
Private CPUPerf As New PerformanceCounter()
then initialize it to get CPU info
    With CPUPerf
        .CategoryName = "Processor"
        .CounterName = "% Processor Time"
        .InstanceName = "_Total"
    End With
then you can access the value
    CPUPerf.NextValue
See PerformanceCounter for more information.
 
    
    
        dbasnett
        
- 11,334
- 2
- 25
- 33
- 
                    So I would use 'MessageBox.Show(CPUPerf.NextValue) to show the CPU Usage of the Program? It says 0 to me but the Program has a CPU Usage of 4.2%. – Baseult Private Aug 28 '20 at 15:18
- 
                    Sure. Measuring usage is not precise and will be based on when the sample is taken. – dbasnett Aug 28 '20 at 16:26
