Windows Service that monitors a folder
Posted on 2004-11-04
I'm trying to create a Windows Service that monitors a given directory. Whenever a file is dropped inside that directory, it processes that file.
When I created this service (using the walkthrough on MSDN) and installed it, it timed out when I tried to start it. It *was* processing the files dropped in the directory, but its status in the MMC was "starting" rather than "started."
I'm assuming I need to return control to the OS somehow, but how? I thought FileSystemWatcher is non-blocking. Do I need to launch a new thread?
Here's the main monitoring function for my application (called through OnStart in the service):
private void Monitor(string path)
// The file monitor for the semaphore directory
FileSystemWatcher watcher = new FileSystemWatcher(path);
watcher.NotifyFilter = NotifyFilters.LastWrite |
NotifyFilters.DirectoryName | NotifyFilters.FileName;
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.EnableRaisingEvents = true;
// Wait for changes in the semaphore directory