0

I wrote a client.cs that opens a spreadsheet in a new thread when server tells so:

client.cs

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();
                        }

When the user clicks save button, the dialog that looks like a picture below will pop up.

if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                spreadsheet.Save(saveFileDialog1.FileName);
            }

enter image description here

However, I am getting this error.

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.

I have tried adding [STAThread] to no avail. How do I resolve this error?

YK1
  • 7,327
  • 1
  • 21
  • 28
jamie_y
  • 1,719
  • 2
  • 13
  • 20
  • did you try adding [STAThread] attribute on Main method in Program.cs? – Reza Apr 10 '14 at 15:43
  • `cient.cs` is WinForms application or ASP.NET WebForm? – YK1 Apr 10 '14 at 15:48
  • look at the right of this webpage, you see related questions. I am sure your question has already been answered numerous times. – thumbmunkeys Apr 10 '14 at 15:51
  • I have tried adding [STAThread] attribute on Main method, that did not work. client.cs is WPF. – jamie_y Apr 10 '14 at 15:53
  • 1
    possible duplicate of [How to run something in the STA thread?](http://stackoverflow.com/questions/2378016/how-to-run-something-in-the-sta-thread) – YK1 Apr 10 '14 at 16:00

2 Answers2

2

That form is going to be popping up on the server, not on the client. Creating new winform in an ASP application is just wrong. Don't do it.

Servy
  • 202,030
  • 26
  • 332
  • 449
  • What do you mean by ASP application? It's a WPF application. – jamie_y Apr 11 '14 at 15:49
  • @jamie_y **You** tagged the question as an ASP question, not a WPF question. On top of that, you're missing in Winform constructs, such as `Appliation.Run` and the use of forms rather than windows. You're not using *any* WPF constructs. – Servy Apr 11 '14 at 15:50
1

I was able to resolve this issue by adding these two lines before starting the thread:

                            thread.IsBackground = true;
                            thread.SetApartmentState(ApartmentState.STA);
                            thread.Start();
jamie_y
  • 1,719
  • 2
  • 13
  • 20