我正在开发一个带宽监控系统,我希望它循环,显示在给定实例上发送或接收的数据量,但问题是系统在处理时冻结,我只想让它工作在背景,在我正在做其他事情的同时在系统中定义的文本框中显示结果。这是功能:
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.即以太网接口。我很感激建议
答案 0 :(得分: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
我终于得到了这个,如果有人有更好的方法请帮忙。这个工作正在进行,但有一个可能有更好的方法。