Rickets Posted June 27, 2015 Report Posted June 27, 2015 Salut,Vreau sa arat utilizatorului cat % load la CPU ii face aplicatia mea, dar nu am gasit o solutie care sa functioneze.Am incercat:vb.net - How to get CPU usage? - Stack Overflow ----> e peste tot asta, am inlocuit InstanceName cu Process.GetCurrentProcess().ProcessName dar zice ca procesul nu exista...Individual Process CPU usage (current process) ----> asta returneaza 0 orice as faceAstept sfaturile voastre. Quote
Byte-ul Posted June 27, 2015 Report Posted June 27, 2015 Incearca sa nu rulezi din debugger. Poate afecteaza. Quote
Active Members MrGrj Posted June 27, 2015 Active Members Report Posted June 27, 2015 Salut,Vreau sa arat utilizatorului cat % load la CPU ii face aplicatia mea, dar nu am gasit o solutie care sa functioneze.Am incercat:vb.net - How to get CPU usage? - Stack Overflow ----> e peste tot asta, am inlocuit InstanceName cu Process.GetCurrentProcess().ProcessName dar zice ca procesul nu exista...Individual Process CPU usage (current process) ----> asta returneaza 0 orice as faceAstept sfaturile voastre.Incearca asa:Public Shared Function GetProcessInstanceName(ByVal PID As Integer) As String Dim cat As New PerformanceCounterCategory("Process") Dim instances() = cat.GetInstanceNames() For Each instance In instances Using cnt As PerformanceCounter = New PerformanceCounter("Process", "ID Process", instance, True) Dim val As Integer = CType(cnt.RawValue, Int32) If val = PID Then Return instance End If End Using NextEnd Function Quote
Rickets Posted June 27, 2015 Author Report Posted June 27, 2015 Incearca sa nu rulezi din debugger. Poate afecteaza.Dim cpu As New PerformanceCounter() With cpu .CategoryName = "Processor" .CounterName = "% Processor Time" .InstanceName = Process.GetCurrentProcess().ProcessName End WithTextBox15.Text = [B]cpu.NextValue()[/B] Primesc eroare ca procesul nu exista in categoria specificata, oricum l-as rula.Incearca asa:Public Shared Function GetProcessInstanceName(ByVal PID As Integer) As String Dim cat As New PerformanceCounterCategory("Process") Dim instances() = cat.GetInstanceNames() For Each instance In instances Using cnt As PerformanceCounter = New PerformanceCounter("Process", "ID Process", instance, True) Dim val As Integer = CType(cnt.RawValue, Int32) If val = PID Then Return instance End If End Using NextEnd FunctionDim cpu As New PerformanceCounter() With cpu .CategoryName = "Processor" .CounterName = "% Processor Time" .InstanceName = GetProcessInstanceName(Nothing) End With TextBox15.Text = cpu.NextValue()Public Shared Function GetProcessInstanceName(ByVal PID As Integer) As String Dim cat As New PerformanceCounterCategory("Process") Dim instances() = cat.GetInstanceNames() For Each instance In instances Using cnt As PerformanceCounter = New PerformanceCounter("Process", "ID Process", instance, True) Dim val As Integer = CType(cnt.RawValue, Int32) If val = PID Then Return instance End If End Using Next End Function0 tot timpul. Quote
Byte-ul Posted June 27, 2015 Report Posted June 27, 2015 Public Class Form1 Private ReadOnly _ _performanceCounter As _ New PerformanceCounter("Process", "% Processor Time", GetProcessInstanceName(Process.GetCurrentProcess.Id)) Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Timer1.Start() End Sub Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick Dim asd = CInt(_performanceCounter.NextValue() / Environment.ProcessorCount).ToString & "%" Label1.Text = asd End Sub Private Shared Function GetProcessInstanceName(pid As Integer) As String Dim cat As New PerformanceCounterCategory("Process") Dim instances As String() = cat.GetInstanceNames() For Each instance As String In instances Using cnt As New PerformanceCounter("Process", "ID Process", instance, True) Dim val = CInt(cnt.RawValue) If val = pid Then Return instance End If End Using Next Throw _ New Exception( "Could not find performance counter " + "instance name for current process. This is truly strange ...") End FunctionEnd Classasta merge. Quote