title: C# Show Dialog threading
body: <p>I have a <code>Dialog Box</code> (Importer) which i use for choosing a file I want to import into an app. This <code>Dialog Box</code> (Importer) also has another Dialog Box (File) which is an <code>OpenFileDialog</code>.</p>
  
  <p>The code runs something like this</p>
  
  <pre><code>//Main File
  
  if (Importer.ShowDialog == DialogResult.Ok){
  // Start Import
  }
  
  //Importer File
  
  OnDoubleClick of TextBox
  
  if(File.ShowDialog == DialogResult.Ok){
  // Find File
  }
  </code></pre>
  
  <p>However on the Second <code>ShowDialog</code> I always get the following error:</p>
  
  <pre><code>An unhandled exception of type 'System.Threading.ThreadStateException' occurred in System.Windows.Forms.dll
  
  Additional information: Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it. This exception is only raised if a debugger is attached to the process.
  </code></pre>
  
  <p>Is this a threading issue, and how should i deal with it.</p>
  
  <p>I look forward to hearing from you.
  Regards
  James</p>
  
  <p>--Update some more code to help This is all inside the first Form.ShowDialog()</p>
  
  <pre><code>private void fileNametxt_DoubleClick(object sender, EventArgs e)
      {
  
  
          myth = new Thread(new System.Threading.ThreadStart(ChooseFile));
          myth.ApartmentState = ApartmentState.STA;
          myth.Start();
  
          while(myth.ThreadState != ThreadState.Aborted || myth.ThreadState != ThreadState.Stopped)
          {
                 fileNametxt.Text = FileName;
          }
  
          fileNametxt.Text = FileName;
  
  
      }
  
  
      private void ChooseFile()
      {
          openFileDialog.ShowDialog();
  
          if (openFileDialog.FileName != "")
          {
              FileName = openFileDialog.FileName.Trim();
  
          }
  
          myth.Abort();
  
      }
  </code></pre>
  
  <p>How do I stop the thread and update the text on screen. The thread only seems to be able to talk to varibles not UI controls.</p>