My.Computer.network.ping Do Until Loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SyGC
    New Member
    • Feb 2008
    • 17

    My.Computer.network.ping Do Until Loop

    Hi People,

    Im trying to do a simple Loop where by an IP address is pinged (Using My.computer.net work.ping) and the results, true or false, are used to invoke another line of code.
    Basically if the IP is pingable (true) image A is displayed if it is not (false) then image B is.

    The code i am using is as follows

    Code:
    Dim IPAddress As String = "192.168.1.122"
           
            If My.Computer.Network.Ping(IPAddress) = False Then
    
                Do Until True
                Loop
    
                PictureBox1.Visible = True
            Else
                PictureBox2.Visible = True
    
            End If
    
    End Sub
    The code compiles fine however it doesnt seem to loop the ping command. For example unplugging the network cable from the target machine and plugging it back in doesnt result in the app showing image A then B (A= Connected B= Disconnected). I thought that by looping the Network.ping, it will continuously do so until the arguement is reached (true)

    Im new to VB.NET environment so any suggestions or advice you can give would be great.

    Thanks in advance

    Sy
  • jagged
    New Member
    • Feb 2008
    • 23

    #2
    Originally posted by SyGC
    Hi People,

    Im trying to do a simple Loop where by an IP address is pinged (Using My.computer.net work.ping) and the results, true or false, are used to invoke another line of code.
    Basically if the IP is pingable (true) image A is displayed if it is not (false) then image B is.

    The code i am using is as follows

    Code:
    Dim IPAddress As String = "192.168.1.122"
           
            If My.Computer.Network.Ping(IPAddress) = False Then
    
                Do Until True
                Loop
    
                PictureBox1.Visible = True
            Else
                PictureBox2.Visible = True
    
            End If
    
    End Sub
    The code compiles fine however it doesnt seem to loop the ping command. For example unplugging the network cable from the target machine and plugging it back in doesnt result in the app showing image A then B (A= Connected B= Disconnected). I thought that by looping the Network.ping, it will continuously do so until the arguement is reached (true)

    Im new to VB.NET environment so any suggestions or advice you can give would be great.

    Thanks in advance

    Sy
    Do until True
    Loop

    essentially does nothing. You loop until the condition after "until" specified is true. Since you set it to true, it never enters the loop.

    You want something like:

    Code:
    Do 
            If My.Computer.Network.Ping(IPAddress) = False Then
                PictureBox1.Visible = True
            Else
                PictureBox2.Visible = True 
            End If
            Application.DoEvents();
            // System.Threading.Thread.Sleep(1000);
     
     Loop
    I'm not too familiar with vb.net (expert with "old" VB) so maybe the syntax changed, but your loop needs to be outside the IF .... ping ... Then block.

    Bear in mind that this will continously ping the server (hundreds of times in a second I would assume) so you may want to use a timer (if you have a form) or System.Threadin g.Thread.Sleep( 1000)

    Comment

    • SyGC
      New Member
      • Feb 2008
      • 17

      #3
      Hi Jagged

      yes indeed in my infinite newbiness i should of put the IF statement inside the DO until Loop.

      However something strange now happans. After implementing your code and compiling my Form no longer appears. Debugging is occuring but i dont see my form.

      If i quote out the 'Do' and 'Loop' and compile my form again i can see it.

      Any Ideas?

      Thanks again
      Sy

      Comment

      • jagged
        New Member
        • Feb 2008
        • 23

        #4
        Where is this code? Is it in the form_load event?

        Did you include the Application.DoE vents()? That processes other messages on the queue, like drawing the form so if you left it out, that could be why you don't see the form.

        What you should do is drag a timer on your form, double click on it to open the event handler and then paste the IF Then block in there (no do .. loop). Set the interval to 1000 (1 second) and make sure enabled = true. This way, the ping will occur every second and it's not blocking the rest of your app from working.

        Comment

        Working...