【发布时间】:2023-03-27 22:56:01
【问题描述】:
我正在数据网格视图中加载一些数据(1,200,000 行),而应用程序加载时间过长,有时会死机。
我不知道如何异步加载它们? (也许有progressBar)。
我可以在这里寻求帮助吗?
【问题讨论】:
标签: c# wpf visual-studio datagrid asynchronous
我正在数据网格视图中加载一些数据(1,200,000 行),而应用程序加载时间过长,有时会死机。
我不知道如何异步加载它们? (也许有progressBar)。
我可以在这里寻求帮助吗?
【问题讨论】:
标签: c# wpf visual-studio datagrid asynchronous
我有一个应用程序,我正在使用线程做一些非常相似的事情。此代码应在后面的代码运行时一次更新您的数据网格。
using System.Windows.Threading;
private void Run()
{
try
{
var t = new Thread(Read) { IsBackground = true };
t.Start();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void Read()
{
foreach (/* whatever you are looping through */)
{
/* I recommend creating a class for the result use that for the
datagrid filling. */
var sr = new ResultClass()
/* do all you code to generate your results */
Dispatcher.BeginInvoke(DispatcherPriority.Normal,
(ThreadStart)(() => dgResults.AddItem(sr)));
}
}
【讨论】:
将数据加载分解为更小的块,例如一次 100 到 1000 行。如果 WPF 网格数据绑定到您的数据集合,并且该集合是可观察的集合(实现 INotifyCollectionChanged),则 WPF 将在向集合中添加新数据时自动更新显示。
您还应该考虑将虚拟化列表控件或网格与分页数据源结合使用,以便仅加载当前显示在屏幕上的数据(而不是内存中的 120 万行数据)。这将为您执行“分块”,并使您能够以非常少的内存使用或网络延迟向用户呈现基本上无限量的数据。
查看这篇关于为虚拟列表框异步检索数据的 SO 文章:How do I populate a ListView in virtual mode asynchronously?
【讨论】: