Question about Loop

Discussion in 'Visual Basic .NET' started by fpforum, Dec 27, 2012.

  1. fpforum

    fpforum Junior Member

    Apr 22, 2008
    Likes Received:
    Hey everyone..Hope I can get a simple answer to this one.

    I'm trying to write a basic ping program where the user puts in a list of IP's/websites and selects or sets the number of times to ping each address. I then want it to ping the first item in the list, wait five seconds and then ping the on and so fourth until the amount of times to ping each address has been reached.

    My loop seams to be working alright but I can't seam to get it so it will pause between each ping. I've created the loop inside a timer and set to 5 seconds and what is happening now is that every 5 seconds it makes one big swoop through the whole list. I've even tried sticking some other options such as dimming "timer" as interval and then placing a timer = 1000 * 5 in my code but that doesn't seam to place a pause in between each individual ping. Also, as you will notice in my code - I have a label (lblTotalPingsReceived) which should increase by 1 when a successful ping is returned. When this loop makes its big swoop through the entire list it counts that whole swoop as 1 instead of the individual pings..

    It kind of seams to me like each individual listbox item needs to be put into an array, but i'm not exactly sure how to do that inside a loop..

    Here's my code maybe someone can help..

        Private Sub pingTimer_Tick(sender As System.Object, e As System.EventArgs) Handles pingTimer.Tick
            Dim pingSender As New Ping()
            Dim options As New PingOptions()
            Dim roundtriptime As String
            Dim x As Integer
            Dim timer As Integer
                For x = 0 To lstIps.Items.Count - 1
                    ' Use the default Ttl value which is 128,
                    ' but change the fragmentation behavior.
                    options.DontFragment = True
                    ' Create a buffer of 32 bytes of data to be transmitted.
                    Dim data As String = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
                    Dim buffer() As Byte = Encoding.ASCII.GetBytes(data)
                    Dim timeout As Integer = 120
                    Dim reply As PingReply = pingSender.Send(lstIps.SelectedItem, timeout, buffer, options)
                    If reply.Status = IPStatus.Success Then
                        timer = 1000 * 5
                        lstIps.SetSelected(x, True)
                        roundtriptime = GetMs(reply.RoundtripTime)
                        txtLog.AppendText("Ping Successful with Roundtrip Time: " & roundtriptime & Now & vbCrLf)
                        lblTotalPingsReceived.Text = TotalPingsReceived + 1
                        timer = 1000 * 5
                        lstIps.SetSelected(x, True)
                        txtLog.AppendText("Ping UnSuccessful at: " & Now & vbCrLf)
                        lblTotalPingsLost.Text = TotalPingsLost + 1
                    End If
                Next x
                lblTotalPingsSent.Text = TotalPingsSent + 1
                pingNumber = pingNumber - 1
            Loop Until pingNumber >= 0
                pingTimer.Enabled = False
                btnStartPing.Text = "Start"
        End Sub
    Last edited: Dec 27, 2012
  2. Psychop1

    Psychop1 Registered Member

    Jul 30, 2010
    Likes Received:
    It looks to me that your timer is enabled, and then runs through all your code on the first tick. This is because there is no code to determine how many times the timer has ticked and when to start your code, therefore your code runs all the way through on the first tick.

    This would likely be easier if your loop was in a separate sub and your if statement was inside the timer. Your loop would simply initiate the timer. If you have your timer interval set to 1000, inside your timer, you can poll your timer with an if statement to see if it has ticked 5 times, and if it has, start your code. Also, the timer will just keep ticking if it isn't stopped. Hope this helps.
    • Thanks Thanks x 2
    Last edited: Dec 28, 2012