当服务器告诉我时,我编写了一个client.cs,它在一个新线程中打开一个电子表格:
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();
}当用户单击“保存”按钮时,下面的对话框将弹出。
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
spreadsheet.Save(saveFileDialog1.FileName);
}

但是,我得到了这个错误。
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.我尝试过添加STAThread,但没有效果。如何解决此错误?
发布于 2014-04-11 03:32:19
我能够通过在启动线程之前添加这两行来解决这个问题:
thread.IsBackground = true;
thread.SetApartmentState(ApartmentState.STA);
thread.Start();发布于 2014-04-10 15:41:14
该表单将弹出在服务器上,而不是在客户机上。在ASP应用程序中创建新的winform是错误的。不要这样做。
https://stackoverflow.com/questions/22992500
复制相似问题