Sheeba
Sheeba

Reputation: 21

Question on Multithread-.Net

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

Answers (2)

Brian Gideon
Brian Gideon

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.

  • It is relatively easy to implement since BlockingCollection is already thread-safe.
  • The amount of parallelism you want is determined by the number of threads you create to monitor the queue.
  • Processing the files will not interfere with the FileSystemWatcher event handlers which are adding the files to the queue.
  • The queue acts as a buffer whose contents will ebb and flow as the rate at which files appear compared to the rate at which those files are processed changes.

Upvotes: 2

DarinH
DarinH

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

Related Questions