whipped up this code real quick which uses a ThreadPool on Form Load to iterate through my subnet and tell me if there's a device communicating at each IP address.

Works great.

some questions:
Why am I able to update BOTH my label at the top AND the listbox, but I'm only using an .Invoke on the Listbox?

How can I know how many threads are currently being used while that is executing?

What does the SyncLock do?

I'm aware some people don't like the use of Application.DoEvents(). Is it ok to be using it in this instance or is there a better practice?

For those wanting to try it out, drag a Label and a Listbox to a new form. Label is called "currentIPTextbox", Listbox is default name.

vb.net Code:
  1. Imports System.Threading
  2.  
  3. Public Class Form1
  4.     Private Elem As String
  5.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  6.         ListBox1.Items.Clear()
  7.         ThreadPool.QueueUserWorkItem(AddressOf Initialize)
  8.     End Sub
  9.  
  10.     Private Sub Add()
  11.         Dim pingresult As String
  12.         pingresult = My.Computer.Network.Ping("192.168.1." & Elem, 200)
  13.         ListBox1.Items.Add("192.168.1." & Elem & " : " & pingresult)
  14.         currentIPTextbox.Text = "192.168.1." & Elem
  15.         Application.DoEvents()
  16.     End Sub
  17.  
  18.     Private Sub Initialize(ByVal State As Object)
  19.         Dim I As Long
  20.         SyncLock ListBox1.GetType
  21.             For I = 1 To 255
  22.                 Elem = I
  23.                 ListBox1.Invoke(CType(AddressOf Add, MethodInvoker))
  24.             Next
  25.         End SyncLock
  26.     End Sub
  27. End Class