I have a form ( MainPage ), and I set UserControl several times in it, so I write a method in this form similar to this to call:
delegate void containerPanelCallback(UIPart uiPart); public void IncludeUIPart(UIPart uiPart) { if (this.containerPanel.InvokeRequired) { containerPanelCallback d = new containerPanelCallback(IncludeUIPart); containerPanel.Invoke(d, new object[] { uiPart }); } else { containerPanel.Controls.Clear(); containerPanel.Controls.Add(uiPart); } uiPart.Size = this.containerPanel.Size; uiPart.Dock = DockStyle.Fill; }
UIPart class inherits from UserControl , which my UserControls inherits from UIPart .
This method starts as follows:
public class myClass { ... private static MainPage _frmMain; private static myUIPart6 UIP6; ... public static void aMethod() { UIP6 = new myUIPart6 { }; _frmMain.IncludeUIPart(UIP6); _frmMain.Show(); } ... }
Error:
Cross-threading is not valid: the "MainPage" control is accessible from a thread other than the thread on which it was created.
I found a lot of questions and a lot of answers here about this error, but I canβt understand why it rushes to _frmMain.Show(); ? Should I call something else? Or I'm wrong? Is this related to creating the Handle of my UserControl?
source share