0

我正在开发带宽监控系统,我希望它循环显示在给定实例中发送或接收的数据量,但问题是系统在处理时冻结,我只想让它在后台工作,显示当我在做其他事情时,系统中定义的文本框中的结果。这是功能:

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。即以太网接口。我很欣赏建议

4

1 回答 1

0
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

我终于明白了,如果有人有更好的方法,请帮忙。这个正在工作,但可能有更好的方法。

于 2013-04-28T19:12:59.810 回答