Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1472
  • Last Modified:

Windows service and the File System Watcher

Hi there, I am using the File System Watcher within a windows service to process files that are created in a specific directory.
While the service is busy processing a file I want the service to process other files that are created at the same time. At the moment it waits until the first file is processed and then starts with the next file. Is this possible? I put in a doevents on the created event of the file system watcher but to no avail.
2 Solutions
you should look at multi threading here, as soon as the file system watcher detects a file it should start the processing in a seperate thread, and while the file is still being processed if another file is detected then the file watcher should spawn a new thread to handle the processing of the file.... i.e. everyfile should be processed by a separate thread, or you could look at the possibility of creating a thread pool
you will need a multithreaded solution. you have two choices here, create a new thread every time your file watcher event fires or create a thread pool of a fixed number of threads and have them pick items from a queue, the event handler can add items to this queue..

the first choice is easiest to implement but if you have a flurry of files then you will have lots of threads started, too many and this may not be the most efficent way to process them. remeber each thread carries an overhead in context switches etc that cost in CPU time.

the second choice will involve you creating a Thread Pool and have the threads wait on a queue. your event handler can then post items to the queue.

I think .NET has a threadpool ready to go - see this link

azrakdragonAuthor Commented:
Many thanks. Works great now.

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now