title: Working with Backgroundworker
body: <p>I want to load a text file in <code>WPF-RichTextbox</code> using <code>BackgroundWorker</code></p>
  
  <p>Here is my code :</p>
  
  <pre><code>    private delegate void update();
  
      private void reader()
      {
          StreamReader str = new StreamReader("C:\\test.txt");
          while (!str.EndOfStream)
          {
              richTextBox1.AppendText(str.ReadToEnd());
          }
  
      }
  
      private void Window_Loaded(object sender, RoutedEventArgs e)
      {
          progress wnd = new progress();
          BackgroundWorker bg = new BackgroundWorker();
          bg.DoWork += delegate(object s, DoWorkEventArgs args)
          {
  
              update up = new update(reader);
              richTextBox1.Dispatcher.Invoke(up);
  
          };
  
          bg.RunWorkerCompleted += delegate(object s, RunWorkerCompletedEventArgs args)
          {
  
              wnd.Close();
          };
  
          wnd.Show();
          bg.RunWorkerAsync();
  
  
  
      }
  </code></pre>
  
  <p>and for progress.xaml :</p>
  
  <pre><code>   &lt;Window x:Class="WpfApplication3.progress"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="progress" Height="192" Width="452" ResizeMode="NoResize" ShowInTaskbar="False" Topmost="True" WindowStartupLocation="CenterScreen" WindowStyle="None"&gt;
  &lt;Grid&gt;
      &lt;Label Content="loading ...." Height="28" HorizontalAlignment="Left" Margin="46,32,0,0" Name="label1" VerticalAlignment="Top" /&gt;
      &lt;ProgressBar Height="29" HorizontalAlignment="Left" Margin="46,78,0,0" Name="progressBar1" VerticalAlignment="Top" Width="325" IsIndeterminate="True" /&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>
  
  <p>When I set <code>wnd.showdialog()</code> , it shows the indeterminate status of progress bar but the application does not load any text .</p>
  
  <p>When I set <code>wnd.show()</code> , it loads the text but when loading , the progress bar freezes and 
  does not show indeterminate status.</p>
  
  <p>Thanks for any help.</p>
  
  <p>EDIT :</p>
  
  <p>due to Servey and SLaks answers , i have updated the code but it has the same problem (it loads the text but when loading , the progress bar freezes and does not show indeterminate status.)</p>
  
  <p>what is the problem? here is my updated code :</p>
  
  <pre><code>           StringBuilder sb = new StringBuilder();
  
  
  
      private void reader()
      {
          StreamReader str = new StreamReader("C:\\test.txt");
          while (!str.EndOfStream)
          {
  
              sb.Append(str.ReadToEnd());
  
          }
  
      }
  
      private void Window_Loaded(object sender, RoutedEventArgs e)
      {
          progress wnd = new progress();
          BackgroundWorker bg = new BackgroundWorker();
          bg.DoWork += delegate(object s, DoWorkEventArgs args)
          {
  
  
  
              reader();
  
          };
  
          bg.RunWorkerCompleted += delegate(object s, RunWorkerCompletedEventArgs args)
          {
              richTextBox1.AppendText(sb.ToString());
              wnd.Close();
  
          };
  
          wnd.Show();
          bg.RunWorkerAsync();
  
  
      }
  </code></pre>