Vb.net – Performance Counter to get CPU and Memory Usage of a Process

performancecountervb.net

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.

Best Answer

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.