By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,269 Members | 1,561 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,269 IT Pros & Developers. It's quick & easy.

My.Computer.network.ping Do Until Loop

P: 17
Hi People,

Im trying to do a simple Loop where by an IP address is pinged (Using My.computer.network.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

Expand|Select|Wrap|Line Numbers
  1. Dim IPAddress As String = "192.168.1.122"
  2.  
  3.         If My.Computer.Network.Ping(IPAddress) = False Then
  4.  
  5.             Do Until True
  6.             Loop
  7.  
  8.             PictureBox1.Visible = True
  9.         Else
  10.             PictureBox2.Visible = True
  11.  
  12.         End If
  13.  
  14. End Sub
  15.  
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
Feb 23 '08 #1
Share this Question
Share on Google+
3 Replies


P: 23
Hi People,

Im trying to do a simple Loop where by an IP address is pinged (Using My.computer.network.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

Expand|Select|Wrap|Line Numbers
  1. Dim IPAddress As String = "192.168.1.122"
  2.  
  3.         If My.Computer.Network.Ping(IPAddress) = False Then
  4.  
  5.             Do Until True
  6.             Loop
  7.  
  8.             PictureBox1.Visible = True
  9.         Else
  10.             PictureBox2.Visible = True
  11.  
  12.         End If
  13.  
  14. End Sub
  15.  
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:

Expand|Select|Wrap|Line Numbers
  1.  
  2. Do 
  3.         If My.Computer.Network.Ping(IPAddress) = False Then
  4.             PictureBox1.Visible = True
  5.         Else
  6.             PictureBox2.Visible = True 
  7.         End If
  8.         Application.DoEvents();
  9.         // System.Threading.Thread.Sleep(1000);
  10.  
  11.  Loop
  12.  
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.Threading.Thread.Sleep(1000)
Feb 23 '08 #2

P: 17
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
Feb 23 '08 #3

P: 23
Where is this code? Is it in the form_load event?

Did you include the Application.DoEvents()? 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.
Feb 24 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.