views:

416

answers:

6

Hello everyone,

In the button click even handler of Form1, I want to create Form2. Here is my code,

even handler of button1 of Form1

// button1 belongs to Form1
private void button1_Click(object sender, EventArgs e)
{
    Form2 form2 = new Form2("www.google.com");
    form2.ShowDialog();
}

Form2 contructor

public Form2 (string str)
{
    InitializeComponent();
    address = str;
    button2.Text = str; // button2 belongs to Form2
}

public Form2()
{
    InitializeComponent();
}

My question and concern is, button 1 event handler is executed by Form1's UI thread, and if I create new Form2 inside the Form1 UI thread, and use the UI thread to set Form2's UI element (button2.Text), is that legal? My understanding is each Form has its owner UI thread and UI elements (e.g. button) should be processed only by the forms individual owner thread? If my code is wrong, appreciated if anyone could show me what is the elegant way to creat another Form inside event handler of the current form and pass parameters.

I am using VSTS 2008 + C# + .Net 2.0.

thanks in advance, George

+1  A: 

It's legal and should work just fine.

JP Alioto
I want to know no matter how many Forms I create, all of them will share the same single UI thread?
George2
+5  A: 

Your code is perfectly fine. Both forms will live in the same thread and can access each other without worrying about threading. No new threads are spawned unless you specifically say so, in this case.

As a side note, I would want to alter your constructor of Form2 a little bit. I assume that Form2 also has the default constructor with no parameters, and that that constructor also calls InitializeComponent? If so, there is no need to duplicate that call in your constructor; if you set it up like this, it will first run the default constructor, and then you add your custom stuff:

public Form2 (string str) : this()
{
    address = str;
    button2.Text = str; // button2 belongs to Form2
}
Fredrik Mörk
Thanks Fredrik, I have posted my another constructor of Form2, and it has only one line of code to call InitializeComponent. Are there any functional bug of my code (if I just keep my code as it is now showed in my original post) compared with yours?
George2
No, there is no bug or antyhing technically wrong with the code. It's only a matter or avoiding code in different places doing the same thing; removing redundancy, nothing else.
Fredrik Mörk
Does it mean no matter how many Forms I create, in a Windows Forms application, all of the Forms will share the same single UI thread?
George2
Yes, that is correct (as Tormod Fjeldskår indicates below). The number of forms is not a factor, everything happens on the same thread.
Fredrik Mörk
+2  A: 

In Winforms, all UI elements within an application share the same UI thread. So rest assure, there is nothing wrong with your code in that sense.

Tormod Fjeldskår
You mean no matter how many Forms I create, all of them will share the same UI thread?
George2
That's right. All forms belonging to the same application will share the same UI thread.
Tormod Fjeldskår
+1  A: 

And the second form will use the same thread as form1

Marcom
Does it mean no matter how many Forms I create, in a Windows Forms application, all of the Forms will share the same single UI thread?
George2
yes.only way the new form will be on another thread is if you create the form using the other thread.
Marcom
+1  A: 

Your code is totally legal :-) Documentation only says that any UI control should be only accessed from the thread on which it was created.

chikak
Does it mean no matter how many Forms I create, in a Windows Forms application, all of the Forms will share the same single UI thread?
George2
as mliesen said "All forms will run on the same thread, (unless you start a new thread and created the form within this thread)."
chikak
+1  A: 

All forms will run on the same thread, (unless you start a new thread and created the form within this thread).

mliesen
Does it mean no matter how many Forms I create, in a Windows Forms application, all of the Forms will share the same single UI thread?
George2