title: IAsyncResult and Multithreading in .NET 3.5
body: <p>I'm want to make multiple requests to a remote service and I'm trying an asynchronous/multithread approach.
  For the concurrent part, I'll try this <a href="http://coding-time.blogspot.pt/2008/03/implement-your-own-parallelfor-in-c.html" rel="nofollow">http://coding-time.blogspot.pt/2008/03/implement-your-own-parallelfor-in-c.html</a></p>
  
  <pre><code>IList&lt;string&gt; returnedValues = new List&lt;string&gt;();
  int numberOfRequests = 5;
  object sync = new object();
  ParallelUtilities.For(0, numberOfRequests, delegate(int i)
  {
      string response = string.Empty;
      try
      {
           try
              {
                  Func&lt;int, string&gt; caller = CallService;
                  IAsyncResult result = caller.BeginInvoke(i, null, null);
                  Thread.Sleep(0);
                  string returnValue = caller.EndInvoke(result);
  
                  lock (sync)
                  {
                      returnedValues.Add(returnValue);
                  }
              }
              catch (Exception ex)
              {
                  Console.WriteLine(string.Concat("ex: ", ex.Message, "stack:", ex.StackTrace));
              }
      });
  }
  </code></pre>
  
  <p>And the service call</p>
  
  <pre><code>private string CallService(int index)
  {
      string value;
      using (var channel = GetClientChannel&lt;IService&gt;("http://localhost:51383/Service/Service.svc"))
      {
          value = channel.GetValue(index);
      }
      return value + "for this index:" + index;
  }
  </code></pre>
  
  <p>Is there a better way to do this?
  How do IAsyncResult works in multithreading?</p>