title: Carry on with UI while waiting for WebClient
body: <p>I'm trying to get a progress bar to show the program is loading (using ProgressStyle.Marquee) while a WebClient object reads from a site. The problem is that upon clicking the button that begins the call, the entire UI freezes up. I've tried putting it in a BackgroundWorker doWork but that causes it to never get any data at all. I've also tried delegates and various Thread options but all seem to have the same issue.</p>
  
  <p>I'm fairly new to C# and have mostly been trying results I've found on the web but none seem to solve my problem.</p>
  
  <p>What I have:</p>
  
  <pre><code>private void getInfoButton_Click(object sender, EventArgs e)
  {
      progressBar1.Style = ProgressBarStyle.Marquee;
      getInfoButton.Enabled = false;
      StreamReader reader = new StreamReader(client.OpenRead("URI here"));
      while ((line = reader.ReadLine()) != null) {
          // Do stuff
      }
      progressBar1.Style = ProgressBarStyle.Continuous;
      getInfoButton.Enabled = true;
  }
  </code></pre>
  
  <p>My attempt with BackgroundWorker</p>
  
  <pre><code>private void getInfoButton_Click(object sender, EventArgs e)
  {
      progressBar1.Style = ProgressBarStyle.Marquee;
      getInfoButton.Enabled = false;
      BackgroundWorker bw = new BackgroundWorker();
      bw.DoWord += new DoWorkEventHandler(bw_DoWork);
      bw.RunWorkerAsync("URI here");
      while (bw.IsBusy)
          ;
      progressBar1.Style = ProgressBarStyle.Continuous;
      getInfoButton.Enabled = true;
  }
  
  private void bw_DoWork(object sender, DoWorkEventArgs e)
  {
      WebClient client = new WebClient();
      StreamReader reader = new StreamReader(e.Argument);
      while ((line = reader.ReadLine()) != null) {
          // Do stuff
      }        
  }
  </code></pre>