Windows Develop Bookmark and Share   
 index > Windows Forms General > Getting control parenting exception in form constructor
 

Getting control parenting exception in form constructor

I'm getting the "Controls created on one thread cannot be parented to a control on a different thread" exception in the field, though I've never seen it in development or test. The odd thing is that it's coming when my form's InitializeComponent() tries to add the first control to its Controls collection:

System.ArgumentException: Controls created on one thread cannot be parented to a control on a different thread.
at System.Windows.Forms.Control.ControlCollection.Add(Control value)
at System.Windows.Forms.Form.ControlCollection.Add(Control value)
at Mixzing.MixzingGUI.MainForm.InitializeComponent() in C:\Documents and Settings\peter\My Documents\Visual Studio 2005\Projects\MixzingGUI\MixzingGUI\MainForm.Designer.cs:line 1213
at Mixzing.MixzingGUI.MainForm..ctor(Manager mgr, Player player, Boolean standalone) in C:\Documents and Settings\peter\My Documents\Visual Studio 2005\Projects\MixzingGUI\MixzingGUI\MainForm.cs:line 89
at Mixzing.MixzingGUI.MixzingGUI.startGUI() in C:\Documents and Settings\peter\My Documents\Visual Studio 2005\Projects\MixzingGUI\MixzingGUI\MixzingGUI.cs:line 59


MixzingGUI is a static class whose startGUI() is newing up the MainForm instance, which simply does:


public MainForm(Manager mgr, Player player, bool standalone) {
this.mgr = mgr;
this.player = player;
this.standalone = standalone;
mgr.Form = this;
player.Listener = this;
InitializeComponent();
[...]


and InitializeComponent() (which is completely generated by the designer) is just doing:


this.Controls.Add(this.topBar);


So...I know that this exception may occur if I'm accessing a control from a thread other than its creator, but at this point how could I possibly be running on a thread other than the one that is currently creating the form? I mean, it's right there in the stack trace--I'm running in the same thread as the form's constructor, right? So what am I missing?

Thanks for any help!

pjeffe  Wednesday, October 08, 2008 10:39 PM
You'll need to consider a threading race, some other thread is using your form before it is completely constructed. Windows Forms creates window handles lazily. Normally, that happens in response to a Show() call, just before the Load event runs. The exception you get indicates that the handle already exists by the time InitializeComponent() runs.
nobugz  Thursday, October 09, 2008 8:23 AM
This is a tuff one. Very unusual exception, first time I've seen it in this forum. The check is definitely there in the .NET framework though, it checks that the form's Handle was created by the same thread as the control Handle. You are using threading, that's clear from the call stack so at least it is not completely out of nowhere. The mgr.Form and player.Listenerer assignments could possibly have side-effects, but that's just a guess. Move InitializeComponent() to the top of the constructor code, what happens?
nobugz  Wednesday, October 08, 2008 11:32 PM
Thanks for the feedback. The two property assignments definitely don't have side effects, they just set instance variables, so that can't affect anything other than timing. Is the check just something like control.owner == handle.owner? Is there any chance the owner of either doesn't get set sometimes? It's clearly an intermittent issue and it's only occurred on two machines, albeit different ones. I'm certainly stumped.

pjeffe  Thursday, October 09, 2008 1:08 AM
You'll need to consider a threading race, some other thread is using your form before it is completely constructed. Windows Forms creates window handles lazily. Normally, that happens in response to a Show() call, just before the Load event runs. The exception you get indicates that the handle already exists by the time InitializeComponent() runs.
nobugz  Thursday, October 09, 2008 8:23 AM
OK, it is possible that this could happen under certain circumstances, so I'll prevent that and hopefully that will do the trick. Thanks for the help!
pjeffe  Thursday, October 09, 2008 1:49 PM

You can use google to search for other answers

Custom Search

More Threads

• problem painting a control
• ObjectDataSource .NET 2.0
• Setting the order of docked controls
• rotatable arrow on my image
• How can I persuade my company to use Windows Forms?
• Forms under 120 DPI settings
• override autoscroll property of panel. show vscrollbar,hscrollbar in panel.
• Problems with cellformatting on datagridview
• Using ReadFile() asynchronously
• Making a .net executable from windows application