Windows console Bandwidth monitoring System

◇◆丶佛笑我妖孽 提交于 2019-12-12 02:55:26

问题


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

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


回答1:


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.



来源:https://stackoverflow.com/questions/16264927/windows-console-bandwidth-monitoring-system

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!