如何在从数据库加载数据表时显示进度条 [英] How do I display a progress bar while loading a datatable from database

查看:71
本文介绍了如何在从数据库加载数据表时显示进度条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Entity Framework从数据库加载数据时如何显示进度条?



我尝试过:



i尝试使用Backgroungworker但失败了:(

How can display a progress bar while data is loading from database using Entity Framework?

What I have tried:

i try to using Backgroungworker but failed :(

推荐答案

如果你想在长期任务中显示进度条它继续,然后你需要在第二个线程中执行长进程,并在正常UI线程中更新进度控件 - 因为你不能从UI线程以外的线程访问任何控件(你会得到一个交叉线程异常,如果你尝试)。

通常,BackgroundWorker是理想的,因为它通过在原始线程上处理的事件提供进度更新机制,并允许您更新直接进度控制。



但是......除非你的数据是通过SqlDataReader加载的,否则当你可以定期更新进度时它不会有帮助。如果你使用DataAdapter - 和EF做 - 然后没有进度指示:所有工作都在服务器端完成,而客户端(您的应用程序)只有在数据全部完成时才知道数据:这是一个单一的过程。



最好的办法是使用BackgroundWorker(或直接使用Thread),否则UI根本不会更新,并在Marquee模式下使用ProgressBar。
If you want to show a progress bar while a long task it going on, then you need to do the long process in a second thread, and update the progress control in the "normal" UI thread - as you cannot access any control from a thread other than the UI thread (and you will get a "Cross threading exception" if you try).
Normally, the BackgroundWorker is ideal for this, as it provides a progress update mechanism via an event which is handled on the original thread and allows you to update the progress control directly.

But ... unless your data is being loaded via a SqlDataReader when you can update progress regularly it's not going to help. If you use a DataAdapter - and EF does - then there is no "progress" indication: all the work is done at the Server end, and the Client (your app) is only made aware of the data when it is all complete: it's a monolithic process.

Your best bet is to use a BackgroundWorker (or a Thread directly) or the UI won't update at all, and use a ProgressBar in Marquee mode.


这篇关于如何在从数据库加载数据表时显示进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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