如何从其他线程将项目添加到listBox? [英] How to add item to listBox from other thread?
本文介绍了如何从其他线程将项目添加到listBox?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在启动新线程:
Thread t = new Thread(UpdateListOutput);
t.IsBackground = true;
t.Start();
UpdateListOutput:
UpdateListOutput:
void UpdateListOutput()
{
while (true)
{
if (!string.IsNullOrEmpty(engineOutput))
{
OutputBox.Items.Add(engineOutput);
}
}
}
我收到错误消息:
跨线程操作无效:控件'OutputBox'从 除了创建线程以外的其他线程.
Cross-thread operation not valid: Control 'OutputBox' accessed from a thread other than the thread it was created on.
我该如何运行?
推荐答案
尝试一下
this.Invoke((MethodInvoker)(() => OutputBox.Items.Add(engineOutput)));
这篇关于如何从其他线程将项目添加到listBox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文