开发者

Fill DataGridView with a large amount of data from an asynchronous source

开发者 https://www.devze.com 2023-04-10 11:55 出处:网络
I\'m writing an application that reads from a bunch of feeds served by a webserver and, after processing the response, dumps the results into a DataGridView. There are a few hundred sources, each of w

I'm writing an application that reads from a bunch of feeds served by a webserver and, after processing the response, dumps the results into a DataGridView. There are a few hundred sources, each of which responds with about 200k items. These are then filtered based on some regex rules, and the ones that pass (there's about a 30% pass rate) get put into the DataGridView.

I need a mechanism to do this, but with the following constraints:

  • UI must remain responsive at all times.
  • Need to fetch 开发者_如何转开发5 resources at any point in time (i.e. in parallel).
  • Have to process the results using regex in parallel.
  • Time between data being fetched from the server and it appearing in the grid must be minimal.
  • Would prefer to avoid Application.DoEvents().

I'm using WebClient.DownloadStringAsync to grab the data from the server. Beyond this, I've tried a few solutions but none seem to work well.


You can follow the producer/consumer pattern

Producer Thread periodically polls your feeds for new items and performs the regex filtering. If the data passes the filter then it gets added to a concurrent queue of pending items (See System.Collections.Concurrent namespace if you are using .NET 4 -- will make this nice)

Consumer thread periodically polls the queue of pending items and if it finds any items then it adds the new items to your data source that is bound in the GUI. This will require an invocation onto the GUI thread

0

精彩评论

暂无评论...
验证码 换一张
取 消