Hello,
Need help with get real CPU, RAM usage of computer.
Using Timer to update the values.
To display in progressbar the value
To display in label.text = the %
CPU - Used % (like it shows in task manager) - This is for computer
Ram - Used / Total (like it shows in task manager) - This is for computer
+ another function option also to check for specific process CPU used and Ram Used (lets say in this function i put "Skype" and will check for process how much cpu and ram use)
Imports System.Management
Imports System.Threading
Public Class SystemWatcher
Public Shared Function TotalRam() As Integer
Dim totalRamuse As Integer = 0
Dim wql As New ObjectQuery("SELECT * FROM Win32_OperatingSystem")
Dim searcher As New ManagementObjectSearcher(wql)
Dim results As ManagementObjectCollection = searcher.[Get]()
Dim res As Double
For Each result As ManagementObject In results.Cast(Of ManagementObject)()
res = Convert.ToDouble(result("TotalVisibleMemorySize"))
Dim fres As Double = Math.Round((res / 1024.0R))
totalRamuse = Convert.ToInt32(fres)
Next
Return totalRamuse
End Function
Public Shared Function MachineCpuUtilization() As Integer
Dim cpuCounter = New PerformanceCounter("Processor", "% Processor Time", "_Total", Environment.MachineName)
cpuCounter.NextValue()
Thread.Sleep(1000)
Return CInt(cpuCounter.NextValue())
End Function
Public Shared Function CurentPcRamUsage() As Integer
Dim curentram As Integer = 0
Dim wql As New ObjectQuery("SELECT * FROM Win32_OperatingSystem")
Dim searcher As New ManagementObjectSearcher(wql)
Dim results As ManagementObjectCollection = searcher.[Get]()
Dim res As Double
For Each result As ManagementObject In results.Cast(Of ManagementObject)()
res = Convert.ToDouble(result("FreePhysicalMemory"))
Dim fres As Double = Math.Round(res / 1024.0R)
curentram = Convert.ToInt32(fres)
Next
Return curentram
End Function
Public Shared Function ApplicationRuning(ByVal ApplicationName As String) As EnumModels.ServerStatus
Dim pname As Process() = Process.GetProcessesByName(ApplicationName)
If pname.Length <= 0 Then
Return EnumModels.ServerStatus.NotRunning
Else
Return EnumModels.ServerStatus.Running
End If
End Function
Public Shared Function ApplicationKill(ByVal ApplicationName As String) As EnumModels.ServerStatus
For Each processuse In Process.GetProcessesByName(ApplicationName)
Try
processuse.Kill()
Return EnumModels.ServerStatus.NotRunning
Catch __unusedException1__ As Exception
Return EnumModels.ServerStatus.Running
End Try
Next
Return EnumModels.ServerStatus.Running
End Function
Public Shared Sub ApplicationStart(ByVal ApplicationName As String, ByVal HideWindw As Boolean)
Try
Using myProcess As New Process()
myProcess.StartInfo.UseShellExecute = False
myProcess.StartInfo.FileName = ApplicationName
If HideWindw = False Then
myProcess.StartInfo.CreateNoWindow = False
myProcess.Start()
ElseIf HideWindw = True Then
myProcess.StartInfo.CreateNoWindow = True
myProcess.Start()
End If
End Using
Catch ex As Exception
Data.Message = ex.Message
End Try
End Sub
Public Shared Function ApplicationRamUsage(ByVal ApplicationName As String) As Integer
If ApplicationRuning(ApplicationName) = EnumModels.ServerStatus.Running Then
Try
Dim PC As New PerformanceCounter With {
.CategoryName = "Process",
.CounterName = "Working Set - Private",
.InstanceName = ApplicationName
}
Dim memsize As Integer = Convert.ToInt32(PC.NextValue()) / CInt((1024 * 1024))
PC.Close()
PC.Dispose()
Return memsize
Catch
Return 0
End Try
End If
Return 0
End Function
End Class
Private Sub TimerWacher_Tick(ByVal sender As Object, ByVal e As EventArgs)
Try
Dim CurrentRamUsage As Integer = SystemWatcher.TotalRam() - SystemWatcher.CurentPcRamUsage()
Dim PCResorceUsageThread As New Thread(Sub()
PCResorcePbarRAM.Maximum = SystemWatcher.TotalRam()
PCLoginPbarRAM.Maximum = CurrentRamUsage
PCWorldPbarRAM.Maximum = CurrentRamUsage
PCWorldPbarRAM.Value = SystemWatcher.ApplicationRamUsage("")
PCResorcePbarRAM.Value = CurrentRamUsage
PCResorcePbarCPU.Value = SystemWatcher.MachineCpuUtilization()
End Sub)
PCResorceUsageThread.Start()
Catch
End Try
End Sub