我正在研究一个带宽监控系统,我希望它循环,显示在给定实例中发送或接收的数据量,但问题是系统在处理时冻结,我只是希望它在后台工作,在系统中定义的文本框中显示结果,而我正在做其他事情。下面是函数:
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。即以太网接口。
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
我终于明白了,如果有人有更好的方法,请帮助。