跨线程操作错误 [英] crossthread operations error

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

问题描述

      if (listBox1.InvokeRequired)
       {
           listBox = new StringBuilder(this.listBox1.Text);
       }

这是 c# 中的代码,它在执行时为我的表单中的列表框 listBox1 产生无效的跨线程操作错误.你们能告诉我为什么吗??我也在使用 invokeRequired 方法,也没有更改列表框的内容.

This is the code in c# which when executed produces an invalid cross thread operation error for listBox1 which is a listbox in my form. Could u guys please tell me why?? I am using the invokeRequired method too and am not changing the contents of the listbox either.

推荐答案

InvokeRequired 仅告诉您 Invoke 是有效访问元素所必需的.它不会使访问合法.必须使用 invoke 方法将更新推送到适当的线程

InvokeRequired only tells you that an Invoke is necessary in order to validly access the element. It doesn't make the access legal. You must use the invoke method to push the update to the appropriate thread

Action update = () => listbox = new StringBuilder(this.listBox1.Text);
if (listBox1.InvokeRequired) {
  listBox1.Invoke(update);
} else {
  update();
}

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

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