Hi Ryan,
For such scenario, I recommend use several BackgroundWorker 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 GetNextIpAddress() As String
Get
SyncLock GetType(IpProvider)
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_COUNT - 1) As BackgroundWorker
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim ipp As New IpProvider
For i As Integer = 0 To THREAD_COUNT - 1
Dim bw As New BackgroundWorker
bw.WorkerReportsProgress = True
bw.WorkerSupportsCancellation = True
AddHandler bw.DoWork, AddressOf bw_DoWork
AddHandler bw.ProgressChanged, AddressOf bw_ProgressChanged
AddHandler bw.RunWorkerCompleted, AddressOf
bw_RunWorkerCompleted
bw.RunWorkerAsync(ipp)
bws(i) = bw
Next
End Sub
Private Sub bw_DoWork(ByVal sender As Object, ByVal e As
DoWorkEventArgs)
Dim bw As BackgroundWorker = CType(sender, BackgroundWorker)
e.Result = PingIp(e.Argument, bw, e)
End Sub
Private Sub bw_ProgressChanged(ByVal sender As Object, ByVal e As
ProgressChangedEventArgs)
ListBox1.Items.Add(e.UserState)
End Sub
Private Sub bw_RunWorkerCompleted(ByVal sender As Object, ByVal e As
RunWorkerCompletedEventArgs)
If (e.Error IsNot Nothing) Then
ListBox1.Items.Add(e.Error.Message)
ElseIf e.Cancelled Then
Else
End If
End Sub
Private Function PingIp(ByVal ipp As IpProvider, ByVal bw As
BackgroundWorker, ByVal e As DoWorkEventArgs) As Boolean
While True
If bw.CancellationPending Then
e.Cancel = True
Return False
Else
Dim ipaddress = ipp.GetNextIpAddress()
If ipaddress Is Nothing Then Exit While
' insert code to ping the address, here we're using Sleep()
to simulate a delay
Thread.Sleep(1000)
' then we report the result, here we don't care the
progress percentage
bw.ReportProgress(0, ipaddress + " is alive")
End If
End While
Return True
End Function
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
For i As Integer = 0 To THREAD_COUNT - 1
bws(i).CancelAsync()
Next
End Sub
Hope this helps. Please feel free to post here if anything is unclear.
Sincerely,
Walter Wang (
[email protected], remove 'online.')
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.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/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.