vb.net 性能计数器获取进程的 CPU 和内存使用情况
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21907364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Performance Counter to get CPU and Memory Usage of a Process
提问by user3332560
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?
首先,我正在使用 Ref 处理 Visual Basic。到这个预先发布的线程获取进程的 CPU 和内存使用情况的正确性能计数器是什么?
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
我想在标签或列表框中获取特定进程使用的 cpu 和内存,假设我想获取 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.
实际上,我正在制作一个连接到 MYSQL 的面向 MYSQL 的工具,我想记录实际导致整个过程发生的 CPU 消耗量。所以我需要添加我可以在任务管理器中看到的几个特定进程的 CPU 和内存消耗。
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.
非常感谢您提前提供任何帮助。
回答by Insomniac
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.
请注意,如果为多个进程执行此操作,这会很快对处理器本身造成很大负担。另一种方法是查看如何获取硬件信息。它在 C# 中,但手动或使用此工具转换为 VisualBasic.NET 应该相对容易。
Hope this helps.
希望这可以帮助。

