Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > c# form multiThread
 

c# form multiThread

private void button1_Click(object sender, EventArgs e)
{
new Thread(new ThreadStart(newForm)).Start();

}
private void newForm()
{
Form2 f = new Form2();
f.Show();

}
上面调用了f.Show()后窗体一闪而过,谁能解释?
如果不是用新的线程来new和show这个form的话是可以的(即在原来的UI线程?.

如果在newForm中调用的不是Show(),而是ShowDialog()的话,Form2也能正常显示.
如果在newForm中f.Show()后再调用Application.Run();的话,也可以显示正?谁能来解释下.

完全不明白到底怎么回事!!!
Form2继承至Form.
本来的目的是想引发一个A线程无法访问B线程拥有的UI控件的问?/div>
赵国  Tuesday, September 01, 2009 1:22 PM
Hi 赵国?

These are my ideas:
1. In a new thread, you called Form.Show(), but the form closes just when it shows.
Explanation: when you call Show method, the code would return immediately. So the thread would also stop immediately.
2. In the main UI thread, Form.Show() would show the form.
Explanation: the main UI thread is started by calling Application.Run(Form) which would run the message loop. In such a thread, the Form would be shows until you sends
a close message to it by calling the Close method.
3. In the new thread, From.ShowDialog() would also show the form as what we want.
This is because: ShowDialog() method would not return until we set the DialogResult or close the Form.
4.In the new thread, if we call Application.Run, the form would also show correctly.
Explanation: This is similar to case 2. The thread runs the message loop.

You can get more infomation from:
Application.Run: http://msdn.microsoft.com/en-us/library/system.windows.forms.application.run.aspx?br/>
Actually, when you call the show method, it shows a modeless dialog; when you call ShowDialog, it shows a modal dialog. You can get differences between these two dialogs form:
http://msdn.microsoft.com/en-us/library/ms644996(VS.85).aspx
http://developer.kde.org/documentation/books/kde-2.0-development/ch08lev1sec3.html

These are some similar threads:
http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/090f4438-b87f-459b-89f1-db10b76c51c4
http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvcs/thread/de88028b-8849-4e22-a873-5ae350531672

我们希望您以后用英文提问题,这样方便大家交流,您也能获取更多的支持?br/>您也可以到中文论坛发帖: http://social.microsoft.com/Forums/zh-CN/visualcshartzhchs/threads?br/>
Best regards,
Aland Li

Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Thursday, September 03, 2009 10:46 AM
Okay, its Chinese and Google Translates it to this :

The above call f.Show () after the flash off the form, who can explain the following?
If it is not a new thread for new and show the form, then it is possible (that is, in the original UI thread).

If newForm not call Show (), but the ShowDialog (), then, Form2 can be displayed properly.
If newForm the f.Show () before calling Application.Run (); words, you can also display a normal, who can next be explained.

Totally do not understand how going on in the end!!!
Form2 inherit from Form.
The original purpose is to trigger a thread A thread B can not access the UI controls have a problem

where as bold text makes me feel like, its a cross thread operation problem.

赵国?

See if you understand this

http://vb2010.wordpress.com/2009/08/29/threadind-and-cross-thread-operations/




Thanks

?My Blog ?My Flickr ?My Facebook ?/div>
Omie  Tuesday, September 01, 2009 2:33 PM
Hi 赵国?

These are my ideas:
1. In a new thread, you called Form.Show(), but the form closes just when it shows.
Explanation: when you call Show method, the code would return immediately. So the thread would also stop immediately.
2. In the main UI thread, Form.Show() would show the form.
Explanation: the main UI thread is started by calling Application.Run(Form) which would run the message loop. In such a thread, the Form would be shows until you sends
a close message to it by calling the Close method.
3. In the new thread, From.ShowDialog() would also show the form as what we want.
This is because: ShowDialog() method would not return until we set the DialogResult or close the Form.
4.In the new thread, if we call Application.Run, the form would also show correctly.
Explanation: This is similar to case 2. The thread runs the message loop.

You can get more infomation from:
Application.Run: http://msdn.microsoft.com/en-us/library/system.windows.forms.application.run.aspx?br/>
Actually, when you call the show method, it shows a modeless dialog; when you call ShowDialog, it shows a modal dialog. You can get differences between these two dialogs form:
http://msdn.microsoft.com/en-us/library/ms644996(VS.85).aspx
http://developer.kde.org/documentation/books/kde-2.0-development/ch08lev1sec3.html

These are some similar threads:
http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/090f4438-b87f-459b-89f1-db10b76c51c4
http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvcs/thread/de88028b-8849-4e22-a873-5ae350531672

我们希望您以后用英文提问题,这样方便大家交流,您也能获取更多的支持?br/>您也可以到中文论坛发帖: http://social.microsoft.com/Forums/zh-CN/visualcshartzhchs/threads?br/>
Best regards,
Aland Li

Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Thursday, September 03, 2009 10:46 AM

You can use google to search for other answers

Custom Search

More Threads

• Status of record
• Binding a DataGridViewColumn to a method as opposed to a property
• I need to display a 2-level heirarchical data in an expandable list, can anyone help me find a control 4 doing that?
• How to get date to a textBox and save him in database???
• Scrollable Datagrids & Freeze Panes
• How to use DataGridViewComboBoxColumn in vb.net 2005
• DataGridView CurrentCell jumps to wrong row
• edit dataset with designer table property question
• how can I retrieve a newly added autoincremented key index?
• Issue with DataGridView / CellMouseClick