title: Using webclient to download file in a WPF application
body: <p>I'm making a WPF application. 
  I'm using WebClient to download files. I have a list of the name of the files that should be downloaded from a current path. I use an foreach to iterate through each name and then download each file sequency. The name of the file i get from a torrent file which i decode.  </p>
  
  <pre><code>public class DownloadGameFile
  {
      private DownloadTorrentFile DLTorrent;
  
      //List of file that already exist
      private List&lt;string&gt; ExistFile = new List&lt;string&gt;();
      DirectoryInfo fileInfo;
      private volatile bool _completed;
  
      private string savePath = @"C:\Program Files (x86)\program\Client\package\downloads\";
  
      public DownloadGameFile()
      {
          DLTorrent = new DownloadTorrentFile();
          fileInfo = new DirectoryInfo(savePath);
      }
      public bool StartDownload(int torrentId)
      {
          try
          {
              DLTorrent.DecodeTorrent(torrentId);
  
  
              //File info from a Directory
              FileInfo[] files = fileInfo.GetFiles();
  
              foreach (FileInfo i in files)
              {
                  Console.WriteLine("Files exit ");
                  if (DLTorrent.GameInfomation[i.Name] != i.Length)
                  {
                      i.Delete();
                  }
                  else
                  {
                      Console.WriteLine("add files ");
                      ExistFile.Add(i.Name);
                  }
              }
              //Make a list which file not downloaded yet
              var res = DLTorrent.GameInfomation.Keys.Except(ExistFile);
  
              foreach (var x in res)
              {
                  Console.WriteLine(x);
              }
  
              foreach (var x in res)
              {
                  DownloadProtocol("http://cdn.path.com/rental/" + torrentId + "/" + x, savePath + x);
              }
  
              return true;
  
          }
          catch
          {
              return false;
          }
      }
  
      public void DownloadProtocol(string address, string location)
      {
          WebClient client = new WebClient();
          Uri Uri = new Uri(address);
  
          client.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
  
          client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgress);
          client.DownloadFileAsync(Uri, location);
  
  
      }
  
  
      private void DownloadProgress(object sender, DownloadProgressChangedEventArgs e)
      {
          // Displays the operation identifier, and the transfer progress.
          Console.WriteLine("{0}    downloaded {1} of {2} bytes. {3} % complete...",
              (string)e.UserState,
              e.BytesReceived,
              e.TotalBytesToReceive,
              e.ProgressPercentage);
      }
  
      private void Completed(object sender, AsyncCompletedEventArgs e)
      {
          if (e.Cancelled == true)
          {
              Console.WriteLine("Download has been canceled.");
          }
          else
          {
  
              Console.WriteLine("Download completed!");
          }
      }
  </code></pre>
  
  <p>This code work fine in an Console app with a current thread blocker. But when I use the same code in an WPF app It doesn't. I'm using a button to execute the StartDownload() function, but when I do that it start downloading all the files at the same time. Example the first file get 3% done and then it switch to another file and so on. I really don't know why this isn't working.</p>