在主线程上打开一个新窗体

本文关键字:一个 新窗体 窗体 线程 | 更新日期: 2024-10-20 15:44:51

如何从C#中的主线程打开一个新表单?

此时此刻,我用这个打开它们:

System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(oppenMainForm));
t.SetApartmentState(ApartmentState.STA);
t.Start();

但这创造了一个新的线索。。。我的主表单是我的登录表单。我想关闭它,然后打开我的第二个表单。

在主线程上打开一个新窗体

转到您的program.cs文件并对其进行更改,以便显示您的登录表单,然后在它关闭后,确定是否应该打开另一个表单:

它看起来可能或多或少是这样的:

LoginForm loginform = new LoginForm();
Application.Run(loginform);
if (loginform.DialogResult == DialogResult.Yes)
    Application.Run(new MainForm());
//TODO handle error cases

只需使用此

Application.Run(new OppenMainForm());