C# ThreadStateException错误:ShowDialog()

C# ThreadStateException错误:ShowDialog(),c#,wpf,visual-studio-2010,c#-4.0,C#,Wpf,Visual Studio 2010,C# 4.0,我编写了一个client.cs,当服务器提示时,它会在一个新线程中打开一个电子表格: client.cs if (!isSpreadsheetOpen) { isSpreadsheetOpen= true; GUI = new Form1(ClientStringSocket); c

我编写了一个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.

我尝试添加[StatThread]但没有效果。如何解决此错误?

该表单将出现在服务器上,而不是客户端上。在ASP应用程序中创建新的winform是错误的。不要这样做。

该表单将出现在服务器上,而不是客户端上。在ASP应用程序中创建新的winform是错误的。不要这样做。

该表单将出现在服务器上,而不是客户端上。在ASP应用程序中创建新的winform是错误的。不要这样做。

该表单将出现在服务器上,而不是客户端上。在ASP应用程序中创建新的winform是错误的。不要这样做。

我可以通过在启动线程之前添加以下两行来解决此问题:

                            thread.IsBackground = true;
                            thread.SetApartmentState(ApartmentState.STA);
                            thread.Start();

通过在启动线程之前添加以下两行,我可以解决此问题:

                            thread.IsBackground = true;
                            thread.SetApartmentState(ApartmentState.STA);
                            thread.Start();

通过在启动线程之前添加以下两行,我可以解决此问题:

                            thread.IsBackground = true;
                            thread.SetApartmentState(ApartmentState.STA);
                            thread.Start();

通过在启动线程之前添加以下两行,我可以解决此问题:

                            thread.IsBackground = true;
                            thread.SetApartmentState(ApartmentState.STA);
                            thread.Start();


您是否尝试在Program.cs的Main方法上添加[StatThread]属性?
cient.cs
是WinForms应用程序还是ASP.NET WebForm?查看此网页的右侧,您会看到相关问题。我相信你的问题已经被回答了很多次。我尝试在Main方法上添加[STAThread]属性,但没有成功。client.cs是WPF。可能与的重复。您是否尝试在Program.cs的Main方法上添加[StatThread]属性?
cient.cs
是WinForms应用程序还是ASP.NET WebForm?查看此网页的右侧,您会看到相关问题。我相信你的问题已经被回答了很多次。我尝试在Main方法上添加[STAThread]属性,但没有成功。client.cs是WPF。可能与的重复。您是否尝试在Program.cs的Main方法上添加[StatThread]属性?
cient.cs
是WinForms应用程序还是ASP.NET WebForm?查看此网页的右侧,您会看到相关问题。我相信你的问题已经被回答了很多次。我尝试在Main方法上添加[STAThread]属性,但没有成功。client.cs是WPF。可能与的重复。您是否尝试在Program.cs的Main方法上添加[StatThread]属性?
cient.cs
是WinForms应用程序还是ASP.NET WebForm?查看此网页的右侧,您会看到相关问题。我相信你的问题已经被回答了很多次。我尝试在Main方法上添加[STAThread]属性,但没有成功。client.cs是WPF。可能与您所说的ASP应用程序重复?这是一个WPF应用程序。@jamie\u您将该问题标记为ASP问题,而不是WPF问题。除此之外,Winform构造中还缺少一些内容,例如
Appliation.Run
,以及使用表单而不是windows。您没有使用任何WPF构造。您所说的ASP应用程序是什么意思?这是一个WPF应用程序。@jamie\u您将该问题标记为ASP问题,而不是WPF问题。除此之外,Winform构造中还缺少一些内容,例如
Appliation.Run
,以及使用表单而不是windows。您没有使用任何WPF构造。您所说的ASP应用程序是什么意思?这是一个WPF应用程序。@jamie\u您将该问题标记为ASP问题,而不是WPF问题。除此之外,Winform构造中还缺少一些内容,例如
Appliation.Run
,以及使用表单而不是windows。您没有使用任何WPF构造。您所说的ASP应用程序是什么意思?这是一个WPF应用程序。@jamie\u您将该问题标记为ASP问题,而不是WPF问题。除此之外,Winform构造中还缺少一些内容,例如
Appliation.Run
,以及使用表单而不是windows。您没有使用任何WPF构造。