c# - 如何从其他线程将项目添加到列表框?

标签 c# multithreading

我要开始新话题了:

Thread t = new Thread(UpdateListOutput);    
t.IsBackground = true;    
t.Start();

更新列表输出:

void UpdateListOutput()
{
    while (true)
    {
        if (!string.IsNullOrEmpty(engineOutput))
        {
            OutputBox.Items.Add(engineOutput);
        }
    }
}

我收到错误:

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)));

关于c# - 如何从其他线程将项目添加到列表框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18534458/

相关文章:

java - 一个线程所做的更改不会反射(reflect)在另一个线程中

c# - 如何 "read"SWIFT 消息?

c# - 在更改的回调中设置 DependencyProperty 值

java - java中的ReentrantLock条件等待获取IllegalMonitorStateException

c - 多线程c程序设计帮助

通过引用返回的 C++ 共享互斥锁

c# - 将数据存储在程序中或文档文件中有区别吗?

c# - 在关闭表单之前等待任务完成

c# - 弹出UAC消息时屏幕颜色默认为正常(无法检测到)

linux - 当 child 在 parent 到达加入之前到达导出时,如何在父线程中收集子线程状态?