Hello All,

I've only just started to develope some code in VB.net and i have very little experience in it and some of the "instructional" web pages are very confusing to my dyslexic mind.

The scenario is as follows: -

I am connected to a server via VPN, but the connection speed to the server is kind of erratic. The CAD software I use in my day job, is quite processor hungry/dependent and sometimes the CAD software will stop working as it processes work/commands. Sometimes the issue is not the CAD software, but the latency of the VPN connection (this is not at my end, but the server connection). When the CAD stops working, I start a command prompt window and ping the server and get something like the following.

Ping.jpg

I would like to do something similar in vb.net.

I have a user form with a button on it to ping the cad server and this works and I can get a single result.

This uses the following code (which I have borrowed from somewhere):

    Private Sub OnePingCadSvr_Click(sender As Object, e As EventArgs) Handles OnePingCadSvr.Click

        Dim host As String = "0.0.0.0"
        Dim pingreq As Ping = New Ping()
        Dim rep As PingReply = pingreq.Send(host)
        Dim SingleRoundtripTime As Long

        Console.WriteLine("Pinging {0} [{1}]", host, rep.Address.ToString())
        Console.WriteLine("Reply From {0} : time={1} TTL={2}", rep.Address.ToString(), rep.RoundtripTime, rep.Options.Ttl)
        PingHost.Text = "Reply From: " & rep.Address.ToString()
        PingHost.Refresh()
        PingRoundTrip.Text = "Round Trip Time: " & rep.RoundtripTime & "ms" ' & rep.Options.Ttl
        PingRoundTrip.Refresh()
        System.Threading.Thread.Sleep(1500)

    End Sub

If I try to run this 4 times in a FOR loop, the round trip time is exactly the same time for each iteration of the loop i.e. If I get 27ms for the first iteration, it will be the same value for the remaining iterations, whereas manually repeating a single ping, 4 times, I get slightly different speeds (as I would expect) and more inline with the cmd ping.

It almost feels like I need to reset something before each iteration of the FOR loop starts, but I don’t have a clue.

I have tried using google and I've gone through lots of different web sites and not come up with a solution, this is mainly because I don't really understand the syntax

Can anyone help?

I also realise that my code is probably not the best, but I’m trying to learn as I go.

Thanks in advance, Steve

I would need to see the code with the for loop to take a stab at this but there are known issues with screen updates in apps like this. You may have to insert a System.Threading.Thread.Sleep(small number here) after the .Refresh to get the text on screen to update. Also it's just one box that you overwrite so again you might be getting 4 numbers but only one number shows. Again, without the code that has the for loop, I will guess badly. I will have better guesses with the actual failing code.

That said, why not insert Debug.Print statements in said code so you can see where the issue is before you work on fixes?

Hello rpoffitt,

Thanks for your advice.

The FOR loop is (as far as I'm concerned) nothing special, other than repeating the same code 4 times, code is as follows (with a couple of Debug.Print statements added).

    Private Sub PingCadSvr_Click(sender As Object, e As EventArgs) Handles PingCadSvr.Click

        Dim host As String = "0.0.0.0"
        Dim pingreq As Ping = New Ping()
        Dim rep As PingReply = pingreq.Send(host)
        Dim SingleRoundtripTime As Long

   For i As Integer = 0 To 3
        Debug.Print("Ping Loop Number: " & i)
        Console.WriteLine("Pinging {0} [{1}]", host, rep.Address.ToString())
        Console.WriteLine("Reply From {0} : time={1} TTL={2}", rep.Address.ToString(), rep.RoundtripTime, rep.Options.Ttl)
        PingHost.Text = "Reply From: " & rep.Address.ToString()
        PingHost.Refresh()
        PingRoundTrip.Text = "Round Trip Time: " & rep.RoundtripTime & "ms" ' & rep.Options.Ttl
        PingRoundTrip.Refresh()
        System.Threading.Thread.Sleep(1500)
        Debug.Print("Round Trip Time : " & rep.RoundtripTime & "ms")
    Next i

    End Sub

So, running this code, as before the round trip time is exactly the same for each iteration and running a single ping 4 times (via a button click) gives different results.

One thing I tried for the single ping was to copy and paste the single ping code 4 times (so that same code was repeated 4 time on differnet lines of code blocks) and I found that this also returned the same ping time for each time the server was pinged (as per the For loop).

The next thing I tried was to put the Ping code in its own sub routine, something like this:

    Private Sub PingCadSvr_Click(sender As Object, e As EventArgs) Handles PingCadSvr.Click

        For i As Integer = 0 To 3
            Debug.Print("Ping Loop Number: " & i)
            Call PingCode()
        Next i

    End Sub

    Private Sub PingCode()

        Dim host As String = "10.0.1.4" ' use any other machine name
        Dim pingreq As Ping = New Ping()
        Dim rep As PingReply = pingreq.Send(host)

        Console.WriteLine("Pinging {0} [{1}]", host, rep.Address.ToString())
        Console.WriteLine("Reply From {0} : time={1} TTL={2}", rep.Address.ToString(), rep.RoundtripTime, rep.Options.Ttl)
        PingHost.Text = "Reply From: " & rep.Address.ToString()
        PingHost.Refresh()
        PingRoundTrip.Text = "Round Trip Time: " & rep.RoundtripTime & "ms " & rep.Options.Ttl
        PingRoundTrip.Refresh()
        Debug.Print("Round Trip Time : " & rep.RoundtripTime & "ms")
        System.Threading.Thread.Sleep(127)

    End Sub

and for some reason this works!!!,

Results: -

Ping Loop Number: 0
Round Trip Time : 14ms
Ping Loop Number: 1
Round Trip Time : 57ms
Ping Loop Number: 2
Round Trip Time : 14ms
Ping Loop Number: 3
Round Trip Time : 44ms

but I have no idea why?

" Ping code in its own sub routine " and why that works.

My thought is that the runtime/compiler optimizers are in play here. As you fixed it I'll pause looking over your code now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.