Pausing a sub

gkellogg

New member
Joined
Oct 26, 2006
Messages
3
Location
detroit, Mi
Programming Experience
Beginner
I am in the process of making a program that does continuous pings on a target. Basically it pings the target and returns back a response (Successful, host unreachable, etc) which is then added to a listbox. So far The program crashes if I attempt to put the code in a Do Loop. Based on this I then proceeded to add in a control feature to limit the number of loops (I set it to 10). This does not crash the application, but the responses are added too quickly to the listbox. Is there a way to slow down the loop so that each items.add into the list box are maybe 1-2 seconds apart? I have tried using thread.sleep but it pauses before anything is added to the listbox, then adds all 10 items. Here is my source code.

Private Sub ping()
Dim i As Integer
Do Until i = 10
Dim LongIP As UInt32
Dim Buffer As String
Dim hIP As Integer
Dim timeout As Short
Buffer = Space(32)
LongIP = convertIPtoLong((tbIP.Text))
hIP = IcmpCreateFile()
pIPo.TTL = 255
timeout = 2000
IcmpSendEcho(hIP, LongIP, Buffer, Len(Buffer), pIPo, pIPe, Len(pIPe) + 8, timeout)
Dim response = describeResponse(pIPe.status).ToString()
ListBox1.Items.Add(response)
Thread.Sleep(500)
i = i + 1
Loop
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If tbIP.Text = "" Then
MsgBox("Please Enter an IP Address")
Else
ping()
End If
End Sub
 
Using Thread.Sleep is correct, and even if it appears for you that 'nothing happens then everything happens at once' this is not what actually goes. Because the loop processes continually only stopped by thread.sleep that freezed that thread entirely there is no letting go of the application to let it paint its controls. Add the Application.DoEvents() command inside the loop to release control for a splitsecond and enable painting during the loop. The reason for this is that you run the loop on the main thread, if you had created a new thread to do this work the main thread would not be busy with this. Multithreading of course raises the stakes a little, so if you want to do this you have to read some on the topic, nothing terrible difficult - read a few tutorials if interested, also look up BackGroundWorker component that eases multithreading for the beginner.
 
I actually tried separating this sub on a new thread, unfortunately I get an error saying listbox1.items.add is not on the same thread and cross threading is not allowed.
 
I actually tried separating this sub on a new thread, unfortunately I get an error saying listbox1.items.add is not on the same thread and cross threading is not allowed.
That is one of the main concepts of multithreading you have to read some about. I would think any multithreading tutorial will tell you about this. This situation also one of the main benefits of our little helper the BackGroundWorker mentioned, its events operate on the calling thread so especially the beginner don't have to deal with the delegates. (of course not only the beginner find simple and good uses for this component)
The application.Doevents works great though. Thanks a lot for your help!
Yes, it can be easy and useful for many cases where multithreading is not needed.
 
Back
Top