尝试使用计时器ping时出现异常错误

时间:2014-06-26 18:16:51

标签: vb.net timer

我得到这个错误我似乎无法用try ... catch来解决,我希望你的建议如何解决这个问题。还有更多的代码,但我相信它与我得到的错误有关。 代码是这样的:

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)    在System.Net.NetworkInformation.Ping.Send(String hostNameOrAddress,Int32 timeout,Byte [] buffer)    在Microsoft.VisualBasic.Devices.Network.Ping(String hostNameOrAddress,Int32 timeout)    在Microsoft.VisualBasic.Devices.Network.Ping(String hostNameOrAddress)    在C:\ Users \ tonakis2108 \ Documents \ Visual Studio 2013 \ Projects \ something \ something \ Form2.vb中的Sky_Casino.Form2.Timer1_Tick(Object sender,EventArgs e):第61行    在System.Windows.Forms.Timer.OnTick(EventArgs e)    在System.Windows.Forms.Timer.TimerNativeWindow.WndProc(Message&amp; m)    在System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)

我希望我的代码执行的操作是ping目标,通过sw返回ping,并每5秒执行一次。

当我连接到程序时,我关闭连接以测试错误时出现此错误。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

Try / Catch的重点是仍然会发生异常,但您可以根据需要处理它,而不是程序崩溃。以下代码将更优雅地处理错误,并且我还重新安排了对ping时间的检查,因为小于和大于检查的组合不会提供您想要的内容,并添加了更长的超时(默认值)是500毫秒)。仍然不是最好的代码,但对我来说它处理网络被断开连接!

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