2013-04-28 2 views
0

대역폭 모니터링 시스템에서 작업 중입니다. 루프를 보내고 주어진 인스턴스에서 보내거나받는 데이터의 양을 표시하고 싶지만 문제는 처리가 끝나면 시스템이 멈추는 것입니다. 다른 일을하는 동안 시스템에 정의 된대로 텍스트 상자에 결과를 표시하면서 백그라운드에서 작동하게 할 수 있습니다. 함수는 다음과 같습니다.Windows 콘솔 대역폭 모니터링 시스템

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, 즉 이더넷 인터페이스를 테스트 중입니다. 제안 해 주셔서 감사합니다.

+1

* gui 앱에서는 절대로 절전 모드를 사용하지 마십시오. 대신 타이머를 사용하십시오. –

답변

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 

누군가가 더 좋은 방법을 가지고 있다면 마침내 얻었습니다. 이것은 작동하지만 더 나은 방법이있을 수 있습니다.

+0

그건 완벽하게 받아 들일 수있는 방법입니다. Hans가 위에서 지적한 것처럼 thread.sleep을 사용하지 마십시오. – GJKH

관련 문제