I’m using VB 2010 Express.
I have a loop that calls a function named “singlePing” which pings an IP Address.
If the application succeeded to reach that IP it turns a certain label back-color into green,
otherwise – red. (There’s a label for each ip address)
When I’m running my application it seems that my application is “stuck” (actually it runs that loop) and after a while it updates the back-color of ALL THE LABLES at once.
Why it doesn’t update each label at a time?
It makes the “user” think that the application got stuck.
Here’s my code:
Private Sub btnActivateMonitor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnActivateMonitor.Click
Dim xmldoc As New XmlDataDocument()
Dim xmlnode As XmlNodeList
Dim fs As New FileStream(acXmlFileName, FileMode.Open, FileAccess.Read)
Dim i As Integer
Dim name As String
Dim lbl As String
Dim ip As String
xmldoc.Load(fs)
xmlnode = xmldoc.GetElementsByTagName("Unit")
For i = 0 To xmlnode.Count - 1
name = xmlnode(i).ChildNodes.Item(0).InnerText.Trim()
lbl = xmlnode(i).ChildNodes.Item(1).InnerText.Trim()
ip = xmlnode(i).ChildNodes.Item(2).InnerText.Trim()
checkUnitStatus(name, lbl, ip)
Next
End Sub
Sub checkUnitStatus(ByVal name As String, ByVal lbl As String, ByVal ip As String)
rtbLog.Text = rtbLog.Text & TimeValue(Now) & " Checking ..." & name & "(" & ip & ") ..........."
If sendPing(ip) Then
Me.Controls(lbl).BackColor = Color.Green
rtbLog.Text = rtbLog.Text & "success" & vbCrLf
Else
Me.Controls(lbl).BackColor = Color.Red
rtbLog.Text = rtbLog.Text & "Failed" & vbCrLf
End If
End Sub
'+--------------------------------------
'| Send Ping (ip)
'+--------------------------------------
Function sendPing(ByVal ip As String)
Dim reachable As Boolean = False
Try
reachable = My.Computer.Network.IsAvailable AndAlso _
My.Computer.Network.Ping(ip, 10000)
Catch pingException As System.Net.NetworkInformation.PingException
Catch genericNetworkException As System.Net.NetworkInformation.NetworkInformationException
' Fail silently and return false
End Try
Return reachable
End Function
you should refresh the ui inside the Loop.
ie after calling
checkUnitStatus