2014-06-26 32 views
2

我一直在得到這個錯誤,我似乎無法解決try ... catch,我希望你的建議如何解決這個問題。還有更多的代碼,但我相信它與我得到的錯誤有關。 的代碼是這樣的:例外錯誤試圖ping一個定時器

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick 
    Dim ip As String 
    Dim sw As New Stopwatch 
    ip = "some.ip.com" 
    Try 
     If My.Computer.Network.Ping(ip) Then 
      ping = (sw.ElapsedMilliseconds) 
      Label1.Text = ping & "ms" 
      sw.Stop() 
      MsgBox("You have no connection.") 
     End If 
    Catch ex As Exception 
     MsgBox(ex) 
    End Try 
    If ping < 200 Then 
     Label2.BackColor = Color.Green 
     Label2.Text = "Good Connection" 
    ElseIf ping > 200 Then 
     Label2.BackColor = Color.Orange 
     Label2.Text = "Medium connection" 
    ElseIf ping > 400 Then 
     Label2.BackColor = Color.Red 
     Label2.Text = "Bad Connection" 
    ElseIf ping <= 0 Then 
     Label2.Text = "No Connection" 
     Label2.BackColor = Color.Brown 
    End If 

的錯誤給了我異常錯誤:

************** Exception Text ************** 
System.Net.NetworkInformation.PingException: An exception occurred during a Ping request. ---> System.Net.Sockets.SocketException: The requested name is valid, but no data of the requested type was found 
    at System.Net.Dns.GetAddrInfo(String name) 
    at System.Net.Dns.InternalGetHostByName(String hostName, Boolean includeIPv6) 
    at System.Net.Dns.GetHostAddresses(String hostNameOrAddress) 
    at System.Net.NetworkInformation.Ping.Send(String hostNameOrAddress, Int32 timeout,  Byte[] buffer, PingOptions options) 

---內部異常堆棧跟蹤的結尾---在 System.Net.NetworkInformation.Ping.Send (String hostNameOrAddress,Int32 timeout,Byte [] buffer,PingOptions options) at System.Net.NetworkInformation.Ping.Send(String hostNameOrAddress,Int32 timeout,Byte [] buffer) at Microsoft.VisualBasic.Devices.Network.Ping(字符串hostNameOrAddress,Int32超時) at Microso ft.VisualBasic.Devices.Network.Ping(String hostNameOrAddress) at C:\ Users \ tonakis2108 \ Documents \ Visual Studio 2013 \ Projects \ something \ something \ Form \ Form2.vb中的Sky_Casino.Form2.Timer1_Tick(Object sender,EventArgs e) :線61 在System.Windows.Forms.Timer.OnTick(EventArgs的) 在System.Windows.Forms.Timer.TimerNativeWindow.WndProc(消息&米) 在System.Windows.Forms.NativeWindow.Callback(IntPtr的的hWnd ,Int32 msg,IntPtr wparam,IntPtr lparam)

我想要我的代碼做的是ping目標,通過sw返回ms,並每5秒做一次。

當我將連接關閉以測試連接到程序時發生的錯誤時,出現此錯誤。誰能幫忙?

+0

它不知道什麼機器是「some.ip.com」。我也沒有。當你拔掉電纜時,你當然也會失去與DNS服務器的連接以及將「some.ip.com」解析爲IP地址的能力。 –

+0

爲什麼你不能「似乎用try ... catch解決」?發生異常時,您可能不想顯示消息框,但可能發現異常被捕獲?你可以在catch塊中設置「ping = -1」嗎? – Mark

+0

@馬克你是什麼意思? 我想在ms中向用戶顯示ping我的意思是說try ... catch不工作,因爲我仍然在計時器上看到異常。 – Tonakis2108

回答

1

Try/Catch的要點是,異常仍會發生,但您可以根據需要處理它,而不是程序崩潰。下面的代碼將更加優雅地處理錯誤,並且我還重新安排了檢查時間,因爲小於和大於檢查的組合不會給你想要的,並且增加了更長的超時時間(缺省值是500ms)。仍然不是最偉大的代碼,但對我來說,它處理網絡斷開連接!

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick 
    Dim ip = "www.google.com" 
    Dim timeout = 1000 
    Dim sw = New Stopwatch() 
    Try 
     Dim ping As Long = -1 
     sw.Start() 
     If My.Computer.Network.Ping(ip, timeout) Then 
      sw.Stop() 
      ping = sw.ElapsedMilliseconds 
      Label1.Text = String.Format("{0}ms", ping) 
     End If 
     If ping < 0 Then 
      Label2.Text = "Ping Timed Out" 
      Label2.BackColor = Color.Brown 
     ElseIf ping < 200 Then 
      Label2.Text = "Good Connection" 
      Label2.BackColor = Color.Green 
     ElseIf ping < 400 Then 
      Label2.Text = "Medium Connection" 
      Label2.BackColor = Color.Orange 
     Else 
      Label2.Text = "Bad Connection" 
      Label2.BackColor = Color.Red 
     End If 
    Catch ex As Exception 
     Label1.Text = "" 
     Label2.Text = ex.Message 
     Label2.BackColor = Color.Red 
     Console.WriteLine(ex.ToString()) 
    End Try 
End Sub 
+0

即使它以這種方式工作,我通過觀察我的工作室構建應用程序的位置來找到我的問題的答案。代碼雖然工作得很好,但我真的很想謝謝你對我的請求提供幫助,即使這是我錯誤導致的。再次感謝併爲此感到抱歉。 (太糟糕了,我不能+1的答案)。 :( – Tonakis2108

相關問題