title: Multiple asynchronous method calls in a loop Window Phone 7
body: <p>I have a collection of picture Objects for which I need to download thumbs and pictures files located on dataservise, how can I managed this? 
  In this method I have loop to call three methods; one to add objects to data base, second to download and save picture thumb and third to download and save picture file the other two is <code>ClientOpenReadCompleted</code> methods. </p>
  
  <pre><code>public bool AddAllPhoto()
  {
    var amount = App.ViewModel.NewPictures.Count;
    for (int i = 0; i &lt; amount; i++)
    {
      //to add picture to DB  
      SavePicture(App.ViewModel.NewPictures[i]);
  
      DownloadPicture(NewPictures[i].ID.ToString());
      DownloadPictureThumb(NewPictures[i].ID.ToString()));
    }
  
    return true;
  }
  </code></pre>
  
  <p>Second;</p>
  
  <pre><code>public void DownloadPictureThumb(string path)
  {
    string outputString = String.Format("http://" + App.ServerAdress + "/ /Pictures/Thumbs/{0}.jpg", path);
    var client = new WebClient();
    client.OpenReadCompleted += ClientOpenReadCompleted1;
    client.OpenReadAsync(new Uri(outputString));
  }
  
  private static void ClientOpenReadCompleted1(object sender, OpenReadCompletedEventArgs e)
  {
    var resInfo = new StreamResourceInfo(e.Result, null);
    var reader = new StreamReader(resInfo.Stream);
  
    byte[] contents;
    using (var bReader = new BinaryReader(reader.BaseStream))
    {
      contents = bReader.ReadBytes((int)reader.BaseStream.Length);
    }
  
    var file = IsolatedStorageFile.GetUserStoreForApplication();
  
    var thumbFilePath = String.Format(PicturesThumbsColectionKey + "{0}", PictureDataStoreLocal.ID);
  
    var stream = thumbFile.CreateFile(thumbFilePath);
  
    stream.Write(contents, 0, contents.Length);
    stream.Close();
  }
  </code></pre>
  
  <p>And third one</p>
  
  <pre><code>public void DownloadPicture(string path)
  {
    string outputString = String.Format("http://" + App.ServerAdress + "/Pictures/{0}.jpg", path);
    var client = new WebClient();
    client.OpenReadCompleted += ClientOpenReadCompleted1;
    client.OpenReadAsync(new Uri(outputString));
  }
  
  private static void ClientOpenReadCompleted1(object sender, OpenReadCompletedEventArgs e)
  {
    var resInfo = new StreamResourceInfo(e.Result, null);
    var reader = new StreamReader(resInfo.Stream);
  
    byte[] contents;
    using (var bReader = new BinaryReader(reader.BaseStream))
    {
      contents = bReader.ReadBytes((int)reader.BaseStream.Length);
    }
  
    var file = IsolatedStorageFile.GetUserStoreForApplication();
  
    IsolatedStorageFileStream stream = file.CreateFile(PictureDataStoreLocal.ID.ToString());
  
    stream.Write(contents, 0, contents.Length);
    stream.Close();
  }
  </code></pre>