Performance Counter crash

cmolson

New member
Joined
Apr 25, 2007
Messages
1
Programming Experience
Beginner
Hi I am newish to visual Basic and have a question regarding a program I am writing. It basically monitors some system info (cpu usage, ram, network etc) and outputs it through the serial port to a microcontroller.

I have a problem with the network monitor. This is what I am doing:

VB.NET:
Dim netcounter As New System.Diagnostics.PerformanceCounter()
Dim pc As New PerformanceCounterCategory("Network Interface")

'This gets the size of the array, so I know how many network devices are detected

k = UBound(pc.GetInstanceNames())

'This adds each network device to a combo list
For i = 0 To k
            cb1.Items.Add(pc.GetInstanceNames(i))
 Next

'This assigns the performance counter to the network interface selected in the combo box, it is run within a timer every 1 second.

netcounter.CategoryName = "Network Interface"
netcounter.CounterName = "Bytes Received/sec"
netcounter.InstanceName = cb1.SelectedText()

'This takes the Bytes/Sec and assigns it to a variable and computes the K/sec

netcnt = netcounter.NextValue
netcnt = netcnt / 1024
This is where I get an error, but only when I select a network interface from the combo box, and then click on another window on the desktop, or on a button on the Frame.

This is the error message:

InvalidOperationException was unhandled
Counter is not single instance, an instance name needs to be specified.


I really appreciate any help you guys can offer. Or if there is something I should read up on about this.

Thanks,
Christian
 
Back
Top