title: How do i wait for a load of asyncronous pings to all complete in C#?
body: <p>I am using the code below to start a load of asyncronous pings. I also have a function which is called by PingCompletedEventHandler, this function adds the address of a successfull ping to a list for use later.</p>
  
  <p>I would like to know how It would be possible to call a function once all of the ping threads have completed?</p>
  
  <pre><code>    public static void PingRange(IPRange range)
      {
  
          foreach (IPAddress ip in range.GetAllIP())
          {
              // Create Pinger
              AutoResetEvent waiter = new AutoResetEvent(false);
              Ping pingSender = new Ping();
              pingSender.PingCompleted += new PingCompletedEventHandler(pingSender_PingCompleted);
  
              // Create a 32 byte data buffer
              string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
              byte[] buffer = Encoding.ASCII.GetBytes(data);
  
              // Wait 4 seconds for a reply
              int timeout = 4000;
  
              PingOptions options = new PingOptions(64, true);
  
              pingSender.SendAsync(ip, timeout, buffer, options, waiter);
              PingStartCount++;
          }
      }
  </code></pre>