title: Backgroundworker processing in own class
body: <p>Well, I have the following problem and I hope that you can help me:</p>
  
  <p>I would like to create a WPF application with a background worker for updating richtextboxes and other UI elements. This background worker should process some data, e.g. handle the content of a folder, do some parsing and much more. Since I would like to move as much code as possible outside the Main class, I created a class called <code>MyProcess.cs</code> as you can see below (in fact, this class does not make much sense so far, it will be filled with much more processing elements if this problem has been solved). The general functionality should be:</p>
  
  <ol>
  <li>MainWindow: An array of strings will be created (named <code>this.folderContent</code>)</li>
  <li>MainWindow: A background worker is started taking this array as argument</li>
  <li>MainWindow: The <code>DoWork()</code> method will be called (I know, this one now runs in a new thread)</li>
  <li>MyProcess: Generates a (so far unformatted) Paragraph based on the given string array</li>
  <li>MainWindow: If the background worker is finished, the <code>RunWorkerCompleted()</code> method is called (running in UI thread) which should update a WPF RichTextBox via the method's return argument</li>
  </ol>
  
  <p>This last step causes an InvalidOperationsException with the note, that "The calling thread cannot access this object because a different thread owns it." I read a bit about the background worker class and its functionality. So I think that it has something to do with the <code>this.formatedFilenames.Inlines.Add(new Run(...))</code> call in the <code>Execute()</code> method of <code>MyProcess</code>. If I replace the Paragraph attribute by a list of strings or something similar (without additional <code>new()</code> calls) I can access this member without any problems by a get method. All examples related to the background worker I have found return only basic types or simple classes.</p>
  
  <p><strong>MainWindow.xaml.cs</strong></p>
  
  <pre><code>    public MainWindow()
      {
          InitializeComponent();
          this.process = new MyProcess();
          this.worker = new BackgroundWorker();
          this.worker.DoWork += worker_DoWork;
          this.worker.RunWorkerCompleted += worker_RunWorkerCompleted;
      }
  
      private void worker_DoWork(object sender, DoWorkEventArgs e)
      {
          this.process.Execute((string[])e.Argument);
          e.Result = this.process.Paragraph();
      }
  
      private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
      {
          this.rtbFolderContent.Document.Blocks.Clear();
          // the next line causes InvalidOperationsException:
          // The calling thread cannot access this object because a different thread owns it.
          this.rtbFolderContent.Document.Blocks.Add((Paragraph)e.Result);
      }
  
      ...
      // folderContent of type string[]
      this.worker.RunWorkerAsync(this.folderContent);
      ...
  </code></pre>
  
  <p><strong>Edit:</strong> Since this has been asked: the <em>RunWorkerAsync</em> is called e.g. on a button click event or after a folder has been selected via a dialog, so within the UI thread.</p>
  
  <p><strong>MyProcess.cs</strong></p>
  
  <pre><code>class MyProcess
  {
      Paragraph formatedFilenames;
  
      public MyProcess ()
      {
          this.formatedFilenames = new Paragraph();
      }
  
      public void Execute(string[] folderContent)
      {
          this.formatedFilenames = new Paragraph();
          if (folderContent.Length &gt; 0)
          {
              for (int f = 0; f &lt; folderContent.Length; ++f)
              {
                  this.formatedFilenames.Inlines.Add(new Run(folderContent[f] + Environment.NewLine));
                  // some dummy waiting time
                  Thread.Sleep(500);
              }
          }
      }
  
      public Paragraph Paragraph()
      {
          return this.formatedFilenames;
      }
  }
  </code></pre>