开发者

Cross Thread Exception Invalid Object Exception

开发者 https://www.devze.com 2023-01-11 08:02 出处:网络
I am trying to get around a Cr开发者_运维问答oss 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 t

I am trying to get around a Cr开发者_运维问答oss 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.

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.

Thank you so much in advance!

Richard

MethodInvoker method = delegate
{
    label3.Enabled = true;
};

if (InvokeRequired)
{
    BeginInvoke(method);
}
else
{
    method.Invoke();
}


To make sure your UpdateUI method is executed on the GUI thread, use the following:

public void UpdateUI() {
    if (this.InvokeRequired) {
        this.Invoke(new MethodInvoker(UpdateUI));
    } else {
        // Changes to UI are made here.
    }
}

Replace this with the reference to your form if appropriate.


Your calls to Invoke or BeginInvoke should be on a control that exists in the target thread. So you should be using Control.Invoke or Control.BeginInvoke.

Internally Invoke actually sends a message (synchronous) to the UI thread message queue, and BeginInvoke posts a message (asynchronous) to the message queue, since the message queue processes messages one at a time, you probably would not need to lock on the call to Invoke/BeginInvoke.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号