I have the below coded module which performs an asyncronous ping, so it does not go to the next server in my datagridview until the curent server responds. I would like to be able to issue all the servers pinging at the same time. Can this code be changed to do that?
Code:
Imports System.Net.NetworkInformation
Imports System.Text
Module MultiPing
Sub MultiPingSystem(ByVal HostName As String)
'Ping the system and display results to the txtPingResult control
'Clear any previous ping results
pingresults = ""
Dim pingsender As New Ping
Dim options As New PingOptions
'Modify the default fragmentation behavior
options.DontFragment = True
'Create a buffer of 32 bytes of data to be transmitted.
Dim data As String = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
Dim buffer As Byte() = Encoding.ASCII.GetBytes(data)
Dim timeout As Integer = 120
Try
'Attempt ping
Dim reply As PingReply = pingsender.Send(HostName, timeout, buffer, options)
If (reply.Status = IPStatus.Success) Then
pingresults = "Success"
lasttimeout = ""
roundtriptime = GetMs(reply.RoundtripTime)
Else
pingresults = "Ping Failed"
End If
'Pause for 1 second before pinging again
System.Threading.Thread.Sleep(1000)
'Next
Catch ex As Exception
pingresults = "Ping Failed"
End Try
End Sub
Function GetMs(ByVal ResponseTime As Integer) As String
'accept and integer value and return a friendly string
' reflecting number of milliseconds
If ResponseTime = 0 Then
Return "<1ms"
Else
Return String.Format("={0}ms", ResponseTime.ToString)
End If
End Function
End Module