跨线程操作 [英] Cross thread operation

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

问题描述

我正在尝试访问从工具箱中下拉创建的组合框,然后创建一个新线程并尝试通过新创建的线程在组合框中添加项目,并且出现错误,如跨线程操作无效:控制'comboBox1'从其创建的线程以外的线程访问。



所有有价值的解决方案将被赞赏

I am trying to access a combobox created by drop down from the toolbox and then created a new Thread and trying add items in the combobox through the newly created thread and tje error arise like Cross-thread operation not valid: Control 'comboBox1' accessed from a thread other than the thread it was created on.

all the valuable solution will be appreciated

推荐答案

您只能从创建它们的同一个线程更改UI控件元素。通常是主线程(UI线程)。

如果是WinForm,你必须通过Control.Invoke [ ^ ]命令。



你可以这样做:

You can only change UI Control elements from the same thread that created them. Usually Main Thread (UI Thread).
If it is WinForm, you have to switch from worker thread to Main Thread via Control.Invoke[^] commands.

You can do something like this:
Invoke(new Action(() => comboBox1.Items.Add("Hello from Thread"));


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

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