Hello,
i am working on some kind of network monitoring tool for Windows and Mac (not to be released). With this tool i need to ping 800+ stations as fast and as resource saving as possible. The online status of each station is populated in a Listbox.
For this i have a dictionary with ip addresses as keys and the ping results as values. To make it Windows and OS X compatible, i use the Shell and the PING command. But i am open for any other cross platform solution.
I do not want to slow down the GUI of my App, because it performs many other Tasks at the same time. User interactions are in most tasks involved.
That’s why i use a Thread which performs the PINGs and updates the dictionary. Plus a timer which checks each row in my Listbox (one column holds the ip addresses in the cell and the ping result in the celltags) and updates the celltags of the ip addresses column. The cellbackgroundpaint event then colors the rows dependent of the online/offline status.
The following code is a quick and (VERY) dirty solution, i wrote just to get it to work. I am pretty sure it can be done much more elegant and smarter. If someone could please help me “optimizing” this code, i would be very happy.
[code] RunningThreads.Increase
Dim IPAdressen(-1) As String
Dim d As New Date
Dim theShell1 As New Shell
Dim theShell2 As New Shell
Dim theShell3 As New Shell
Dim theShell4 As New Shell
Dim theShell5 As New Shell
Dim theShell6 As New Shell
#If TargetWin32 Then
theShell1.Timeout = -1
theShell2.Timeout = -1
theShell3.Timeout = -1
theShell4.Timeout = -1
theShell5.Timeout = -1
theShell6.Timeout = -1
#EndIf
theShell1.Mode = 2
theShell2.Mode = 2
theShell3.Mode = 2
theShell4.Mode = 2
theShell5.Mode = 2
theShell6.Mode = 2
PingeVerteilerNummer = PingResult.Count
AnzahlPingTestDurchlaeufeBisher = AnzahlPingTestDurchlaeufeBisher + 1
DauerLetzterDurchlauf = d.TotalSeconds-StartDesDurchlaufs
StartDesDurchlaufs = d.TotalSeconds
For Each Value As Variant In PingResult.Keys
IPAdressen.Append Value.StringValue
Next
Dim x,y As Integer
y = IPAdressen.Ubound
For x = 0 To y Step 6
PingeVerteilerNummer = PingeVerteilerNummer - 6
If TargetWin32 Then
theShell1.Execute "ping -n 1 -w 500 " + IPAdressen(x)
If x+1<=y Then theShell2.Execute "ping -n 1 -w 500 " + IPAdressen(x+1)
If x+2<=y Then theShell3.Execute "ping -n 1 -w 500 " + IPAdressen(x+2)
If x+3<=y Then theShell4.Execute "ping -n 1 -w 500 " + IPAdressen(x+3)
If x+4<=y Then theShell5.Execute "ping -n 1 -w 500 " + IPAdressen(x+4)
If x+5<=y Then theShell6.Execute "ping -n 1 -w 500 " + IPAdressen(x+5)
Else
theShell1.Execute "ping -c 1 -t 1 " + IPAdressen(x)
If x+1<=y Then theShell2.Execute "ping -c 1 -t 1 " + IPAdressen(x+1)
If x+2<=y Then theShell3.Execute "ping -c 1 -t 1 " + IPAdressen(x+2)
If x+3<=y Then theShell4.Execute "ping -c 1 -t 1 " + IPAdressen(x+3)
If x+4<=y Then theShell5.Execute "ping -c 1 -t 1 " + IPAdressen(x+4)
If x+5<=y Then theShell6.Execute "ping -c 1 -t 1 " + IPAdressen(x+5)
End If
Do
App.DoEvents(40)
Loop Until Not theShell1.IsRunning And Not theShell2.IsRunning And Not theShell3.IsRunning And Not theShell4.IsRunning And Not theShell5.IsRunning And Not theShell6.IsRunning
If theShell1.Errorcode <> 0 Then
// alert or handle ping failure
PingResult.Value(IPAdressen(x)) = False
Else
PingResult.Value(IPAdressen(x)) = True
End If
If x+1<=y Then
If theShell2.Errorcode <> 0 Then
// alert or handle ping failure
PingResult.Value(IPAdressen(x+1)) = False
Else
PingResult.Value(IPAdressen(x+1)) = True
End If
End If
If x+2<=y Then
If theShell3.Errorcode <> 0 Then
// alert or handle ping failure
PingResult.Value(IPAdressen(x+2)) = False
Else
PingResult.Value(IPAdressen(x+2)) = True
End If
End If
If x+3<=y Then
If theShell4.Errorcode <> 0 Then
// alert or handle ping failure
PingResult.Value(IPAdressen(x+3)) = False
Else
PingResult.Value(IPAdressen(x+3)) = True
End If
End If
If x+4<=y Then
If theShell5.Errorcode <> 0 Then
// alert or handle ping failure
PingResult.Value(IPAdressen(x+4)) = False
Else
PingResult.Value(IPAdressen(x+4)) = True
End If
End If
If x+5<=y Then
If theShell6.Errorcode <> 0 Then
// alert or handle ping failure
PingResult.Value(IPAdressen(x+5)) = False
Else
PingResult.Value(IPAdressen(x+5)) = True
End If
End If
If AppIsQuitting Then Exit
Next
RunningThreads.Decrease[/code]