在WPF中使用EntityFrameWork加载数据时显示进度条 [英] Show Progressbar when loading data using EntityFrameWork in WPF
问题描述
我想在进度栏中中显示从数据库中获取数据的进度.目前,我正在更新 Button
的内容.不知道如何在此代码中实现.
I want to show the progress of Data being fetched from Database in a Progressbar
. Currently i am updating the content of a Button
. Have no idea how to implement in this code .
private async void FetchInvoicesDataFunc(object sender, RoutedEventArgs e)
{
ProgressBtn.Content = "Loading Data ...";
InvoiceGrid.ItemsSource = await Task.Run(FetchInvoiceDataAsync);
ProgressBtn.Content = "Loaded !";
}
private async Task<List<Invoice>> FetchInvoiceDataAsync()
{
List<Invoice> result;
using(var context = new Intelliventory_DBEntities() )
{
result = await context.Invoices.Where(b => b.InvoiceID <= 2000).Include(x => x.Customer).ToListAsync();
}
return result;
}
}
推荐答案
您将无法显示EF查询的百分比,因为它不会通知您有关数据库查询的进度(我不会不知道任何能让客户了解查询状态的数据库系统
You won't be able to show the percentage of an EF-Query, as it doesn't notify you about the progress of the DB query (I don't know any DB-System that keeps the client informed about the status of a query)
您可以考虑将查询分为以下几个部分:
You could consider splitting your query into pieces like this:
private async void FetchInvoicesDataFunc(object sender, RoutedEventArgs e)
{
List<Invoice> results = new List<Invoice>();
ProgressBtn.Content = "Loading Data ...";
await Task.Run(async() => results.AddRange(await FetchInvoiceDataAsync(0, 500));
ProgressBtn.Content = "25% done...";
await Task.Run(async() => results.AddRange(await FetchInvoiceDataAsync(501, 1000));
ProgressBtn.Content = "50% done...";
await Task.Run(async() => results.AddRange(await FetchInvoiceDataAsync(1001, 1500));
ProgressBtn.Content = "75% done...";
await Task.Run(async() => results.AddRange(await FetchInvoiceDataAsync(1501, 2000));
InvoiceGrid.ItemsSource = results;
ProgressBtn.Content = "Loaded !";
}
private async Task<List<Invoice>> FetchInvoiceDataAsync(int start, int end)
{
List<Invoice> result;
using(var context = new Intelliventory_DBEntities() )
{
result = await context.Invoices.Where(b => b.InvoiceID >= start && b.InvoiceID <= end).Include(x => x.Customer).ToListAsync();
}
return result;
}
请注意,这将对应用程序的性能产生负面影响.有成千上万的教程介绍如何在WinForms和WPF中使用进度条,因此我不会深入探讨该主题.
Please note that this will have a negative impact on the performance of your application. There are thousands of tutorials how to use progressbars both in WinForms and WPF so i won't dive into that topic.
这篇关于在WPF中使用EntityFrameWork加载数据时显示进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!