title: Asynchronous HTTP requests throws IndexOutOfRange Exception in Generic List
body: <p>I'm working on a Windows Service that cycles through a series of databases and then sends SMS messages using HTTP requests. In the beginning of the method that goes through each database, I define a generic list as follows:</p>
  
  <pre><code>public static List&lt;Recipient&gt; Recipients = new List&lt;Recipient&gt;();
  </code></pre>
  
  <p>I populate that list with the results of an asynchronous HTTP request sent through a thread pool:</p>
  
  <pre><code>//inside main method                    
  foreach (var SMS in SMSJoin)
  {
      ...
  
      SMSMessage oSMS = new SMSMessage(Keyword, Number, Message, MessageID);
  
      ThreadPool.QueueUserWorkItem(SendSMS, oSMS);
  }
  </code></pre>
  
  <p>Then it gets passed to the next method:</p>
  
  <pre><code>public static void SendSMS(object SMStoSend)
  {
      try
      {
          SMSMessage oSMS = (SMSMessage)SMStoSend;
          ...
          Request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), state);
      }
      ...
  }
  </code></pre>
  
  <p>Then to <code>GetRequestStreamCallback</code>...</p>
  
  <pre><code>public static void GetRequestStreamCallback(IAsyncResult AsynchronousResult)
  {
      State state = (State)AsynchronousResult.AsyncState;
      SMSMessage oSMS = state.oSMS;
      try
      {
          ...
          Request.BeginGetResponse(new AsyncCallback(ReceiveResponse), state);
      }
      ...
  }
  </code></pre>
  
  <p>And finally the response is received and added to the Recipients List:</p>
  
  <pre><code>public static void ReceiveResponse(IAsyncResult Result)
  {
      ...
      finally
      {
          oSMS.Complete = DateTime.Now;
  
          Recipient CompleteMessage = new Recipient(oSMS.MessageID, oSMS.Recipient, oSMS.ErrorCode, oSMS.Complete, oSMS.ResponseCode);
          Recipients.Add(CompleteMessage);
      }
  </code></pre>
  
  <p>At the end of the code, each response should be added to a <code>Recipient</code> object and then stored in the generic list. The problem is that once about every 1000 or so SMS messages (sent in batches of 50), there is an unhandled <code>IndexOutOfRangeException</code> thrown by the line of code <code>Recipients.Add(CompleteMessage)</code>.</p>
  
  <p>I didn't think that there were indexes in the list, and many of the recipients are duplicates. Why would it throw this error, and why only every once in a good while? Could this have to do with threading issues? </p>
  
  <p>As a note, I'm activating the main method from a <code>Timer</code> object, but it will wait until the previous instance is complete before starting a new instance.</p>
  
  <p><strong>Edit:</strong>
  Here is the <code>Recipient</code> class:</p>
  
  <pre><code>public class Recipient
  {
      public Recipient(long MessageID, string PhoneNumber, string ErrorMessage, DateTime? Completed, string Response)
      {
          this.MessageID = MessageID;
          this.PhoneNumber = PhoneNumber;
          this.ErrorMessage = ErrorMessage;
          this.Completed = Completed;
          this.Response = Response;
      }
  
      public long MessageID { get; set; }
      public string PhoneNumber { get; set; }
      public string ErrorMessage { get; set; }
      public DateTime? Completed { get; set; }
      public string Response { get; set; }
  }
  </code></pre>