title: ThreadStateException Error: ShowDialog()
body: <p>I wrote a client.cs that opens a spreadsheet in a new thread when server tells so:</p>
  
  <p><strong>client.cs</strong></p>
  
  <pre><code>if (!isSpreadsheetOpen)
                          {
                              isSpreadsheetOpen= true;
                              GUI = new Form1(ClientStringSocket);
                              connectedToServer = true;
                              var thread = new System.Threading.Thread(delegate()
                              {
                                  Application.EnableVisualStyles();
                                  Application.SetCompatibleTextRenderingDefault(false);
                                  DemoApplicationContext appContext = DemoApplicationContext.getAppContext();
                                  appContext.RunForm(GUI);
                                  Application.Run(appContext);
                              });
                              thread.Start();
                          }
  </code></pre>
  
  <p>When the user clicks save button, the dialog that looks like a picture below will pop up. </p>
  
  <pre><code>if (saveFileDialog1.ShowDialog() == DialogResult.OK)
              {
                  spreadsheet.Save(saveFileDialog1.FileName);
              }
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/DZe68.png" alt="enter image description here"></p>
  
  <p>However, I am getting this error.</p>
  
  <pre><code>ThreadStateExcepton: 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>I have tried adding [STAThread] to no avail. How do I resolve this error?</p>