在Windows Forms应用程序中异步填充datagridview [英] Asynchronously populate datagridview in Windows Forms application

查看:61
本文介绍了在Windows Forms应用程序中异步填充datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

howzit!

我是一名网络开发人员,最近被要求开发Windows窗体应用程序,所以如果您愿意,请忍受(或不要笑!)我的问题有点基本。

I'm a web developer that has been recently requested to develop a Windows forms application, so please bear with me (or dont laugh!) if my question is a bit elementary.

在与客户进行了多次会话之后,我们最终决定使用一个包含5个选项卡的tabcontrol的界面。每个选项卡都有一个数据网格视图,最终 可以容纳25,000行数据(每行约6列)。当标签页被加载时,我已经成功地绑定了网格,并且对于一些记录它可以正常工作,但是当我用20,000个虚拟记录绑定网格时,UI冻结了。当我单击选项卡本身时,就会发生冻结,并且绑定完成后,UI才释放(并且呈现了选项卡页面)。

After many sessions with my client, we eventually decided on an interface that contains a tabcontrol with 5 tabs. Each tab has a datagridview that may eventually hold up to 25,000 rows of data (with about 6 columns each). I have successfully managed to bind the grids when the tab page is loaded and it works fine for a few records, but the UI freezes when I bound the grid with 20,000 dummy records. The "freeze" occurs when I click on the tab itself, and the UI only frees up (and the tab page is rendered) once the bind is complete.

这给客户,并提到了每个网格的分页选项,但是她很敬重不想要这个。然后,我唯一的选择是在后台寻找某种异步方法来执行此操作。我对Windows窗体中的线程了解不多,但是我知道可以使用BackgroundWorker控件来实现此目的。在读了一点之后,我唯一的问题是它理想地用于长时间运行的任务和I / O操作。

I communicated this to the client and mentioned the option of paging for each grid, but she is adament w.r.t. NOT wanting this. My only option then is to look for some asynchronous method of doing this in the background. I don't know much about threading in windows forms, but I know that I can use the BackgroundWorker control to achieve this. My only issue after reading up a bit on it is that it is ideally used for "long-running" tasks and I/O operations.

我的问题:


  1. 如何确定一项长期运行的任务?

  2. 一个人如何不滥用BackgroundWorker控件,即。使用此工具时,有一般的指导方针吗? (我知道在某些情况下可能不希望打开/产生多个线程)

  3. 最重要的是:在之后,如何实现(异步)绑定datagridview 标签页-及其所有子控件-加载。

  1. How does one determine a long-running task?
  2. How does one NOT MISUSE the BackgroundWorker control, ie. is there a general guideline to follow when using this? (I understand that opening/spawning multiple threads may be undesirable in certain instances)
  3. Most importantly: How can I achieve (asychronously) binding of the datagridview after the tab page - and all its child controls - loads.

感谢您阅读本文档( ahem )冗长的查询,对于在此问题上的任何回应/想法/指示,我深表感谢!

Thank you for reading this (ahem) lengthy query, and I highly appreciate any responses/thoughts/directions on this matter!

干杯!

推荐答案


  1. 确定长期运行的任务没有硬性规定。作为开发人员,您必须了解一些事情。您必须了解数据和架构的性质。例如,如果您希望只由一个用户从包含几十行的表中从桌面数据库中获取某些信息,则您甚至不必费心显示等待光标。但是,如果您要通过网络将数百行数据提取到共享数据库服务器,则最好期望这将是一项长期运行的任务,不仅可以通过等待游标来处理,还可以通过释放线程的方式来处理您的用户界面在获取期间。 (您肯定在这里是对的。)

  1. There's no hard and fast rule for determining a long-running task. It's something you have to know as a developer. You have to understand the nature of your data and your architecture. For example, if you expect to fetch some info from a desktop database with a single user from a table that contains a couple dozen rows you might not even bother showing a wait cursor. But if you're fetching hundreds of rows of data across a network to a shared database sever then you'd better expect that it will potentially be a long-running task to be handled not simply with a wait cursor but a thread that frees up your UI for the duration of the fetch. (You're definitely on the right track here.)

BackgroundWorker是一种处理表单线程的快速而肮脏的方法。在您的情况下,它将在很大程度上将数据的获取与用户界面联系在一起。它是可行的,可以正常工作,但绝对不被认为是线程,OOP,关注点分离等的最佳实践。如果您担心滥用线程的分配,则可能需要阅读线程池

BackgroundWorker is a quick and dirty way of handling threading in forms. In your case, it will very much tie the fetching of data to the user interface. It is doable, works fine but certainly is not considered "best practice" for threading, OOP, separation of concerns etc. And if you're worried about abusing the alocation of threads you might want to read up on the ThreadPool.

这是一个很好的示例,该示例将异步线程与线程池一起使用。要进行数据绑定,您需要在线程中获取数据,并在获得回调时,只需将结果集分配给网格视图的datasource属性即可。

Here's a nice example of using asynchronous threading with the thread pool. To do data binding, you fetch your data in the thread and when you get your callback, simply assign the result set to the the grid view's datasource property.

这篇关于在Windows Forms应用程序中异步填充datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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