Question about Loop

 

Results 1 to 2 of 2
Hey everyone..Hope I can get a simple answer to this one. I'm trying to write ...
  1. #1
    fpforum is offline Junior Member
    Join Date
    Apr 2008
    Posts
    117
    Thanks
    23
    Thanked 4 Times in 4 Posts

    Default Question about Loop

    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 next..so 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..

    Code:
        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
    
            Do
                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
                    Else
                        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 by fpforum; 12-27-2012 at 08:53 AM.




  2. #2
    Psychop1 is offline Registered Member
    Join Date
    Jul 2010
    Posts
    60
    Thanks
    21
    Thanked 18 Times in 15 Posts

    Default Re: Question about Loop

    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.
    Last edited by Psychop1; 12-28-2012 at 12:06 AM.

  3. The Following 2 Users Say Thank You to Psychop1 For This Useful Post:

    fpforum (12-28-2012), MAGNATER (04-18-2013)

Similar Threads

  1. Out of the loop...how's BH?
    By chhhad in forum Black Hat SEO
    Replies: 5
    Last Post: 09-09-2012, 09:16 PM
  2. Imacros Loop Question
    By nothingmore in forum General Programming Chat
    Replies: 1
    Last Post: 08-10-2012, 06:04 AM
  3. I am in a loop here...
    By falarious in forum BlackHat Lounge
    Replies: 0
    Last Post: 05-13-2010, 10:06 PM
  4. loop
    By uncivilBANKS in forum Making Money
    Replies: 2
    Last Post: 07-22-2009, 01:01 PM
  5. Link question: closing the loop
    By noesis2020 in forum White Hat SEO
    Replies: 0
    Last Post: 12-15-2007, 06:55 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




BlackHatWorld on Twitter BlackHatWorld on FaceBook


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98