JayWeb
Member
- Joined
- May 27, 2010
- Messages
- 7
- Programming Experience
- 1-3
I am trying to setup performance counters for CPU Usage and Memory Used.
I would like a progress bar and a label to display the percentage.
I have the below code and although it seems to be half working, there is something just not right.. The memory one seems to be working fine although the CPU one is acting strange. Can someone take a look and tell me what might be wrong.
I would like a progress bar and a label to display the percentage.
I have the below code and although it seems to be half working, there is something just not right.. The memory one seems to be working fine although the CPU one is acting strange. Can someone take a look and tell me what might be wrong.
VB.NET:
Shared perfTotalMemory As New ComputerInfo
Shared perfFreeMemory As New PerformanceCounter("Memory", "Available MBytes")
Shared perfProcessorTime As New PerformanceCounter("Processor", "% Processor Time", "_Total")
Private Sub Timer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer.Tick
Dim totalMemory As Double = perfTotalMemory.TotalPhysicalMemory / 1000000
Dim freeMemory As Double = (totalMemory - Int(perfFreeMemory.NextValue.ToString())) / totalMemory * 100
pbRAM.Maximum = totalMemory
pbCPU.Value = Int(perfProcessorTime.NextValue.ToString())
pbRAM.Value = totalMemory - Int(perfFreeMemory.NextValue.ToString())
lblCPU.Text = Format(Int(perfProcessorTime.NextValue.ToString()), "0.0") & "%"
lblRAM.Text = Format(freeMemory, "0.0") & "%"
End Sub