Click here to Skip to main content
15,911,646 members
Please Sign up or sign in to vote.
2.17/5 (3 votes)
See more:
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.
Posted
Comments
Prasad Avunoori 9-Jul-14 23:43pm    
Usually the while loop terminates when it reaches list.count().
What do you want then? Can you please post your while loop?
Dave Kreskowiak 10-Jul-14 0:52am    
Don't post the same question in multiple forums. It makes collaboration on an answer a royal pain in the ass.

1 solution

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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900