Sunday, August 24, 2008

Ping in VB.NET 2005 with Async method using multiple IP's

Ping in VB.NET 2005 with Async method using multiple IP's

Private mPingAddresses As List(Of String)

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
mPingAddresses = New List(Of String)
' Initialise the list of addresses...
mPingAddresses.Add("10.0.0.110")
mPingAddresses.Add("10.0.0.128")
mPingAddresses.Add("10.0.0.197")
mPingAddresses.Add("192.168.1.1")
mPingAddresses.Add("192.168.1.2")
mPingAddresses.Add("192.168.0.2")
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' Clear any existing items
Me.ListBox1.Items.Clear()
' Loop through the addresses generating an aysnc ping...
For Each ipAddy As String In mPingAddresses
' Generate the request
Dim myPing As New Net.NetworkInformation.Ping()
' Add the handler for this request...
AddHandler myPing.PingCompleted, AddressOf PingRequestCompleted
myPing.SendAsync(ipAddy, ipAddy)
Next
End Sub

Public Sub PingRequestCompleted(ByVal sender As Object, ByVal e As Net.NetworkInformation.PingCompletedEventArgs)
' When received, add the approrpiate entry into the listbox
Me.ListBox1.Items.Add(e.UserState.ToString & " " & e.Reply.Status)
End Sub

No comments: