If your user control does not immediately appear after it is created, the handle will not be created in the stream that you think is created. This is not a C # object whose parent thread is important, this is a Windows Handle object whose parent is important.
To force the control to be instantly created in the thread that you think you created it, then read control.Handle , which will cause the control to actually be assigned and the handle is assigned.
MyUserControl uc = new MyUserControl(); // the handle is not created here uc.Visible = false; IntPtr dummy = uc.Handle; // The control is immediately given a real handle
You can also try playing with uc.CreateControl, but this will not create a handle if the control is not displayed.
You may now have a different thread updating your user control, even if the user control is not displayed.
uc.BeginInvoke((Action)(() => uc.Text = "ha ha"));
If you leave the line dummy = uc.Handle , you will get an exception that you cannot call BeginInvoke for a control that does not have a handle.
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.createcontrol(v=vs.90).aspx
Mark lakata
source share