Getting Cross-thread operation not valid [duplicate]

You can only make changes to WinForm controls from the master thread. You need to check whether InvokeRequired is true on the control and then Invoke the method as needed.

You can do something like this to make it work:

public void CheckUnusedTabs(string strTabToRemove)
{ 
    if (TaskBarRef.tabControl1.InvokeRequired)
    {
        TaskBarRef.tabControl1.Invoke(new Action<string>(CheckUnusedTabs), strTabToRemove);
        return;
    }      

    TabPage tp = TaskBarRef.tabControl1.TabPages[strTabToRemove];
    tp.Controls.Remove(this);
    TaskBarRef.tabControl1.TabPages.Remove(tp);
}

Leave a Comment