title: How to Make Sure UI is Responsive Using BackgroundWorker
body: <p>Is BackgroundWorker in c# Thread Safe?</p>
  
  <p>The reason I ask this is because I get a </p>
  
  <blockquote>
    <p>Controls created on one thread cannot
    be parented to a control on a
    different thread</p>
  </blockquote>
  
  <p>exception with it. This is my <code>DoWork</code> event code:</p>
  
  <pre><code>private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
  {
  
  
  
      var openFile = document.Open(MyFileName);
      e.Result = openFile;
  }
  </code></pre>
  
  <p>where <code>document</code> is an UI control that is initialized when the parent form is created. During <code>Open</code> method various properties in <code>document</code> will be filled.</p>
  
  <p>I tried to change the code to invoke, yet the same problem persists. i.e,</p>
  
  <pre><code>document.GetType().GetMethod("Open)".Invoke(document, new object[]{MyFileName})
  </code></pre>
  
  <p>will yield the same error as the above.</p>
  
  <p>Any idea how to manipulate the <code>document</code> control? In other words, how to make the above code work?</p>
  
  <p><strong>Edit: It was suggested that I use <code>Control.Invoke</code>, but it still didn't work ( both of the threads hanged). This is the code I tried:</strong></p>
  
  <pre><code>private delegate bool OpenFile(string filePath);
  private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
  {
  
  
  
      OpenFile oF = new OpenFile(document.Open);
      var openFile = Invoke(oF, MyFileName);  // it doesn't really matter whether I use BeginInvoke or Invoke, or other Control.Invoke, the end result is the same. Both the main thread hosting the document and the thread that launches the UI hanged.
  
      e.Result = openFile;
  }
  </code></pre>