1

I'm trying to display the received bytes per second and have them displayed in the console once a second. The I am presented with a Invalid Operation Exception once it reaches .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);
}
dinnertoast
  • 81
  • 1
  • 9
  • 1
    Can you define "the code breaks"? – sab669 Aug 25 '15 at 18:37
  • I am presented with a dialogue box that shows a Invalid Operation Exception. – dinnertoast Aug 25 '15 at 18:39
  • 2
    https://msdn.microsoft.com/en-us/library/system.diagnostics.performancecounter.nextvalue(v=vs.110).aspx *"To read performance counters, you must have administrative privileges. In Windows Vista, (UAC) determines the privileges of a user. If you are a member of the Built-in Administrators group, you are assigned two run-time access tokens: a standard user access token and an administrator access token. By default, you are in the standard user role. To execute the code that accesses performance counters, you must first elevate your privileges from standard user to administrator. "* – Ron Beyer Aug 25 '15 at 18:43
  • Not running as administrator will give an `UnauthorizedAccessException`. If you can verify you are running as admin or what exception you are getting, we can help further, but this is the biggest problem. Edit: If you are getting `InvalidOperationException` then you haven't correctly set up your performance counter, see the page I linked. – Ron Beyer Aug 25 '15 at 18:44
  • Thanks for sharing that link. I can confirm i am running as the system administrator. I've tried changing the performance counter to "Memory" , "Available MBytes" This makes the code run. are network values handled differently? – dinnertoast Aug 25 '15 at 19:30

2 Answers2

3

Sorry for answering after someone already did, but does this helps by any chance ?

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);
    }
}

From here.

Drew
  • 24,851
  • 10
  • 43
  • 78
Alon M
  • 1,583
  • 7
  • 30
  • 44
0

NetworkDownSpeed.NextValue() can throw InvalidOperationException.

In NetworkDownSpeed.NextValue there is a comment that details the why.

// 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.

An alternative solution that takes care of this issue can be found on this stack overflow post.

Community
  • 1
  • 1
Joshua
  • 542
  • 1
  • 4
  • 17