digitaldrew
Well-known member
- Joined
- Nov 10, 2012
- Messages
- 167
- Programming Experience
- Beginner
Trying to write a small program that will take each website in a listbox and ping them.
Everything seams to be working fine, however it seams like the loop goes so fast the txtLog doesn't have time to update itself. I've gone in and added a thread.sleep to put a pause in there but it doesn't seam to fix the issue. The program will pause, but the txtLog still doesn't update. However, if I stick a messagebox in the code then it will give me the messagebox that I requested, and then update the text log appropriately before continuing to the next website..
Here is the start button code. Basically, they click Start and it finds out which radio button they have selected so it knows how many times to ping each address. Then it starts the loop where it should check each item..
When it calls the pingIt sub that is where it's actually performing the ping..Here is the code for that
As you see, I have the MsgBox(selectedPing) commented out in there. If I uncomment that then I'll get the correct msgbox with the string and everything after that will perform just fine. However, if I uncomment the msgbox then everything still goes as it should - but the txtLog doesn't update.
Any idea why this happening? What can I do so that it actually updates appropriately?
Everything seams to be working fine, however it seams like the loop goes so fast the txtLog doesn't have time to update itself. I've gone in and added a thread.sleep to put a pause in there but it doesn't seam to fix the issue. The program will pause, but the txtLog still doesn't update. However, if I stick a messagebox in the code then it will give me the messagebox that I requested, and then update the text log appropriately before continuing to the next website..
Here is the start button code. Basically, they click Start and it finds out which radio button they have selected so it knows how many times to ping each address. Then it starts the loop where it should check each item..
VB.NET:
Private Sub btnStartPing_Click(sender As System.Object, e As System.EventArgs) Handles btnStartPing.Click
If lstIps.Items.Count <= 0 Then
MsgBox("Please Add at Least One IP or Website!")
Exit Sub
ElseIf chkOnePing.Checked = True Then
pingNumber = 1
ElseIf chkFourPings.Checked = True Then
pingNumber = 4
ElseIf chkCustom.Checked = True Then
Try
Integer.Parse(txtCustomPing.Text)
pingNumber = Me.txtCustomPing.Text
Catch ex As Exception
MessageBox.Show("Custom Value Must be a Number!")
End Try
Else
MsgBox("Please Select How Many Pings to Send!")
Exit Sub
End If
Do
For i As Integer = 0 To lstIps.Items.Count - 1
lstIps.SelectedIndex = i
selectedPing = lstIps.SelectedItem.ToString()
Call pingIt()
'Thread.Sleep(2000)
Next
pingNumber = pingNumber - 1
Loop Until pingNumber <= 0
MsgBox("Pinging Completed!")
End Sub
When it calls the pingIt sub that is where it's actually performing the ping..Here is the code for that
VB.NET:
Public Sub pingIt()
Dim pingSender As New Ping()
Dim options As New PingOptions()
Dim roundtriptime As String
' Use the default Ttl value which is 128,
' but change the fragmentation behavior.
options.DontFragment = True
' Create a buffer of 32 bytes of data to be transmitted.
Dim data As String = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
Dim buffer() As Byte = Encoding.ASCII.GetBytes(data)
Dim timeout As Integer = 120
'MsgBox(selectedPing)
Dim reply As PingReply = pingSender.Send(selectedPing, timeout, buffer, options)
lblTotalPingsSent.Text = lblTotalPingsSent.Text + 1
If reply.Status = IPStatus.Success Then
MsgBox("Success")
roundtriptime = GetMs(reply.RoundtripTime)
txtLog.AppendText("Ping Successful to: " & selectedPing & " with Roundtrip Time: " & roundtriptime & " at: " & TimeOfDay & vbCrLf)
lblTotalPingsReceived.Text = lblTotalPingsReceived.Text + 1
Exit Sub
Else
txtLog.AppendText("Ping UnSuccessful to: " & selectedPing & " at: " & Now & vbCrLf)
lblTotalPingsLost.Text = lblTotalPingsLost.Text + 1
Exit Sub
End If
End Sub
As you see, I have the MsgBox(selectedPing) commented out in there. If I uncomment that then I'll get the correct msgbox with the string and everything after that will perform just fine. However, if I uncomment the msgbox then everything still goes as it should - but the txtLog doesn't update.
Any idea why this happening? What can I do so that it actually updates appropriately?