简体   繁体   English

在主线程上打开一个新表单

[英]Open a new form on the main thread

How can I open a new form from the main thread in C#? 如何在C#中从主线程打开新表单?

At this moment I open them by using this: 此时,我使用以下命令打开它们:

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

But this creates a new thread... My main form is my login form.. And I want that to close and then open my second form. 但这会创建一个新线程...我的主要表单是我的登录表单。.我希望关闭它,然后打开第二个表单。

Go to your program.cs file and alter it so that you show your login form and then, after it has been closed, determine if you should open up another form: 转到program.cs文件并对其进行更改,以便显示您的登录表单,然后在关闭登录表单后,确定是否应打开另一个表单:

It'll likely look something more or less like this: 看起来或多或少是这样的:

LoginForm loginform = new LoginForm();
Application.Run(loginform);

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

只是用这个

Application.Run(new OppenMainForm());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM