所以基本上我已经为我的程序制作了一个登录系统,当用户登录它时,它会打开 Form1。但我需要 Form1 成为 STA 线程。我在 Form1 中收到此错误:
{“在进行 OLE 调用之前,必须将当前线程设置为单线程单元 (STA) 模式。确保您的 Main 函数上标记了 STAThreadAttribute。仅当调试器附加到进程时才会引发此异常。”} 在这段代码
SaveFileDialog FSave = new SaveFileDialog()
{
Filter = "Executable Files|*.exe",
InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
};
if (FSave.ShowDialog() == DialogResult.OK)//im getting the error here
{
// CodeDom compiler code
}
这是我的 Program.cs
using System;
using System.Windows.Forms;
namespace hwid_login_system
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Login());
}
}
}
这就是我在表单登录中打开表单 Form1 的方式
private void complete()
{
if (loggedin && hwid)
{
MessageBox.Show("Logged in successfully!");
System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(ThreadProc));
t.Start();
this.Close();
}
else
MessageBox.Show("Something else went wrong..", "Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
public static void ThreadProc()
{
Application.Run(new Form1());
}