problem removing tab from a tabcontrol c #

0

I'm trying to remove a tabcontrol, this tabcontrol is with the cef open (browser with google chrome engine) on it I have a button called quit. It is created like this:

TabPage nova = new TabPage();
nova.Text = "aba1"; //aba 2 assim por diante
tabControl1.Controls.Add(nova);
chromeBrowser.Add(new ChromiumWebBrowser("http://sitedoprojeto.com/index.html"));
tabControl1.SelectedIndex = tabControl1.Controls.Count-1;
chromeBrowser[chromeBrowser.Count - 1].Parent = tabControl1.SelectedTab;
chromeBrowser[chromeBrowser.Count - 1].RegisterJsObject("cefCustomObject", new CefCustomObject(chromeBrowser[chromeBrowser.Count - 1], this, nova));

I have tried several ways, passing the last parameter above "new", I also tried to pass it chromeBrowser [chromeBrowser.Count - 1] .Parent, and also tried to remove the selected tab directly, all attempts resulted in the error below : An explanation in code:

In javascript I call the method exit cefCustomObject.sair ();

In the cefCustomObject script I call this:

public void sair()
{
     _instanceMainForm.sair(_instanceTabPage);
}

And in the form I get like this:

public void sair(TabPage _instanceTabPage)
    {
        //aqui removo os dados do sistema e entao da erro nesse comando
        tabControl1.Controls.Remove(_instanceTabPage);
    }

System.InvalidOperationException: 'Invalid thread operation: control' 'accessed from a thread that is not the one in which it was created.'

Does anyone know how to solve this?

    
asked by anonymous 14.06.2018 / 10:04

1 answer

0

I solved it as well

if (tabControl1.InvokeRequired)
            tabControl1.BeginInvoke((MethodInvoker)delegate {
                tabControl1.Controls.Remove(_instanceTabPage);
            });
    
14.06.2018 / 10:18