简体   繁体   中英

Open a new form on the main thread

How can I open a new form from the main thread in 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:

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());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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