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