title: Large amount of pings in async task - getting Exception "An asynchronous call is already in progress."
body: <p>I posted a previous question about this, I found a sort of working method using threads - but it was causing reliability issues when variables were rewritten between threads.  I was pointed towards using ping async and tasks, but I'm getting the following exception:</p>
  
  <pre><code>Error: System.InvalidOperationException: An asynchronous call is already in progress. It must be completed or canceled before you can call this method.
  </code></pre>
  
  <p>That points specifically to the "await PingAsync(HostList)" and "await Task.WhenAll(tasks)"</p>
  
  <p>Heres the code:</p>
  
  <pre><code>            MySqlConnection Connection = new MySqlConnection(ConnectionString);
              Connection.Open();
              MySqlCommand query = Connection.CreateCommand();
              query.CommandText = "SELECT obj_id AS id,ip FROM objects LIMIT 15";
              MySqlDataReader Nodes = query.ExecuteReader();
  
              // Record in log that a NEW iteration is starting - for tracking issues with pings
              Library.WriteErrorLog("######################## New Iteration ########################");
  
              var HostList = new List&lt;HostObject&gt;();
  
              int i = 1;
              while (Nodes.Read())
              {
                  var Host = new HostObject();
                  Host.HostName = Nodes["ip"].ToString();
                  Host.HostID = Nodes["id"].ToString();
                  HostList.Add(Host);
                  i++;
              }
  
              Connection.Close();
  
              var results = await PingAsync(HostList);
              bool WaitingOnPings = false;
              while (WaitingOnPings == false)
              {
                  if (results != null)
                  {
                      foreach (PingReply result in results)
                      {
                          Library.WriteErrorLog(result.Status.ToString());
                      }
  
                      WaitingOnPings = true;
                  }
                  else
                  {
                      Thread.Sleep(500);
                  }
              }
  </code></pre>
  
  <p>and :</p>
  
  <pre><code>private async Task&lt;List&lt;PingReply&gt;&gt; PingAsync(List&lt;HostObject&gt; HostList)
      {
          Ping pingSender = new Ping();
          var tasks = HostList.Select(HostName =&gt; pingSender.SendPingAsync(HostName.ToString(), 2000));
          var results = await Task.WhenAll(tasks);
  
          return results.ToList();
      }
  </code></pre>
  
  <p>Thanks for the help!</p>