FOR语句与VB中的IF语句冲突

时间:2015-09-20 09:13:11

标签: vb.net visual-studio-2015

我正在创建一个相当简单的ping工具,它以毫秒为单位显示服务器响应的时间。如果服务器没有响应,则显示为0ms响应。我想实现一个If语句在ListBox中写Server failed to respond而不是在0ms内回复。唯一的问题是我有一大块代码需要在If之外运行,但继续在If内部,并涉及使用代码行Next ...这似乎导致If语句无法识别End IfEnd If无法识别If ...

这是我的代码:

For i As Integer = 0 To numberOfPings - 1
        Dim ping As New Ping
        Dim pingRe As PingReply = ping.Send(pingTarget)

        If pingRe.RoundtripTime = 0 Then
            Me.listboxPing.Items.Add("Server failed to respond...")
        Else

            Me.listboxPing.Items.Add("Response from " & pingTarget & " in " & pingRe.RoundtripTime.ToString() & "ms")
            listboxPing.SelectedIndex = listboxPing.Items.Count - 1
            listboxPing.SelectedIndex = -1
            Application.DoEvents()

            Threading.Thread.Sleep(500)
    Next



    Me.listboxPing.Items.Add("")


        End If

有谁知道我可以解决这个问题的方法/解决这个问题?

谢谢,

2 个答案:

答案 0 :(得分:1)

如果我打算编写代码来ping一个地址并显示结果,它会看起来像这样。

Dim pingThrd As Threading.Thread

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    If pingThrd Is Nothing OrElse pingThrd.ThreadState = Threading.ThreadState.Stopped Then
        RichTextBox1.Clear()
        pingThrd = New Threading.Thread(AddressOf PingIt)
        pingThrd.IsBackground = True
        pingThrd.Start("192.168.33.1")
    End If
End Sub

Public Sub PingIt(pingTarget As Object)
    Dim numberOfPings As Integer = 5
    Dim pingT As String = DirectCast(pingTarget, String)
    Dim pingTimeOut As Integer = 1000
    Const dlyBetweenPing As Integer = 500

    Dim dspStr As String

    For i As Integer = 0 To numberOfPings - 1
        Dim pingit As New Ping
        Dim pingRe As PingReply = pingit.Send(pingT, pingTimeOut)
        'check if success
        If pingRe.Status = IPStatus.Success Then
            dspStr = String.Format("Response from: {0} in {1}ms.", pingRe.Address, pingRe.RoundtripTime)
        Else
            dspStr = String.Format("{0} failed.  Status: {1}", pingRe.Address, pingRe.Status)
        End If
        Me.BeginInvoke(Sub()
                           RichTextBox1.AppendText(dspStr)
                           RichTextBox1.AppendText(Environment.NewLine)
                       End Sub)
        Threading.Thread.Sleep(dlyBetweenPing)
    Next
End Sub

编辑:相同的基本代码,但允许线程以不同的地址和计数开始。

Structure PingWhat
    Dim addr As String
    Dim howmany As Integer
End Structure

Dim pingThrd As Threading.Thread

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    If pingThrd Is Nothing OrElse pingThrd.ThreadState = Threading.ThreadState.Stopped Then
        RichTextBox1.Clear()
        'setup a thread to do the actual ping'ing
        'this allows the UI to function
        pingThrd = New Threading.Thread(AddressOf PingIt)
        pingThrd.IsBackground = True
        'setup address to ping and howmany times to ping it
        Dim somePing As New PingWhat With {.addr = "192.168.33.1", .howmany = 3}
        'start the thread
        pingThrd.Start(somePing)
    End If
End Sub

Public Sub PingIt(pingTarget As Object)
    Dim pingT As PingWhat = DirectCast(pingTarget, PingWhat)
    Dim pingTimeOut As Integer = 1000
    Const dlyBetweenPing As Integer = 500

    Dim dspStr As String

    For i As Integer = 1 To pingT.howmany
        Dim pingit As New Ping
        Dim pingRe As PingReply = pingit.Send(pingT.addr, pingTimeOut)
        'check if success
        If pingRe.Status = IPStatus.Success Then
            dspStr = String.Format("Response from: {0} in {1} ms.", pingRe.Address, pingRe.RoundtripTime)
        Else
            dspStr = String.Format("Ping Failed {0}.  Status: {1}", pingT.addr, pingRe.Status)
        End If
        'update the UI
        Me.BeginInvoke(Sub()
                           RichTextBox1.AppendText(dspStr)
                           RichTextBox1.AppendText(Environment.NewLine)
                           RichTextBox1.ScrollToCaret()
                       End Sub)
        Threading.Thread.Sleep(dlyBetweenPing)
    Next
    Me.BeginInvoke(Sub()
                       RichTextBox1.AppendText("Done")
                       RichTextBox1.AppendText(Environment.NewLine)
                       RichTextBox1.ScrollToCaret()
                   End Sub)
End Sub

答案 1 :(得分:0)

这是你之后的事吗?

 options_description_easy_init add_options();

If会改变范围,因此,你需要使用一个变量来检查它是否进入了Else部分。

当然,您需要在Next之前关闭第一个If。