Click here to Skip to main content
15,881,089 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I need to parallelize a portion of an application (in C# 2010) that returns a list of strings (file names). Can someone point me to a good example of how to put this in a worker thread that requires both input (I've read about how to do that) AND output?

I've done multithreading in MFC and simple ones in .NET, so I have a bit of experience.

#################################################

I'll try to improve the question. I obtain from the user a path to a directory, and I want to parse all the subdirectories and return a list of PDF files in the subdirectories. This can take quite a while so I want to

1> start a thread , giving it the top directory path
2. have the thread go through all sub-directories , building a n array of paths to PDF files
3) when the thread is done, obtain the array of PDF files for further processing
4) the reason I want to do this in a thread is that the UI may be used for some other things while the directory search is ongoing

Hope this is clearer.

I am thinking I should just create my array, put a lock on it, and then access is later when the search is done. This way the thread does not really "return" anything ...

Thanks,
Tom
Posted
Updated 15-Apr-11 17:58pm
v2
Comments
CodingLover 14-Apr-11 23:58pm    
If I'm get you correctly, you have an input and expecting an output as a list of strings. So what you want to do is in parallel thread you want to do that. Am I correct?
Sergey Alexandrovich Kryukov 15-Apr-11 15:53pm    
Is I say, the problem is not well defined, but I have a general and pretty constructive detailed answer, will you see?
--SA

The problem is not well-defined. The notion of "return an object" and "output" in its traditional sense has little in common. It output means simply getting some data produced by a worker thread, the question is "at what moment of time", that is, a synchronization.

Let's start from a simple model: a worker thread puts some data in a shared memory and another thread reads the latest value. Sometimes it is needed. The simple answer is using lock statement on the same object. Very often several threads mostly read, some threads write, less often. In this case a special kind of mutex or lock helps to improve performance: System.Threading.ReaderWriterLockSlim. You get this lock for read first, and if write is required, "upgrade" it for writing.
One of my articles illustrates the great benefit of this class: Wish You Were Here… Only Once[^] with code samples.

If you think you know a good way of thread input, perhaps you need to think again. Please look at the code of thread wrapper:
How to pass ref parameter to the thread[^].

Now, let's get back to thread "output". A really robust way is thread communication in producer-consumer manner. This can be achieved using System.Threading.EventWaitHandle. Please see my generic blocking queue which can be used for this purpose. It can even be used for inter-thread method invocation. See my Tips/Tricks article on the topic:
Simple Blocking Queue for Thread Communication and Inter-thread Invocation[^].

You will find completed source code and detailed usage samples.

Good luck,
—SA
 
Share this answer
 
Comments
Albin Abel 15-Apr-11 15:28pm    
my 5
Sergey Alexandrovich Kryukov 15-Apr-11 15:52pm    
Thank you, Albin.
--SA
Abhinav S 16-Apr-11 0:03am    
Good answer. 5.
Sergey Alexandrovich Kryukov 16-Apr-11 0:47am    
Thank you, Abhinav.
--SA
You coul try using the Dispatcher class.
 
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