-1

Firstly, I'm working on Visual Basic With Ref. to this pre-posted thread What is the correct Performance Counter to get CPU and Memory Usage of a Process?

I wanted to get the cpu and memory used by a particular process in a label or listbox, let's say i want to get the processor % time of notepad.exe

Actually i am making a MYSQL Oriented tool that connects to MYSQL and i want to record how much CPU Consumption is actually caused to make the whole process happen. So i need to add the CPU and Memory consumptions of few particular processes concerned that i can see in task Manager.

So i want to supply the names of all the processes in code and get the output.

I need to figure this out using performance counter for one process and rest i can make out.

Thanks a lot for any help in advance.

Community
  • 1
  • 1
user3332560
  • 1
  • 3
  • 4

2 Answers2

0

You migh want to consider using this:

Dim cpu as New System.Diagnostics.PerformanceCounter()
With cpu
    .CategoryName = "Processor"
    .CounterName = "% Processor Time"
    .InstanceName = "MyProcess"
End With

cpuLabel.Text = cpu.NextValue()

Note that this can quickly become quite heavy on the processor itself if doen for multiple processes. For an alternative take a look at How To Get Hardware Information. Its in C# but should be relatively easy to convert to VisualBasic.NET either manually or using this tool.

Hope this helps.

Insomniac
  • 446
  • 7
  • 15
-1

Since the topic is about VB.NET, I can direct you outside of StackOverflow to VBForums, on this thread.

Be aware of tha VS2019 and newer PerformanceCounters does not cover CategoryName, CounterName and InstantName as comboboxes anymore, intentionally. You have to code instead.

For a basic CPU/RAM meter - which supports for almost all type of machines - you may use the following:

Public Class Form1
Private pCPU As New PerformanceCounter
Private pRAM As New PerformanceCounter
Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Try
        pCPU.CategoryName = "Processor"
        pCPU.CounterName = "% Processor Time"
        pCPU.InstanceName = "_Total"
        pRAM.CategoryName = "Memory"
        pRAM.CounterName = "% Committed Bytes In Use"
        pRAM.InstanceName = ""
    Catch ex As Exception
        MsgBox(ex.Message, MsgBoxStyle.Critical)
    End Try
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    Try 'RAM
        Dim RAMpercent As Single = pRAM.NextValue
        Label1.Text = RAMpercent.ToString
    Catch ex As Exception
        Label1.Text = "ERROR"
    End Try
    Try 'CPU
        Dim CPUpercent As Single = pCPU.NextValue
        Label2.Text = CPUpercent.ToString
    Catch ex As Exception
        Label2.Text = "ERROR"
    End Try
End Sub

End Class