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调用的方法内的绑定集合中。。