在 C# 中使用性能计数器获取给定进程的结果中的 0 字节
Getting 0 bytes in results for the given Process using performance counter in c#
我正在创建一个 C# WinForm 应用程序,它将显示给定进程(例如:进程名称的 chrome.exe
)的网络 Activity(接收的字节数/发送的字节数)和速度Megabyte/s 由进程生成。
我的应用程序使用 Performance Counter Class 来获取进程活动,例如 IO Read Bytes/sec
用于接收字节,IO Writes Bytes/sec
用于发送字节。但是,它给了我 0 Bytes 结果,这很奇怪,因为 chrome.exe
是 运行 并且它肯定使用一些字节数据。
我试图找到解决方案的研究是:
- C# Resource Monitor get network activity values
- https://www.c-sharpcorner.com/forums/i-want-to-develop-resource-monitor-desktop-application
这是我正在使用的一些代码:
PerformanceCounter PC = new PerformanceCounter();
PC.CategoryName = "Process";
PC.CounterName = "IO Read Bytes/sec";
PC.InstanceName = "chrome";
PC.ReadOnly = true;
Console.WriteLine("Bytes Receieved: " + Math.Round(PC.NextValue()));
PC.CounterName = "IO Write Bytes/sec";
Console.WriteLine("Bytes Sent: " + Math.Round(PC.NextValue()));
结果:
Bytes Received: 0
Bytes Sent: 0
根据 documentation:
If the calculated value of a counter depends on two counter reads, the first read operation returns 0.0. Resetting the performance counter properties to specify a different counter is equivalent to creating a new performance counter, and the first read operation using the new properties returns 0.0. The recommended delay time between calls to the NextValue method is one second, to allow the counter to perform the next incremental read.
我正在创建一个 C# WinForm 应用程序,它将显示给定进程(例如:进程名称的 chrome.exe
)的网络 Activity(接收的字节数/发送的字节数)和速度Megabyte/s 由进程生成。
我的应用程序使用 Performance Counter Class 来获取进程活动,例如 IO Read Bytes/sec
用于接收字节,IO Writes Bytes/sec
用于发送字节。但是,它给了我 0 Bytes 结果,这很奇怪,因为 chrome.exe
是 运行 并且它肯定使用一些字节数据。
我试图找到解决方案的研究是:
- C# Resource Monitor get network activity values
- https://www.c-sharpcorner.com/forums/i-want-to-develop-resource-monitor-desktop-application
这是我正在使用的一些代码:
PerformanceCounter PC = new PerformanceCounter();
PC.CategoryName = "Process";
PC.CounterName = "IO Read Bytes/sec";
PC.InstanceName = "chrome";
PC.ReadOnly = true;
Console.WriteLine("Bytes Receieved: " + Math.Round(PC.NextValue()));
PC.CounterName = "IO Write Bytes/sec";
Console.WriteLine("Bytes Sent: " + Math.Round(PC.NextValue()));
结果:
Bytes Received: 0
Bytes Sent: 0
根据 documentation:
If the calculated value of a counter depends on two counter reads, the first read operation returns 0.0. Resetting the performance counter properties to specify a different counter is equivalent to creating a new performance counter, and the first read operation using the new properties returns 0.0. The recommended delay time between calls to the NextValue method is one second, to allow the counter to perform the next incremental read.