Reputation: 85
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.
Upvotes: 1
Views: 2150
Reputation: 2685
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
Upvotes: 2
Reputation: 11773
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.
Upvotes: 1