首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带宽监控系统

带宽监控系统
EN

Stack Overflow用户
提问于 2013-04-27 01:06:46
回答 1查看 5.3K关注 0票数 1

我正在尝试监控我的互联网连接的流量,即在给定时间的平均速度,以获得平均速度。我试过这个代码,但它不工作。我哪里错了?

代码语言:javascript
复制
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

我已经调用了函数,但是文本框只返回零。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-04-27 02:51:22

您需要找到您使用的网络接口,使用下面的代码创建一个控制台应用程序,您将能够看到哪个接口是活动的。

此外,您不能以这种方式更新UI,您需要使用后台工作程序在文本框中进行滚动更新。

代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16241722

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档