Parallel ping by using Parallel.For RRS feed

  • Question

  • I have a case where I need to ping a few thousand possible addresses and found that Parallel.For helps me.
    However, a simple test (code below) shows that pinging 254 addresses takes approximately 14 seconds (46 replies out of 254 possible).
    I hoped that this could be done faster, is my code optimal for this?

    Update: Sorry for asking, found the solution my self, however others may be interested.
    The ThreadPool only handed out 8 threads as a maximim which means that the loop was divided in quite a few "chunks" executing more-or-less in serial instead of parallel. ThreadPool.SetMinThreads fixed the problem (or at least it is one way of fixing/reducing the problem)

                List<int> activeNodes = new List<int>();
                Parallel.For(1, 255, (nodeNumber) =>
                    string ip = $"192.168.130.{nodeNumber}";
                    System.Net.NetworkInformation.Ping p = new Ping();
                    System.Net.NetworkInformation.PingReply reply = p.Send(IPAddress.Parse(ip), 10);
                    if (reply.Status == IPStatus.Success)
                        Debug.WriteLine($"Got reply from {ip}, now {activeNodes.Count()} active nodes in list");

    • Edited by EuroEager Thursday, June 21, 2018 11:14 AM
    Thursday, June 21, 2018 10:30 AM


  • Just to not leave unanswered or deleted (for others), I choose to mark this post as an answer my self.
    • Marked as answer by EuroEager Thursday, June 21, 2018 11:16 AM
    Thursday, June 21, 2018 11:16 AM