hi im making a task manager and want to get cpu usage of a process


this is how my code looks now

For Each proc As System.Diagnostics.Process In pList
            Dim lstStuff As ListViewItem = New ListViewItem()
            lstStuff.Text = proc.Id.ToString
            lstStuff.SubItems.Add(proc.ProcessName)
            Try
                lstStuff.SubItems.Add(proc.TotalProcessorTime.Milliseconds.ToString + "%")
            Catch

            End Try
            lstStuff.SubItems.Add(proc.WorkingSet64.ToString())
            lstStuff.SubItems.Add(proc.Responding.ToString)
            ListView1.Items.Add(lstStuff)
        Next

everything works but the cpu part it returns values over 100%

Are you using a 32 or 64 processor?

lstStuff.SubItems.Add(proc.WorkingSet64.ToString())

??

oh 64 bits but that dosent have anything whit it to do

proc.WorkingSet64.ToString()

is the mem usage remove 64 if the code is 32 bits

proc.TotalProcessorTime.Milliseconds.ToString + "%"

is the cpu part

You need to use a performance counter. Try the following. This will load the processes to a listbox -

Private m_PerformanceCounter As New _
    System.Diagnostics.PerformanceCounter( _
        "Processor", "% Processor Time", "_Total")

Private Sub btnClear_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnClear.Click
    lstCpu.Items.Clear()
End Sub

Private Sub tmrCheckCpu_Tick(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles tmrCheckCpu.Tick
    lstCpu.Items.Add(CInt(m_PerformanceCounter.NextValue()) _
        & "%")
    lstCpu.SelectedIndex = lstCpu.Items.Count - 1
End Sub

Or you can do it as in -

Imports System.Diagnostics

Dim oPerf1 As New PerformanceCounter

oPerf1.CategoryName = "Processor"
oPerf1.CounterName = "% Processor Time"
oPerf1.InstanceName = "0"

Dim I As Integer
For I = 0 To 100
SomeListBox.Items.Add (oPerf1.NextValue)
Threading.Thread.Sleep (20)
Next
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.