Monday, August 30, 2010

.NET FileSystemWatcher and dropping mutiple files

If you spend too much time in the event handler, FileSystemWatcher will miss events. This leads to lost business functionality. There are lot of questions around this topic (how to handle multiple file drops).

Even though you cannot spend too much time in the FileSystemWatcher event handlers, you have to wait until 'at least' file copy is complete (file created event will be fired as soon as the first byte is copied).

I design and develop quite a few server applications which have to be scalable and handle any amount of load. I solved the above mentioned problem as follows.

1. I have created two separate services. 'FileWatcher' and 'FileProcessor'.

FILE PROCESSOR SERVICE
This consists of two logical components. First one writes the full path of the dropped file to a message queue (MSMQ) called 'pre-copy-queue'. Note that there is absolutely no delay here. The other component in this service will pick up one item at time from this queue and waits until it is copied before it makes an entry in queue (MSMQ) called 'FileReadyToBeProcessed'. I am also processing mutiple files in parallel using SmartThreadPool.

FILE PROCESSOR SERVICE
This process picks up items from the queue (MSMQ) 'FileReadyToBeProcessed' and does whatever needs to be done. I am using SmartThreadPool to process multiple items in parallel.

If you are interested in code snippets, let me know.

Followers