如何在 C# 中显示每秒接收的字节数
How to display bytes received per second in C#
我正在尝试显示每秒接收到的字节数,并让它们每秒在控制台中显示一次。一旦达到 .nextvalue,我就会收到无效操作异常。
PerformanceCounter NetworkDownSpeed = new PerformanceCounter("Network Interface", "Bytes Received/sec");
float CurrentNetworkDownSpeed = (int)NetworkDownSpeed.NextValue();
while (true)
{
Console.WriteLine("Current Network Download Speed: {0}MB", CurrentNetworkDownSpeed);
Thread.Sleep(1000);
}
NetworkDownSpeed.NextValue() 可以抛出 InvalidOperationException。
在 NetworkDownSpeed.NextValue 中有一条评论详细说明了原因。
// If the category does not exist, create the category and exit.
// Performance counters should not be created and immediately used.
// There is a latency time to enable the counters, they should be created
// prior to executing the application that uses the counters.
// Execute this sample a second time to use the category.
可以在此 stack overflow post.
上找到解决此问题的替代解决方案
很抱歉在有人回答后回答,但这是否有帮助?
private static void ShowNetworkTraffic()
{
PerformanceCounterCategory performanceCounterCategory = new PerformanceCounterCategory("Network Interface");
string instance = performanceCounterCategory.GetInstanceNames()[0]; // 1st NIC !
PerformanceCounter performanceCounterSent = new PerformanceCounter("Network Interface", "Bytes Sent/sec", instance);
PerformanceCounter performanceCounterReceived = new PerformanceCounter("Network Interface", "Bytes Received/sec", instance);
for (int i = 0; i < 10; i++)
{
Console.WriteLine("bytes sent: {0}k\tbytes received: {1}k", performanceCounterSent.NextValue() / 1024, performanceCounterReceived.NextValue() / 1024);
Thread.Sleep(500);
}
}
来自 here.
我正在尝试显示每秒接收到的字节数,并让它们每秒在控制台中显示一次。一旦达到 .nextvalue,我就会收到无效操作异常。
PerformanceCounter NetworkDownSpeed = new PerformanceCounter("Network Interface", "Bytes Received/sec");
float CurrentNetworkDownSpeed = (int)NetworkDownSpeed.NextValue();
while (true)
{
Console.WriteLine("Current Network Download Speed: {0}MB", CurrentNetworkDownSpeed);
Thread.Sleep(1000);
}
NetworkDownSpeed.NextValue() 可以抛出 InvalidOperationException。
在 NetworkDownSpeed.NextValue 中有一条评论详细说明了原因。
// If the category does not exist, create the category and exit.
// Performance counters should not be created and immediately used.
// There is a latency time to enable the counters, they should be created
// prior to executing the application that uses the counters.
// Execute this sample a second time to use the category.
可以在此 stack overflow post.
上找到解决此问题的替代解决方案很抱歉在有人回答后回答,但这是否有帮助?
private static void ShowNetworkTraffic()
{
PerformanceCounterCategory performanceCounterCategory = new PerformanceCounterCategory("Network Interface");
string instance = performanceCounterCategory.GetInstanceNames()[0]; // 1st NIC !
PerformanceCounter performanceCounterSent = new PerformanceCounter("Network Interface", "Bytes Sent/sec", instance);
PerformanceCounter performanceCounterReceived = new PerformanceCounter("Network Interface", "Bytes Received/sec", instance);
for (int i = 0; i < 10; i++)
{
Console.WriteLine("bytes sent: {0}k\tbytes received: {1}k", performanceCounterSent.NextValue() / 1024, performanceCounterReceived.NextValue() / 1024);
Thread.Sleep(500);
}
}
来自 here.