Windows控制台带宽监控系统 [英] Windows console Bandwidth monitoring System

查看:229
本文介绍了Windows控制台带宽监控系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带宽监视系统,我希望它循环显示在给定实例中发送或接收的数据量,但是问题是系统在处理时冻结,我只是希望它能在背景,在我做其他事情的同时,在系统中定义的文本框中显示结果.功能如下:

I am working on a Bandwidth monitoring system, i want it loop, displaying the amount of data being sent or received at a given instance, but the problem is that the system freezes when its processing, i just want it to work in background, displaying the results in the text boxes as defined in the system whilst i am doing other things. Here is the function:

Private Function netSpeed() As Boolean

    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

    bytesSent(9) = New System.Diagnostics.PerformanceCounter("Network Interface", "Bytes Sent/sec", nics(9), True)
    bytesReceived(9) = New System.Diagnostics.PerformanceCounter("Network Interface", "Bytes received/sec", nics(9), True)

    Dim up As Integer
    Dim down As Integer

    For k As Integer = 0 To 2
        up = bytesSent(9).NextValue
        down = bytesReceived(9).NextValue

        System.Threading.Thread.Sleep(1000)

    Next

    TextBox1.Text = up
    TextBox2.Text = down

    Return True
End Function

请注意:我正在测试接口9.即以太网接口.我很感谢建议

Please note: I am testing interface 9. i.e Ethernet interface. I appreciate suggestions

推荐答案

Private Sub netSpeed()

    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

    bytesSent(9) = New System.Diagnostics.PerformanceCounter("Network Interface", "Bytes Sent/sec", nics(9), True)
    bytesReceived(9) = New System.Diagnostics.PerformanceCounter("Network Interface", "Bytes received/sec", nics(9), True)

    Dim up As Integer
    Dim down As Integer

    Do
        up = bytesSent(9).NextValue
        down = bytesReceived(9).NextValue
        System.Threading.Thread.Sleep(1000)

        AddText(up)
        AddTxt(down)
    Loop

End Sub

Delegate Sub AddTextCallBack(ByVal [text] As String)

Private Sub AddText(ByVal [text] As String)
    If Me.TextBox1.InvokeRequired Then
        Dim d As New AddTextCallBack(AddressOf AddText)
        Me.Invoke(d, New Object() {[text]})
    Else
        Me.TextBox1.Text = [text]
    End If
End Sub

Private Sub AddTxt(ByVal [text] As String)
    If Me.TextBox2.InvokeRequired Then
        Dim d As New AddTextCallBack(AddressOf AddTxt)
        Me.Invoke(d, New Object() {[text]})
    Else
        Me.TextBox2.Text = [text]
    End If
End Sub

我终于明白了,如果有人有更好的方法,请帮忙.这是可行的,但可能会有更好的方法.

I finally got this, if anyone has a better way please help. This one is working but one might have a better way.

这篇关于Windows控制台带宽监控系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆