title: C# WinForms: How to set Main function STAThreadAttribute
body: <p>I get the following exception when calling <code>saveFileDialog.ShowDialog()</code> in a background thread:</p>
  
  <blockquote>
    <p>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.</p>
  </blockquote>
  
  <p>According to <a href="http://www.charlespetzold.com/pwcs/ThreadStateException.html">this</a>:</p>
  
  <blockquote>
    <p>To fix the problem, insert the
    statement:</p>
  </blockquote>
  
  <pre><code>Threading.Thread.CurrentThread.ApartmentState = Threading.ApartmentState.STA;
  </code></pre>
  
  <blockquote>
    <p>in Main right before the
    Application.Run statement.</p>
  </blockquote>
  
  <p>But the Application.Run statement is in Program.cs which seems to be generated code so any changes might be unexpectedly lost. Also, I could not find a way to set current thread to STA in the project or main form properties but maybe I am looking in the wrong place.
  What is the proper way to call <code>saveFileDialog.ShowDialog()</code> in a background thread?</p>