Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将项目添加到ObservableCollection<;T>;不阻塞用户界面_C#_Observablecollection - Fatal编程技术网

C# 将项目添加到ObservableCollection<;T>;不阻塞用户界面

C# 将项目添加到ObservableCollection<;T>;不阻塞用户界面,c#,observablecollection,C#,Observablecollection,我正在尝试使用SqlReader从数据库表向observateCollection添加项。代码如下: public void GetProductSpecification() { ProductSpecificationList.Clear(); using (var connect = Connection.Connect()) { string query = "select * from ut_kst_specyfikacje_indeksow_

我正在尝试使用
SqlReader
从数据库表向
observateCollection
添加项。代码如下:

public void GetProductSpecification()
{
    ProductSpecificationList.Clear();

    using (var connect = Connection.Connect())
    {
        string query = "select * from ut_kst_specyfikacje_indeksow_test";

        using (SqlCommand cmd = new SqlCommand(query, connect))
        {
            try
            {
                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        ProductSpecificationList.Add(new ProductSpecification()
                        {
                            //Here I am setting values of fields from reader[xx]. No parsing/converting errors in here.
                            //Also tried Dispatcher in here, but it throws exception "Invalid attempt to call MetaData while reader is closed"
                        });
                    }

                }

            }
            catch (SqlException e)
            {
                NLogger.logger.Error(e.Message);
            }
        }
    }
}
此方法在绑定到按钮的我的
ICommand
命令之一中调用。当我单击此按钮时,UI冻结约10秒(在
ut\u kst\u specyfikacje\u indexshow\u test
中有45000条记录)。在我看来,它有点慢,老实说,我预计大约2秒

据我所知,
observateCollection
绑定到UI,不能从UI线程以外的其他线程更新。我尝试使用
调度程序
,但出现了异常:

在读取器关闭时调用元数据的尝试无效


如何做到这一点?

问题是,您的
可观察收集的每一项添加都会发出通知。这对45000件物品来说不太管用

请注意,有很多解决方案,但是,由于您每次都要清除它,只需创建一个新列表并一次性更新即可。事实上,除非您想添加和删除项目,否则您甚至不需要
observateCollection
。然而,下面是一个一次更新的例子,可以更好地使用您的UI

var list = new List<ProductSpecification>();

while (reader.Read())
{
    list.Add(new ProductSpecification()
                            {
                                //Here I am setting values of fields from reader[xx]. No parsing/converting errors in here.
                                //Also tried Dispatcher in here, but it throws exception "Invalid attempt to call MetaData while reader is closed"
                            });
} 

ProductSpecificationList = new ObservableCollection<ProductSpecification>(list);
var list=newlist();
while(reader.Read())
{
列表.添加(新产品规范()
{
//这里我正在设置读取器[xx]中字段的值。这里没有解析/转换错误。
//在这里也尝试了Dispatcher,但它引发异常“在读卡器关闭时调用元数据的尝试无效”
});
} 
ProductSpecificationList=新的ObservableCollection(列表);
注意:假设
ProductSpecificationList
引发属性更改事件

还考虑整批代码<代码>异步任务< /代码>,并使用<代码>异步y>代码>方法> <代码> ExeRealEdErasyCyc < /C> >和类似


添加胡椒粉和盐来调味

问题是,您每次添加到
可观察收集
中都会发出通知。这对45000件物品来说不太管用

请注意,有很多解决方案,但是,由于您每次都要清除它,只需创建一个新列表并一次性更新即可。事实上,除非您想添加和删除项目,否则您甚至不需要
observateCollection
。然而,下面是一个一次更新的例子,可以更好地使用您的UI

var list = new List<ProductSpecification>();

while (reader.Read())
{
    list.Add(new ProductSpecification()
                            {
                                //Here I am setting values of fields from reader[xx]. No parsing/converting errors in here.
                                //Also tried Dispatcher in here, but it throws exception "Invalid attempt to call MetaData while reader is closed"
                            });
} 

ProductSpecificationList = new ObservableCollection<ProductSpecification>(list);
var list=newlist();
while(reader.Read())
{
列表.添加(新产品规范()
{
//这里我正在设置读取器[xx]中字段的值。这里没有解析/转换错误。
//在这里也尝试了Dispatcher,但它引发异常“在读卡器关闭时调用元数据的尝试无效”
});
} 
ProductSpecificationList=新的ObservableCollection(列表);
注意:假设
ProductSpecificationList
引发属性更改事件

还考虑整批代码<代码>异步任务< /代码>,并使用<代码>异步y>代码>方法> <代码> ExeRealEdErasyCyc < /C> >和类似


添加胡椒粉和盐调味

我会将数据库调用与收集操作分离。您可以使方法
异步
并返回任务(例如),然后在
等待
s db调用的方法内将结果同步合并到绑定集合中。我会将数据库调用与集合操作分离。您可以使方法
异步
并返回任务(例如),然后将结果同步合并到
等待
s db调用的方法内的绑定集合中。。