中的性能计数器获取给定流程的结果中的0个字节

中的性能计数器获取给定流程的结果中的0个字节

本文介绍了使用C#中的性能计数器获取给定流程的结果中的0个字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个C#WinForm应用程序,该应用程序将显示给定进程的网络活动(接收的字节数/发送的字节数)(例如:进程名称的chrome.exe)以及该进程生成的兆字节/秒的速度.

I'm creating a C# WinForm Application which will display Network Activity (Bytes Received / Bytes Sent) for a given Process (For Example: Process name 's chrome.exe) and Speed in Megabyte/s generated by the Process.

我的应用程序使用性能计数器类,用于获取Process活动,例如IO Read Bytes/sec用于接收字节,IO Writes Bytes/sec用于发送字节.但是,结果是给我 0 字节,这很奇怪,因为chrome.exe正在运行,并且肯定使用了一些字节数据.

My application uses Performance Counter Class to get the Process activities like IO Read Bytes/sec for received bytes and IO Writes Bytes/sec for sent bytes. But, it's giving me 0 Bytes as a result, which's very weird because chrome.exe is running and its definitely using some bytes data.

我试图找到解决方案的研究是:

Researches that I've tried to find the solution are:

  • https://stackoverflow.com/a/17026417/5377037
  • C# Resource Monitor get network activity values
  • https://www.c-sharpcorner.com/forums/i-want-to-develop-resource-monitor-desktop-application

这是我正在使用的一些代码:

Here is some code that I'm using :

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

推荐答案

根据文档:

这篇关于使用C#中的性能计数器获取给定流程的结果中的0个字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 05:52