title: What's wrong with my cross-thread call in Windows Forms?
body: <p>I encounter a problem with a Windows Forms application.</p>
  
  <p>A form must be displayed from another thread. So in the form class, I have the following code:</p>
  
  <pre><code>private delegate void DisplayDialogCallback();
  
  public void DisplayDialog()
  {
      if (this.InvokeRequired)
      {
          this.Invoke(new DisplayDialogCallback(DisplayDialog));
      }
      else
      {
          this.ShowDialog();
      }
  }
  </code></pre>
  
  <p>Now, every time I run this, an <code>InvalidOperationException</code> is thrown on the line <code>this.ShowDialog();</code>:</p>
  
  <p><em>"Cross-thread operation not valid: Control 'SampleForm' accessed from a thread other than the thread it was created on."</em></p>
  
  <p>What's wrong with this piece of code? Isn't it a valid way to make cross-thread calls? Is there something special with <code>ShowDialog()</code>?</p>