如何从仍在填充的列表中读取? [英] How do I Read from a list that is still populating?

查看:86
本文介绍了如何从仍在填充的列表中读取?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我正在写一些有两个后台工作者的软件。第一个用于查找硬盘驱动器上的所有文件夹并将它们放入List< string>中。第二个背景工作者在60秒后开火。我需要它开始从列表中提取文件夹名称,因为它将进入每个文件夹并搜索文件。当第一个后台工作人员仍在工作时,我需要能够从列表中读取。我已经尝试了while语句但它在启动循环时从list.count()中获取数字,所以一旦达到该数字就停止。我需要它直到列表完成。有什么建议?在此先感谢。

OK, so I am writing a bit of software that has two background workers. The first one goes and finds all of the folders on a hard drive and puts them in a List<string>. The second background worker fires 60 seconds later. I need it to start pulling the folder names from the List because it is going to go into each of the folders and search the files. I need to be able to read from the List while the first background worker is still doing its work. I have tried the while statement but it grabs the number from the list.count() when it starts the loop so stops as soon as it reaches that number. I need it to go until the List is done. Any suggestions? Thanks in advance.

推荐答案

非常糟糕。再想想。您不能保证某些东西总是在60秒或任何其他硬编码的时间内完成。如果你使用足够的时间,等待线程可能会等待太多。这很荒谬。为什么使用从不并行工作的线程,如果这样做,这会导致麻烦?只是为了说我开发多线程代码? :-)



所以,首先你应该想到的是你为什么要使用这些线程。即使它们并行工作,它的好处是什么?



您的列表还有另一个问题。当您填充最少时,其他一些应用程序可以更改磁盘的内容。在你告诉我们使用你的最终目标之前,我不知道该提出什么建议。这只是需要考虑的事情。



现在,用两个线程处理一些列表的问题非常简单;它由线程同​​步原语解决,例如 lock 。您可以使用一个线程填充集合,并在线程等待数据时使用另一个线程读取元素​​,而不使用任何CPU时间。这是一种称为生产者 - 消费者的经典模式( http://en.wikipedia.org/wiki /制片人 - 消费者 [ ^ ])。但是足够的数据结构是队列,而不是列表。但你甚至不必那样做。从.NET v.4.0开始,您可以使用synchronized collection System.Collections.Concurrent.BlockingCollection<>

http://msdn.microsoft.com/en-us/library/dd267312%28v=vs .110%29.aspx [ ^ ]。



如果您阅读我的文章完整的源代码和用法示例,您可以理解这些工作如何工作详细信息:

线程的简单阻塞队列通信和线程间调用 [ ^ ]。



此外,您可以使用我的 BlockingQueue 类如果你正在使用.N ET版本早于4.0。



-SA
Very bad. Think again. You cannot guarantee that something always finished in 60 seconds or any other hard-coded amount of time. And if you use sufficient amount of time, the waiting thread probably wait too much. This is absurd. Why using threads which never work in parallel, and if they do, this leads to trouble? Just for saying "I develop multithreaded code"? :-)

So, first thing you should think of is why are you using those threads. Even if they work in parallel, what's the benefit of it?

There is another problem with your list. When you are populating the least, some other application can change the content of the disk. And I don't know what to advise until you tell use your ultimate goals. This is just something to think about.

Now, the problem with working on some list by two threads is pretty trivial; it is solved by thread synchronization primitives, such as lock. You can populate a collection with one thread and read the elements as they appear with another thread, without using any CPU time when a thread is waiting for data. This is the classic pattern called producer-consumer (http://en.wikipedia.org/wiki/Producer-consumer[^]). But the adequate data structure is queue, not list. But you don't even have to do that. Since .NET v.4.0, you can use the synchronized collection System.Collections.Concurrent.BlockingCollection<>:
http://msdn.microsoft.com/en-us/library/dd267312%28v=vs.110%29.aspx[^].

You can understand how such things work if you read my article complete with full source code and usage examples explained in detail:
Simple Blocking Queue for Thread Communication and Inter-thread Invocation[^].

Besides, you can use my BlockingQueue class if you are using .NET version earlier than 4.0.

—SA


这篇关于如何从仍在填充的列表中读取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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