Reputation: 21
We have developed a vb.net application using multi-threaded listener concept and a dedicated thread for each folder to monitor. With that we simultaneously keep track of the incoming files in each folder.
Currently, we are facing an issue whenever we receive multiple files of bulk volume, parallel processing of the files getting delayed with thread concept or failing sometimes. We need to implement the same logic with some other concept avoiding multi-hreading. Please provide your suggestion on the same. Or is it possible to achieve performance tuning with the same thread concept? Please guide me.
Upvotes: 2
Views: 199
Reputation: 48949
Here is how I would approach the problem.
First, use FileSystemWatcher
to monitor the folders. When the Created
event (or any other event of concern) is raised then add the file which caused the event into a queue. This queue will contain all of the files needing to be processed.
Second, use the producer-consumer pattern to process the files added to the queue. This is most easily accomplished if your queue is of type BlockingCollection
. The nice thing about BlockingCollection
is that it abstracts most of the producer-consumer logic for you. The Take
method blocks until at least one item is added to the queue. You can have as many threads as you want monitoring this queue. They will wait patiently until items are added to the queue.
Here are a few of the advantages of using this approach.
BlockingCollection
is already thread-safe.FileSystemWatcher
event handlers which are adding the files to the queue.Upvotes: 2
Reputation: 4879
I suspect what might help you is a combination of File System monitor and a background worker subsystem.
For FileSystemMonitor, check here
http://www.codeproject.com/KB/files/monitor_all_filesystem.aspx
Basically, there are apis you can use to register yourself to be signalled when specific folder change (new/changed/deleted files).
Couple that with a system of performing work in the background via BackGroundWorked objects, and you shouldn't really have to directly deal with multiple threads at all. BackgroundWorkers will automatically be spun up on seperate threads to keep your ui responsive.
Upvotes: 0