在C#中的跨线程中更新列表框绑定列表 [英] Updating a listbox binded list in a cross-thread in c#

查看:173
本文介绍了在C#中的跨线程中更新列表框绑定列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是要分两个部分.首先,我有一个学生ID的列表,该列表会不断添加到程序中或从程序中删除.我如何拥有它,以便列表框中仅显示列表中的当前项目.因此,例如,如果我有三个ID并加一个ID,则列表框会自动显示4;如果我从列表中取出一个项目,则列表框也会类似地显示,列表框会自动缩小1个条目.

My question if kind of a two parter. First of all, i have a list of student id's that is constantly added to and removed from within the program. How can i have it so that only the current items in the list are displayed in the listbox. So for example, if i have three id's and i add one, the listbox shows 4 automatically and similarly if i take one item away from the list, the listbox shrinks by 1 entry automatically.

第二,对学生ID列表的所有加减都是通过backgroundworker_do工作线程完成的.那么,当在ui线程之外工作并处于工作线程中时,如何获得上述功能?示例代码将不胜感激

Secondly, all this adding and subtracting to the student id list is done with a backgroundworker_do work thread. So how can i get the above functionality while working outside the ui thread and being in the worker thread? Sample code will be much appreciated

预先感谢!

推荐答案

对于第一部分,您可以使用 Control.Invoke() /

For the first part, you can use an ObservableCollection instead of a List. For the second part, as you know, you cannot update UI elements from backgroundworker thread so you need to marshall the updating logic back to UI thread. This can be achieved by using Control.Invoke() / Control.BeginInvoke(). There are plenty of examples out there for both topics so I'll just let you figure the details out.

对于winforms,您可以查看 BindingList<T> .这是我创建的一个简单示例.

For winforms, you can look into BindingList<T>. Here's a quick example I created.

说您有

    class Car
    {
        public string CarName { get; set; }

        public override string ToString()
        {
            return CarName;
        }
    }

您可以像这样创建BindingList

    BindingList<Car> carList = new BindingList<Car>();

    private void Form1_Load(object sender, EventArgs e)
    {
        carList.Add(new Car{CarName = "Foo"});
        listBox1.DataSource = carList;
    }

将一项添加到列表中,并将其设置为列表框的数据源.

add one item to the list and set it as the datasource for the listbox.

从UI线程更新列表很简单(请注意,将其添加到列表中会自动将该项目添加到列表框中.

Updating the list from the UI thread is simple (note adding to the list automatically adds the item to listbox.

    private void btnUIThread_Click(object sender, EventArgs e)
    {
        carList.Add(new Car{CarName = "BarFromUIThread"});
    }

这是您可以从后台工作人员添加(或删除)项目的方法.

Here's how you can add (or remove) items from a background worker.

private void btnBackgroundworker_Click(object sender, EventArgs e)
{
    BackgroundWorker bg = new BackgroundWorker();
    bg.DoWork += bg_DoWork;
    bg.RunWorkerAsync();
}

private delegate void UpdateUIDelegate();
void bg_DoWork(object sender, DoWorkEventArgs e)
{
    listBox1.Invoke(new UpdateUIDelegate(UpdateListBox));
}

private void UpdateListBox()
{
    carList.Add(new Car { CarName = "BarFromBackgroundWorkerThread" });
}

这篇关于在C#中的跨线程中更新列表框绑定列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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