Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When new a Form, will I get a process or thread?

Tags:

c#

In C#, I have two Forms: mainForm and form1.

class Form1; //...
class mainForm {
    //...
    void f() {
        Form1 form1 = new Form1();
        ...
    }
}

I want to wait for the form1 to exit and continue the following work in the mainForm. But I don't know the form1 is implemented as a process or a thread and how to get its ID.

Thanks.

like image 596
Yantao Xie Avatar asked Dec 22 '22 09:12

Yantao Xie


2 Answers

It is neither. It runs on the same UI thread as your other form(s) unless you go out of your way to do something clever, with the message pump handling messages to all of them.

What is it that you want to do? Normally keeping the reference to the second form instance is sufficient for sending messages, etc.

To wait for the second form to finish, use ShowDialog(), or if you are in a form, ShowDialog(this).

like image 109
Marc Gravell Avatar answered Jan 02 '23 14:01

Marc Gravell


form1 runs on the same Main Thread and you can simply run form1 as modal dialog like

Form1 form1 = new Form1();
form1.ShowDialog(this); //mainForm waits till form1 finishs its work
//extra work in mainForm 
like image 33
Ahmed Avatar answered Jan 02 '23 12:01

Ahmed