无法获取在ListView跨线程项目 [英] Can't get items in a ListView cross-thread

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

问题描述

后台工作需要通过每个迭代在的ListView <项目/ A>。不过,我不能做到这一点:

My background worker needs to iterate through each item in a ListView. However I can't do this:

foreach (ListViewItem Item in List.Items)

,因为它是一个跨线程操作。

because it's a cross thread operation.

我也不能把在<一个项目href=\"http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.listviewitemcollection.aspx\"相对=nofollow> ListView.ListViewItemCollection ,并从阅读后台工作。这仍是试图访问的的ListView 并创建一个跨线程操作。

I also can't put the items in a ListView.ListViewItemCollection and make the background worker read from that. This is still trying to access the ListView and creating a cross thread operation.

我怎样才能让的的ListView 的,而不把它们放在一个变量的地方提供给后台工作的项目?

How can I make the ListView's items available to the background worker without putting them in a variable somewhere?

推荐答案

尝试使用此功能。

private delegate ListView.ListViewItemCollection GetItems(ListView lstview);

private ListView.ListViewItemCollection getListViewItems(ListView lstview)
{
    ListView.ListViewItemCollection temp = new ListView.ListViewItemCollection(new ListView());
    if (!lstview.InvokeRequired)
    {
        foreach (ListViewItem item in lstview.Items)
        temp.Add((ListViewItem)item.Clone());
        return temp;
    }
    else
        return (ListView.ListViewItemCollection)this.Invoke(new GetItems(getListViewItems), new object[] { lstview });
}

这样称呼它:

foreach(ListViewItem item in getListViewItems(List))

这篇关于无法获取在ListView跨线程项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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