title: Cross-thread exception when setting WinForms.Form owner - how to do it right?
body: <p>I have a main UI thread which runs the application and creates the main window form (let's call it <code>W</code>). I have also a secondary thread that I spin up and which creates a dialog box (let's call it <code>B</code>).</p>
  
  <p>I want to set the owner of the dialog <code>B</code> to be the main window <code>W</code>. The setting of <code>B</code>s owner happens on the thread that created <code>B</code>. Basically:</p>
  
  <pre><code>b.Owner = w;
  </code></pre>
  
  <p>but this throws a cross-thread exception telling me that I am tryng to access the <code>W</code> object from the wrong thread.</p>
  
  <p>So I tried to execute the code on the main UI thread, by using a <code>Control.Invoke</code> on <code>W</code>. But then, I get the same error telling me that I am trying to access <code>B</code> from the wrong thread:</p>
  
  <pre><code>System.InvalidOperationException was unhandled by user code
    Message=Cross-thread operation not valid: Control 'B' accessed from a
    thread other than the thread it was created on.
    Source=System.Windows.Forms
  </code></pre>
  
  <p>How am I supposed to do it right?</p>