Cross Thread Exception Invalid Object Exception
<p>I am trying to get around a Cross Thread Exception, Invalid Object Exception. Here are the details: I have two Background worker threads (there will be lots more, but I haven't coded them yet, I am just coding the engine for them all to run on at the moment. This is a C#, Windows Forms app. Every so often, the BGWs call UpdateUI, which does a lot of updating to the UI. The same UpdateUI is used for all threads, and so it is Locked. The performance degradation of this is so minimal, that it does not matter to me. Obviously, I am now altering the UI with a BGW, ie a different thread, and so I will get a Cross Thread Exception, and so I need to Invoke(), or BeginInvoke(), but whenever I do, somehow, all of these time-consuming processes get merged onto the same thread, and execute in order, rather than at the same time. This will be because my code is wrong, not because of any other reason. I don't really know how to use Invoke() and BeginInvoke(), and so this is copied code from similar threads, rather than my own, but it still doesn't work. Just looking at it, it seems very odd, and not what I would have done.</p>

<p>So, could you please help me correct my Invoke() code? It removes the exception, but merges them all onto one thread, but not exactly sure which one, the UI thread? Also, my UpdateUI() is very long and complex, and so ideally, I would Invoke() the entire method (basically the whole thing needs Invoking) rather than each line individually, as the blow code is doing, if possible. I repeat, this is NOT what I am using to Invoke() the method, but simply change an "Enabled" status, which is why it seems so odd.</p>

<p>Thank you so much in advance!</p>

<p>Richard</p>

<pre><code>MethodInvoker method = delegate
{
    label3.Enabled = true;
};

if (InvokeRequired)
{
    BeginInvoke(method);
}
else
{
    method.Invoke();
}
</code></pre>
