C# – Control.Invoke fails at second call

ccompact-frameworkinvokewindows-mobilewinforms

I'm developing a Windows Mobile 5.0 and above application with .Net Compact Framework 2.0 SP2 and C#.

I have this code inside a method:

if (listBox1.InvokeRequired)
{
    Invoke(new MethodInvoker(
        delegate() { listaBox1 = listaBox2; listBox1.Visible = true; }));
}
else
{
    listBox1 = listBox2;
    listBox1.Visible = true;
}

When I run it, it throws an exception on second statement (listBox1.Visible = true;) saying:

Control.Invoke must be used to interact with controls created on a separate thread.

What's happening?

Best Answer

Your two ListBoxes were created on different threads. That is, in almost all cases, a really, really bad idea.