Hi Ryan,
For such scenario, I recommend use several BackgroundWorke r instances to do
the ping and update UI to report IP address's status.
First, we need to create a class to provide the next IP address that needs
to ping:
Public Class IpProvider
Private _index As Integer = 0
Public ReadOnly Property GetNextIpAddres s() As String
Get
SyncLock GetType(IpProvi der)
If _index = 255 Then Return Nothing
_index = _index + 1
Return "192.168.1. " + _index.ToString ()
End SyncLock
End Get
End Property
End Class
Here I'm using a hardcoded subnet for demostrating purpose. You may need to
use some properties to set the specified subnet.
Add a ListBox to your Form which will display each IP address's status. Add
two buttons, the first one will be used to start the ping threads, the
second one will be used to cancel the ping threads. Here we will use 5
threads to do the ping, each thread will use the IpProvider to get the next
ip address to ping.
Const THREAD_COUNT As Integer = 5
Private bws(THREAD_COUN T - 1) As BackgroundWorke r
Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
Dim ipp As New IpProvider
For i As Integer = 0 To THREAD_COUNT - 1
Dim bw As New BackgroundWorke r
bw.WorkerReport sProgress = True
bw.WorkerSuppor tsCancellation = True
AddHandler bw.DoWork, AddressOf bw_DoWork
AddHandler bw.ProgressChan ged, AddressOf bw_ProgressChan ged
AddHandler bw.RunWorkerCom pleted, AddressOf
bw_RunWorkerCom pleted
bw.RunWorkerAsy nc(ipp)
bws(i) = bw
Next
End Sub
Private Sub bw_DoWork(ByVal sender As Object, ByVal e As
DoWorkEventArgs )
Dim bw As BackgroundWorke r = CType(sender, BackgroundWorke r)
e.Result = PingIp(e.Argume nt, bw, e)
End Sub
Private Sub bw_ProgressChan ged(ByVal sender As Object, ByVal e As
ProgressChanged EventArgs)
ListBox1.Items. Add(e.UserState )
End Sub
Private Sub bw_RunWorkerCom pleted(ByVal sender As Object, ByVal e As
RunWorkerComple tedEventArgs)
If (e.Error IsNot Nothing) Then
ListBox1.Items. Add(e.Error.Mes sage)
ElseIf e.Cancelled Then
Else
End If
End Sub
Private Function PingIp(ByVal ipp As IpProvider, ByVal bw As
BackgroundWorke r, ByVal e As DoWorkEventArgs ) As Boolean
While True
If bw.Cancellation Pending Then
e.Cancel = True
Return False
Else
Dim ipaddress = ipp.GetNextIpAd dress()
If ipaddress Is Nothing Then Exit While
' insert code to ping the address, here we're using Sleep()
to simulate a delay
Thread.Sleep(10 00)
' then we report the result, here we don't care the
progress percentage
bw.ReportProgre ss(0, ipaddress + " is alive")
End If
End While
Return True
End Function
Private Sub Button2_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button2.Click
For i As Integer = 0 To THREAD_COUNT - 1
bws(i).CancelAs ync()
Next
End Sub
Hope this helps. Please feel free to post here if anything is unclear.
Sincerely,
Walter Wang (wa****@online. microsoft.com, remove 'online.')
Microsoft Online Community Support
=============== =============== =============== =====
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
=============== =============== =============== =====
This posting is provided "AS IS" with no warranties, and confers no rights.