/// <summary> /// Loops through the IP address and does the pings. /// </summary> /// <param name="o">Start and end IP addresses to ping.</param> private void pingWorker_DoWork(object o) { PingRange pingRange = (PingRange)o; // // Get the starting and ending address as a byte array to make it easier to // loop through. // byte[] start = pingRange.StartRange.GetAddressBytes(); byte[] end = pingRange.EndRange.GetAddressBytes(); LinkedList <Thread> threads = new LinkedList <Thread>(); // // Loop through each octet in the IP address, and ping on a background thread. // for (byte o0 = start[0]; o0 <= end[0]; o0++) { for (byte o1 = start[1]; o1 <= end[1]; o1++) { for (byte o2 = start[2]; o2 <= end[2]; o2++) { for (byte o3 = start[3]; o3 <= end[3]; o3++) { pingBlock.WaitOne(); Thread t = new Thread(new ParameterizedThreadStart(DoPing)); t.Start(new IPAddress(new byte[] { o0, o1, o2, o3 })); threads.AddLast(t); } } } } // // Wait until all the pings are done. // foreach (Thread ln in threads) { ln.Join(); } // // Raise an event saying the pings are done on the main UI thread. // try { this.Invoke(new RaiseNetScanCompleteHandler(RaiseNetScanComplete)); } catch (InvalidOperationException) { // Can happen if the application is closed while pings are going on in // the background. Safe to ignore. } }
/// <summary> /// Starts ping operations, running on background thread /// </summary> /// <param name="pr">Contains the starting and ending IP address for the pings.</param> public void Start(PingRange pr) { Thread t = new Thread(new ParameterizedThreadStart(pingWorker_DoWork)); t.Start(new PingRange(pr.StartRange, pr.EndRange)); }