性能计数器,用于获取进程的CPU和内存使用情况

问题描述 投票:0回答:1

首先,我正在研究Visual Basic With Ref。到这个预发布的线程What is the correct Performance Counter to get CPU and Memory Usage of a Process?

我想在标签或列表框中获取特定进程使用的cpu和内存,假设我想获得notepad.exe的处理器%时间

实际上我正在制作一个MYSQL工具,它连接到MYSQL,我想记录实际产生的CPU消耗量,以使整个过程发生。所以我需要添加几个特定进程的CPU和内存消耗,我可以在任务管理器中看到。

所以我想在代码中提供所有进程的名称并获得输出。

我需要在一个过程中使用性能计数器来解决这个问题,然后我可以知道。

非常感谢您提前提供任何帮助。

vb.net performancecounter
1个回答
0
投票

您可能需要考虑使用此:

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

cpuLabel.Text = cpu.NextValue()

请注意,如果对多个进程执行操作,则处理器本身会很快变得很重。如需替代方案,请查看How To Get Hardware Information。它在C#中,但应该相对容易地手动或使用this tool转换为VisualBasic.NET。

希望这可以帮助。

© www.soinside.com 2019 - 2024. All rights reserved.