9 Replies - 1138 Views - Last Post: 20 January 2013 - 07:52 AM Rate Topic: -----

#1 Absolute Zero  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 21-June 11

No text output on ping loop

Posted 19 January 2013 - 02:30 PM

Hi, guys. I was wondering if you could once again point out my silly mistakes for me! I'll eventually learn how to do this the right way, without asking questions.

I have a bit of a problem. I want this code to ping a remote host and monitor for a downage, but I can't get the text to display in the boxes, so I have no idea whether it's pinging or not. All I know is it just freezes when I run it. Seems like the correct implementation of a loop to me, so I'm not sure what's wrong. Maybe my Ifs/End Ifs are in the wrong places, but I've swapped them around a couple times.

The code is as follows:

Imports System.Net.NetworkInformation

Public Class Form1
    Dim Fails As Integer = 0
    Dim Downage As Boolean = False
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Do Until Downage = True
            Dim Pingas As Integer = 0
            Dim Server As String = "127.0.0.1"
            Dim TimeOut As Integer = 200
            Dim PingServer As Ping = New Ping()
            Dim Reply As PingReply = PingServer.Send(Server)
            If Reply.Status = IPStatus.Success Then
                Pingas += 1
            End If
            TextBox1.Text = Pingas
            If Reply.Status = IPStatus.TimedOut Then
                Fails += 1
            End If
            If Fails >= 15 Then
                Downage = True
            End If
            TextBox1.Text = Pingas
            TextBox2.Text = Fails
        Loop
    End Sub
End Class



I appreciate your input! Thanks!

Originally (And incorrectly) posted in VB6 Forums. I am using VStudio 2012 Professional.

Is This A Good Question/Topic? 0
  • +

Replies To: No text output on ping loop

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5674
  • View blog
  • Posts: 12,188
  • Joined: 02-June 10

Re: No text output on ping loop

Posted 19 January 2013 - 02:45 PM

Freezing is because you have the work taking place on the same thread as the GUI, so its got the entire thread locked up in this never ending loop.

Simplest fix is to stop hammering the ping 1,000 times a second.
Create a timer and in the timer .Tick event do a ping.
Then you can set the timer to every 5 seconds or so.

The correct fix would be to to that *AND* put it on its own thread so as to not tie up the GUI at all.
Was This Post Helpful? 0
  • +
  • -

#3 Absolute Zero  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 21-June 11

Re: No text output on ping loop

Posted 19 January 2013 - 02:51 PM

View PosttlhIn`toq, on 19 January 2013 - 02:45 PM, said:

Freezing is because you have the work taking place on the same thread as the GUI, so its got the entire thread locked up in this never ending loop.

Simplest fix is to stop hammering the ping 1,000 times a second.
Create a timer and in the timer .Tick event do a ping.
Then you can set the timer to every 5 seconds or so.

The correct fix would be to to that *AND* put it on its own thread so as to not tie up the GUI at all.


Thanks. I'll do that. Any idea on the lack of text output? Are those just in the wrong place?

Edit: I moved all from the button to Timer1.Tick and changed the button to Timer1.Start

This post has been edited by Absolute Zero: 19 January 2013 - 02:54 PM

Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5674
  • View blog
  • Posts: 12,188
  • Joined: 02-June 10

Re: No text output on ping loop

Posted 19 January 2013 - 02:56 PM

Some of my common tips (some may apply more than others to your specific style):
  • Take the extra 3 seconds to rename your controls each time you drag them onto a form. The default names of button1, button2... button54 aren't very helpful. If you rename them right away to something like btnOk, btnCancel, btnSend etc. it helps tremendously when you make the methods for them because they are named after the button by the designer.btnSend_Click(object sender, eventargs e) is a lot easier to maintain than button1_click(object sender, eventargs e)

  • You aren't paying for variable names by the byte. So instead of variables names of a, b, c go ahead and use meaningful names like index, timeOut, row, column and so on. You should avoid 'T' for the timer. Amongst other things 'T' is commonly used throughout C# for Type and this will lead to problems. There are naming guidelines you should follow so your code confirms to industry standards. It makes life much easier on everyone around you, including those of us here to help. If you start using the standards from the beginning you don't have to retrain yourself later.
    You might want to look at some of the naming guidelines. Its a lot easier to start with good habits than to break bad habits later and re-learn.



  • Don't use your GUI objects as your variable. In other words don't keep referencing TextBox4.Text everyplace. TextBox4.Text is not a variable or property. The GUI is on its own thread so as soon as you start doing multi-threading you're screwed because your worker thread can't access the GUI elements. Use properties.

  • Try to avoid having work actually take place in GUI control event handlers. It is better to have the GUI handler call other methods so those methods can be reused and make the code more readable. This is also how you can send parameters rather than use excessive global variables. Get in this habit even if you are using WinForms because WPF works a lot under the idea of "commands" and this will get you working towards that. Think of each gester, control click, menu option etc. as a command to do something such as a command to SAVE. It doesn't matter where the command comes from, all sources should point at the same target to do the actual saving.
    Spoiler


  • Don't replace lines of code that don't work. Instead comment them out and put your new attempts below that. This will keep you from re-trying the same ideas over and over. Also, when you come back to us saying "I've tried this 100 different ways and still can't get it", we can actually see what you tried. So often a failed attempt is very very close and just needs a little nudge in the right direction. So if we can say "See what you did in attempt 3... blah blah" it helps a lot

    Spoiler

    If you are using Visual Studio you can select a block of lines and hit control+k control+c (Kode Comment) to comment it out. control+k control+u (Kode Uncomment) to uncomment a selected block.


  • You have to program as if everything breaks, nothing works, the cyberworld is not perfect, the attached hardware is flakey, the network is slow and unreliable, the harddrive is about to fail, every method will return an error and every user will do their best to break your software. Confirm everything. Range check every value. Make no assumptions or presumptions.

  • I strongly suggest installing VMware or some other virtualization technology on your development PC so you can create a couple virtual computers for testing. This would allow you to debug and test inside: WinXP32, XP64, Vista, Win7x32, Win7x64... etc. without having to actually have 5 physical PC's. Visual Studio will let you send the debug directly into one of these virtual machines so you can watch it operate, check its variables, see the crashes and so on just as if it were debugging on your real machine.

  • This can't be stressed enough in today's world of cell phone messaging:
    Don't use txt/sms/leet/T9 speak like: u no, u r, dnt, wut i m do-n, coz, al gud, b4, ny1, sum1, please and so on like this guy:

    Spoiler


View PostAbsolute Zero, on 19 January 2013 - 03:51 PM, said:

Any idea on the lack of text output? Are those just in the wrong place?


The thread is too busy handling your work loop to update the form. The work is getting done, the textboxes technically are getting new text but the form is so badly hammered that it doesn't have time to repaint itself.

You might also get in the habit of appending the time to the output. Otherwise when the textbox.text changes from "failed" to "failed" you wouldn't see any change. But if it changes from "15:45:30.123 Failed" to "15:46:15.789 Failed" you would at least see the change in the time even though the status remained the same.
Was This Post Helpful? 0
  • +
  • -

#5 Absolute Zero  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 21-June 11

Re: No text output on ping loop

Posted 19 January 2013 - 02:58 PM

Hm, I thought that changing it to every five seconds or so would help that. Apparently not.
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5674
  • View blog
  • Posts: 12,188
  • Joined: 02-June 10

Re: No text output on ping loop

Posted 19 January 2013 - 03:04 PM

Did you use a timer to have it poll every 5 seconds?
Or did you just do thread.sleep(5000) which sleeps the entire thread so again it still doesn't do anything including repaint the form?

Let's see the new code.
Was This Post Helpful? 0
  • +
  • -

#7 Absolute Zero  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 21-June 11

Re: No text output on ping loop

Posted 19 January 2013 - 03:08 PM

Yeah, I may be doing this completely wrong. I just set the Timer's interval to 5000, then had the button start it. I'm going to work now, though, so I'll check the thread when I get home. Thanks for your help today.

Imports System.Net.NetworkInformation

Public Class Form1
    Dim Fails As Integer = 0
    Dim Downage As Boolean = False
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Timer1.Start()
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        Do Until Downage = True
            Dim Pingas As Integer = 0
            Dim Server As String = "127.0.0.1"
            Dim TimeOut As Integer = 200
            Dim PingServer As Ping = New Ping()
            Dim Reply As PingReply = PingServer.Send(Server)
            If Reply.Status = IPStatus.Success Then
                Pingas += 1
            End If
            TextBox1.Text = Pingas
            If Reply.Status = IPStatus.TimedOut Then
                Fails += 1
            End If
            If Fails >= 15 Then
                Downage = True
            End If
            TextBox1.Text = Pingas
            TextBox2.Text = Fails
        Loop
        Me.Refresh()
    End Sub
End Class


Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5674
  • View blog
  • Posts: 12,188
  • Joined: 02-June 10

Re: No text output on ping loop

Posted 19 January 2013 - 03:16 PM

You haven't changed the basic concept of what is happening; you've only moved the loop.

Comment out lines 11 and 29

That way it checks just one time per 5 seconds.
Was This Post Helpful? 0
  • +
  • -

#9 Absolute Zero  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 21-June 11

Re: No text output on ping loop

Posted 19 January 2013 - 09:28 PM

Thanks for the help; I have it working. I appreciate your time. I was on the verge of : :hang:
Was This Post Helpful? 0
  • +
  • -

#10 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5674
  • View blog
  • Posts: 12,188
  • Joined: 02-June 10

Re: No text output on ping loop

Posted 20 January 2013 - 07:52 AM

Glad to help. If you don't mind would you share your working solution? That's how 'community' works best, so the next guy with the same issue can get some help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1