Some VB code help

Zstream

Diamond Member
Oct 24, 2005
3,395
277
136
Hey all,

What would be the simplest way to change this code? It needs to try to ping a site every five seconds for 30 seconds. If it is succesfull at any time then notify the user. This is what I have come up with but the looping part is killing me.


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
My.Computer.Network.Ping("google.com")
MsgBox("You are now online! Click here to continue working.")
Catch ex As Exception
MsgBox("Unable to connect to Google, please reboot your device")
End Try
End Sub
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,836
4,816
75
There's probably several ways. Not knowing much VB, here's one that ought to work:

Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
dim keeplooping
keeplooping = true
While keeplooping
Try
My.Computer.Network.Ping("google.com")
MsgBox("You are now online! Click here to continue working.")
keeplooping=0
Catch ex As Exception
MsgBox("Unable to connect to Google, please reboot your device")
End Try
End While
End Sub

Edit: You probably need a sleep or something in there for the 5 seconds, and maybe a counter for the 30 seconds.
 

Grabo

Senior member
Apr 5, 2005
254
57
101
Well, I'm far from a vb.net god, but the msgbox requires an answer, and if you want the user alerted at any time a reasonable ping response is had then the user would have to click 'OK' 6 times for the program to do six pings (one ping every 5 secs for 30 secs).

Furthermore, without using a separate process for the form, it'll be somewhat hard to quit the program.

A suggestion might be this:
Code:
Imports System.Net.NetworkInformation
Imports System.Threading
Public Class Form1

    Private Sub Ping_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Ping.Click
        Dim t As Thread
        t = New Thread(AddressOf Me.pinger)
        t.Start()
    End Sub

    Private Sub pinger()
        Dim ping As New Ping
        Dim i As Integer
        i = 0

        While i < 6
            Try
                ping.Send("jodi.org")
               MsgBox("Remote answer received")
            Catch ex As Exception
                MsgBox("No lifesigns")
            End Try
            Thread.Sleep(5000)
            i += 1
        End While

    End Sub
End Class

You can quit it when you want, but it won't continue unless you hit OK six times. Thus, it would seem wiser to put the 'ping OK' messages in a textbox in a form, or such. Additionally, I don't know how / if possible to specify ping timeout.


Or, is your intention for it to ping six times only if it fails five? Meaning, ping until successfull, every 5 secs, and notify and abort if successful?
 
Last edited:

Zstream

Diamond Member
Oct 24, 2005
3,395
277
136
Or, is your intention for it to ping six times only if it fails five? Meaning, ping until successfull, every 5 secs, and notify and abort if successful?

That is correct.

Ping - Fail - Continue
Ping - Fail - Continue
Ping - Pass - Alert User
Do not ping 3 more times.
Close.
 

Grabo

Senior member
Apr 5, 2005
254
57
101
That is correct.

Ping - Fail - Continue
Ping - Fail - Continue
Ping - Pass - Alert User
Do not ping 3 more times.
Close.

Alright.

Then you could revise relevant part above with:
Code:
  While i < 5
            Try
                ping.Send("jodi.org")
                MsgBox("Answer received, connectivity indicated, aborting test.")
                Exit While
            Catch ex As Exception
            End Try
            Thread.Sleep(5000)
            i += 1
        End While

        If i > 4 Then
            MsgBox("No answer / connectivity detected.")
        End If