title: C# download file with progress indicator and then install file
body: <p>I'm trying to write a C# Windows application which downloads a file (waits for that download to complete - providing some indicator of progress) and then executes the downloaded file. I think I either need:</p>
  
  <ul>
  <li>A synchronous download that has a progress indicator AND doesn't make my application unresponsive</li>
  <li>An asynchronous download that waits for the download to complete and then attempts to execute it.</li>
  </ul>
  
  <p>The former (synchronous download) seems to do what I want but I can't find a way to indicate progress any way and it appears to make my program non-responsive (like it's hung) - which may cause users to close the application instead of waiting for it to finish downloading.</p>
  
  <p>The later (asynchronous download) I've been able to accomplish with a progress indicator etc but what happens is that the download kicks off and my application immediately tries to install it before its finished downloading which of course fails.</p>
  
  <p>So whats the best way to accomplish this? Below is the code I presently have for the async download with progress bar. </p>
  
  <p><strong>Edit 4/10/2012</strong> </p>
  
  <p>The old code was getting to cumbersome to work around so here is what I presently have. It works except that it doesn't update the progress bar. </p>
  
  <pre><code>    private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
      {
  
              List&lt;App&gt; FilesToDownload = e.Argument as List&lt;App&gt;;
  
              foreach (App Program in FilesToDownload) // Loop through List with foreach
              {
                  //string Text = ;
                  UpdateRichTextBox("Downloading " + Program.Filename + "... Please wait");
  
                  string Source = Program.DownloadLocation + "/" +  Program.Filename;
                  string Destination = System.AppDomain.CurrentDomain.BaseDirectory + Program.Filename;
  
                  WebClient webClient = new WebClient();
                  webClient.DownloadFile(Source, @Destination);
              }
  
              UpdateRichTextBox("File download complete");
  
              foreach (App Program in FilesToDownload) // Loop through List with foreach
              {
                  string Filename = Program.Filename;
                  string Arguments = Program.InstallParameters;
  
                  if (Filename != "advisorinstaller.exe")
                  {
                      Process p = new Process();
                      p.StartInfo.FileName = System.AppDomain.CurrentDomain.BaseDirectory + Filename;
                      p.StartInfo.Arguments = Arguments;
                      p.Start();
                      p.WaitForExit();
                  }
                  else
                  {
                      MessageBox.Show("About to install Belarc Advisor. This may take 10-15 minutes to run - do not shutdown this program. Click OK when ready to proceed.");
                  }
              }
      }
  
      private void backgroundWorker1_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
      {
          progressBar1.Value = e.ProgressPercentage;
      }
  
      private void backgroundWorker1_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
      {
          UpdateRichTextBox("Install Complete");
  
      }
  </code></pre>
  
  <p><strong>Edit 4/11/2012</strong></p>
  
  <p>So I have edited my backgroundworker do work as follows:</p>
  
  <pre><code>        private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
          {
  
              List&lt;App&gt; FilesToDownload = e.Argument as List&lt;App&gt;;
  
              int counter = 0;
              int percent = 0;
  
              foreach (App Program in FilesToDownload) // Loop through List with foreach
              {
                  percent = ((counter / FilesToDownload.Count) * 100);
                  backgroundWorker1.ReportProgress(percent, "");
  
                  UpdateRichTextBox("Downloading " + Program.Filename + "... Please wait");
  
                  string Source = Program.DownloadLocation + "/" +  Program.Filename;
                  string Destination = System.AppDomain.CurrentDomain.BaseDirectory + Program.Filename;
  
                  WebClient webClient = new WebClient();
                  webClient.DownloadFile(Source, @Destination);
  
  
                  counter++;
  
              }
  
              //backgroundWorker1.ReportProgress(100, "Complete!");
      }
  </code></pre>
  
  <p>If I uncomment the last line the progress bar proceeds to 100%. But its not progressing using: </p>
  
  <pre><code>percent = ((counter / FilesToDownload.Count) * 100);
  backgroundWorker1.ReportProgress(percent, "");
  </code></pre>
  
  <p>Any ideas why?</p>