跨线程操作无效: [英] Cross-thread operation not valid:

查看:107
本文介绍了跨线程操作无效:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这是因为我正在使用后台工作程序,但是这是调试时遇到的异常:

跨线程操作无效:从其他线程访问的控件"rtBoxStatus"

那么该如何解决呢?

解决方案

您不能从其他线程访问控件.尝试使用Form.Invoke,它将在正确的线程上调用指定的委托:

this.Invoke(new Action(DoSomething1)); DoSomething2),``something2'');

...


private void DoSomething1()
;
}

私有无效的DoSomething2(字符串arg)
br/>

I assume this is because I'm using a background worker, but here is the exception I get when debugging:

Cross-thread operation not valid: Control 'rtBoxStatus' accessed from a thread other than the thread it was created on.

So how do I fix this?

解决方案

You cannot access controls from other threads. Try using Form.Invoke, it will call the specified delegate on the correct thread:

        this.Invoke(new Action(DoSomething1));
        this.Invoke(new Action<string>(DoSomething2), "something2");

...

        private void DoSomething1()
        {
            label1.Text = "something1";
        }

        private void DoSomething2(string arg)
        {
            label2.Text = arg;
        }


这篇关于跨线程操作无效:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆