Jump to content
Rickets

[VB.net]CPU load-ul procesului deschis de aplicatia mea

Recommended Posts

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 face

Astept sfaturile voastre.

Link to comment
Share on other sites

  • Active Members
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 face

Astept 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
Next
End Function

Link to comment
Share on other sites

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 With

TextBox15.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
Next
End Function

Dim 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 Function

0 tot timpul.

Link to comment
Share on other sites

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 Function

End Class

asta merge.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...