2013-04-26 95 views
1

我試圖監控我的互聯網連接的流量,即給定時間的平均速度,以獲得平均速度。我試過這個代碼,但它不工作。我哪裏出錯了?帶寬監控系統

Function bwmonitor() 
    Dim pc As New PerformanceCounterCategory("Network Interface") 
    Dim instance As String = pc.GetInstanceNames(0) 
    Dim bs As New PerformanceCounter("Network Interface", "Bytes Sent/sec", instance) 
    Dim br As New PerformanceCounter("Network Interface", "Bytes Received/Sec", instance) 
    Dim k As Integer = 0 

    Do 
     k = k + 1 
     Dim kbsent As Integer = bs.NextValue()/1024 
     Dim kbRecieved As Integer = br.NextValue/1024 
     TextBox10.Text = kbsent 
     TextBox11.Text = kbRecieved 
     Threading.Thread.Sleep(1000) 
    Loop Until k = 10 

    Return 0 
End Function 

我已經調用函數,但文本框只返回零。

回答

1

您需要找到您使用的網絡接口,使用下面的代碼創建控制檯應用程序,並且您將能夠看到哪個接口處於活動狀態。

此外,您將無法以這種方式更新UI,您需要使用後臺工作人員在文本框中進行滾動更新。

Sub Main() 

Dim networkInterfaces As New System.Diagnostics.PerformanceCounterCategory("Network Interface") 
    Dim nics As String() = networkInterfaces.GetInstanceNames() 
    Dim bytesSent(nics.Length - 1) As System.Diagnostics.PerformanceCounter 
    Dim bytesReceived(nics.Length - 1) As System.Diagnostics.PerformanceCounter 
    Dim i As Integer 
    For i = 0 To nics.Length - 1 
    bytesSent(i) = New System.Diagnostics.PerformanceCounter("Network Interface", "Bytes Sent/sec", nics(i), True) 
    bytesReceived(i) = New System.Diagnostics.PerformanceCounter("Network Interface", "Bytes received/sec", nics(i), True) 
Next 
    'Dim ni As System.Diagnostics.PerformanceCounter 
For i = 0 To nics.Length - 1 
    System.Console.WriteLine(String.Format("  interface {0}: {1} ", i, nics(i))) 
Next 
Do 
     For i = 0 To nics.Length - 1 
      System.Console.WriteLine(String.Format("  interface {0}: {1} bytes sent/sec, {2} bytes received/sec. ", i, bytesSent(i).NextValue, bytesReceived(i).NextValue)) 
     Next 

     System.Console.WriteLine("") 
     System.Console.WriteLine("") 
     System.Threading.Thread.Sleep(3000) 
Loop 

End Sub 
+0

thanx,我仍然有一個挑戰,我仍然想與我的WindowsApplication集成它。就像分配給變量一樣,這將幫助我顯示正在發送或接收的平均帶寬。 – 2013-04-28 13:00:13

+0

另外,我正在使用接口9 – 2013-04-28 13:01:08

+0

我已經解決了GUI部分檢查這個問題[Windows控制檯帶寬監視器](http://stackoverflow.com/questions/16264927/windows-console-bandwidth-monitoring-system) – 2013-04-28 16:22:10