Hallöle,
ich hab mir ausm Netz 2 Codes besorgt um die Aktuelle CPU Auslastung auszulesen (1x Per WMI, 1x per Performance-Counter):
Die WMI Methode
Die Performance.Counter Methode
So, funzen beide astrein, aber:
Ich habe einen Quadcore, heist die Zeigen die Gesamtauslastung an. Ich brauche nun aber für Jeden kern einzelnd einen Wert.
Weis jemand wie das Geht?
MfG
ich hab mir ausm Netz 2 Codes besorgt um die Aktuelle CPU Auslastung auszulesen (1x Per WMI, 1x per Performance-Counter):
VB.NET-Quellcode
- Function GetCPULoad() As Long
- '// -----------------------------------------------------
- '// Methode: | Mithilfe des WMI die aktuelle CPU-
- '// | auslastung in Prozent ermitteln
- '// -----------------------------------------------------
- '// Parameter: | -
- '// -----------------------------------------------------
- '// Rückgabe: | Ermittelte CPU-Auslastung
- '// -----------------------------------------------------
- '// Autor: | Stefan Kulpa
- '// | EDV Innovation & Consulting - Dormagen
- '// -----------------------------------------------------
- On Error GoTo Err_GetCPULoad
- Dim objCPUSet As Object
- Dim objCPUItem As Object
- Dim lPercent As Long
- GetCPULoad = -1
- objCPUSet = _
- GetObject("winmgmts:").InstancesOf("Win32_Processor")
- For Each objCPUItem In objCPUSet
- If objCPUSet.Count > 1 Then
- lPercent = _
- lPercent + objCPUItem.LoadPercentage
- Else : lPercent = objCPUItem.LoadPercentage
- End If
- Next
- If objCPUSet.Count > 1 Then _
- lPercent = _
- lPercent \ objCPUSet.Count
- GetCPULoad = lPercent
- Exit_GetCPULoad:
- On Error Resume Next
- objCPUSet = Nothing
- objCPUItem = Nothing
- Exit Function
- Err_GetCPULoad:
- 'MsgBox("Error reading CPU Usage!" & vbCrLf & _
- ' Err.Number & ": " & _
- ' Err.Description, vbExclamation)
- GoTo Exit_GetCPULoad
- End Function
Die WMI Methode
VB.NET-Quellcode
- 'Beispiel: VB .Net - PerformanceCounter - CPU Auslastung ermitteln - 2
- '
- Option Explicit On
- Option Strict On
- Public Class Form1
- Private [PerformanceCounter] As New PerformanceCounter
- Private utilization As Byte
- Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
- Handles MyBase.Load
- With Me.Timer1
- .Interval = 1000
- .Enabled = True
- End With
- Call Me.InitPerformanceCounter("Processor", "% Processor Time", "_Total")
- End Sub
- Private Sub InitPerformanceCounter(ByVal CategoryName As String, _
- ByVal CounterName As String, _
- ByVal InstanceName As String)
- With [PerformanceCounter]
- .CategoryName = CategoryName
- .CounterName = CounterName
- .InstanceName = InstanceName
- End With
- End Sub
- Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) _
- Handles Timer1.Tick
- Me.utilization = CByte(([PerformanceCounter].NextValue()))
- Me.Label1.Text = CStr(Me.utilization) & "%"
- End Sub
- End Class
Die Performance.Counter Methode
So, funzen beide astrein, aber:
Ich habe einen Quadcore, heist die Zeigen die Gesamtauslastung an. Ich brauche nun aber für Jeden kern einzelnd einen Wert.
Weis jemand wie das Geht?
MfG