Most people see this error and see one thing: "you are not accessing this control from the main user interface thread." In fact, you can have 100 UI threads if you want (the behavior for this is undefined, but supported). Most likely, panelMain is created on another thread (this); I do not see the code - but it looks like you are creating it in your worker / thread.
To confirm the behavior, try the following:
Action addAction = new Action ( new delegate { panelMain.Controls.Add(UCLoadingScreen); } ) if(panelMain.InvokeRequired) { panelMain.Invoke(addAction);
Be prepared for another error (a child control in another thread is parent for it, not sure which error you will get, but I'm sure you will). It's not a problem.
A factory will fix this:
public void CreateControl<T>() where T : new() { if(InvokeRequired) { return (T)Invoke(new Func<T>(CreateControl<T>())); } return new T(); }
EDIT: panelMain may not be the "intruder" of the thread, as I said, parental controls from different threads lead to undefined behavior. Ensure that all controls are created in the context of the main thread.
Jonathan c dickinson
source share